Hi,
Is there a way to retrieve the built-in operators in Nim? Perhaps by passing its string/char counterpart would be best. An example would be passing in '+' and getting the plus operator back.
Something similar in Python would be
import operator as op
a = op.add
I am trying to write an interpreter and this will make things a lot easier. Thanks.
The builtin operators are often mapped to CPU instructions and CPU instructions have no address. But with templates and anon procs it's easy enough to do what you need:
template makeBuiltin(op, T): untyped =
(proc (a, b: T): T {.nimcall.} = op(a, b))
let addInstr = makeBuiltin(`+`, int)
echo addInstr(3, 4)
That said, for interpreters simply forget how you would do it in Python and use an enum type and a case statement.
5 ways to write an interpreter loop in Nim: https://github.com/status-im/nimbus-eth1/wiki/Interpreter-optimization-resources#nim-implementation-benchmark
Do NOT use strings for interpreters, that would be a sin.