JuliaServices / TreeTransform.jl

A Julia package to transform the nodes of a tree until it reaches a fixed point.
Other
3 stars 1 forks source link

Rewrite does not apply for `Expr` with [a, a] #6

Closed mbravenboer closed 1 year ago

mbravenboer commented 1 year ago

This example works when replacing Expr with some specific struct.

module GenericExpr

using Rematch2: @match2
using TreeTransform: bottom_up_rewrite, TreeTransform
using Test

function xform(node)
    dump(node)
    @match2 node begin
        Expr(:call, [a, a]) => 1
        x => x
    end
end

function simplify(node)
    bottom_up_rewrite(xform, node)
end

@testset "Check some simple cases" begin
    @test simplify(Expr(:call, [:a, :a])) == 1
end

end
gafter commented 1 year ago

You are passing Expr(:call, [:a, :a]) to simplify. That is

Symbol call
Symbol a
Array{Symbol}((2,))
  1: Symbol a
  2: Symbol a
Array{Any}((1,))
  1: Array{Symbol}((2,))
    1: Symbol a
    2: Symbol a
Expr
  head: Symbol call
  args: Array{Any}((1,))
    1: Array{Symbol}((2,))
      1: Symbol a
      2: Symbol a

Perhaps you meant Expr(:call, :a, :a) because the Expr constructor is vararg? That works. Also :(a(a)) works.