I'm writing a parser for a custom language in go. My problem is that each node on the syntax tree is different based on it's type (be it an if
or a variableDef
or a funcDef
) and thus has different values, but I need to have pointers that can point at any of them, to give it treeish functionality.
Someone suggested having an interface
, with something like a describe
method (implementation-dependent) that described the node, but then the problem is that it'll return different things based on the node that is it. (A funcDef
would give args []variableDef
and body []node
, but an if would give cond bin
and body []node
, and a variable def would have type typ
and name string
)
I originally wrote this in JS and used the flexibility of JSON to my advantage. I have heard that Go has built-in JSON support but I don't think i want to go down that route.