I am developing a new language and I am following a traditional approach: so there is a lexer, a parser, an ast and a virtual machine that executes bytecode.
When the parser encounters an operator like + then it produces an ADD virtual machine instruction and everything works as expected so far.
I would be interested in give a way to end user to extends the language, so for example the user could add an operator++ function in order to create its own post increment operator. I am not sure about the right way to proceed because the parser cannot know in advance all the operators the user could add so I am asking about what is the right approach to follow.
You could do it the Swift way: have operator declarations in your grammar
In Swift it looks like:
This tells the Swift parser that SomeToken will be a left-associative, infix operator, and that in any precedence-based decision, this operator's weight is 150. other operators will have higher, lower, or equal precedence metrics, and that will guide the parser in creating the AST