I’m trying to write a language parser and build a nice AST. In the language, a function is essentially a variable with a callable value. For example:
int f(int arg) {...};
#int(int) f: int(int arg) {...};
both are equal, and I want to transform the first into the second. As you can see, the variable’s type contains parameters, but without name. The function value needs the parameter name.
So the question is: Is it possible to get both (int arg) and (int) back from my rule that matches a parameter list, or is it alternatively possible to transform the first into the second on the right of the ->?
I’ll add an example source and result tree below
Input:
^(FUN_DEF
^(TYPE_SIMP 'int')
'f'
^(PARAM_LIST
^(PARAM 'int' 'arg')
)
^(BLOCK ...)
)
Result:
^(VAR_DEF
^(TYPE_FUN
^(TYPE_SIMP 'int')
^(PARAM_LIST
^(PARAM 'int')
)
)
'f'
^(FUN
^(TYPE_SIMP 'int')
^(PARAM_LIST
^(PARAM 'int' 'arg')
)
^(BLOCK ...)
)
)
A possibility would be invoke a custom method in your
shortFunctionrule that, given aparamList, would strip all identifiers from them leaving only the types and insert that tree in the proper place:A demo:
file: Fun.g
file: Main.java
If you run the main class, you will see that the input:
produces two identical trees: