expr
expr copied to clipboard
Not finding the right Operator
Hi!
When I am trying to patch a Node, let's say from a Integer Node to a Function Node like the following:
if n, ok := (*node).(*ast.IntegerNode); ok { // Replace the int node by wrapping it in a Value ast.Patch(node, &ast.FunctionNode{ Name: "NewStructS", Arguments: []ast.Node{&ast.FloatNode{Value: float64(n.Value)}}, Fast: false, }) }
The end result is that I am creating a new instance of a custom struct 'S'.
Now, when I am trying to run the following expression 'var1 > 12', and that var1 is an instance of S, and 12 is being patched and replaced by another instance of S (calling the 'NewStructS' function), I've got an error of operation ' not defined for S > S', even though my environment has a function of type
func (e MyEnv) GreaterThan(a, b S) bool {...}, and that the operator '>' is added with expr.Operator(">", "GreaterThan").
Note that if I am running another expression with two instances of type S, like var1 > var2 (so 2 instances of S), the operator is found.
Am I missing something? How can I replace any Node with a custom Struct, so that all my operators get registered and called?
Thanks :)
Hi,
MAybe there some problem with the steps applied. If operator override is first, patch is second -> there is no way to detect it.
Hi @antonmedv
I've found a workaround. Initially, the Patching occurred within the Exit callback function
Exit(node *ast.Node)
I've moved it in
Enter(node *ast.Node)
And since, I had an error 'Call using S as type int' - (looks like the function node created successfully the new node, but the operator function is still registered as (S, int)) - I've replaced it with (S, interface{}) and even though is not a signature with (S, S) - it worked.
Let me know if you have any other idea or if I miss something regarding the order of operations.
Thanks