How is eval implemented?

Viewed 40

I was wondering .. how is eval() being implemented in programming languages?

I mean ... when you pass a = 3 to an interpreter.

It will tokenize input as Token(IDENTIFIER, 'a'), Token(EQUAL, '='), Token(NUMBER, '3')

Than create VariableAssignment node in an AST and evaluator will eventually save that variable to memory.

But eval is different, since it holds the code to be executed as an argument and still doesn't brake continuity of evaluation.

That means:

a = 3
eval("a += 6")
print(a)

Prints correct value of 9.

I was thinking, that the interpreter will spawn the interpreter process itself, evaluate the code with access to parent process' memory and than return result of evaluation back to the caller. But that doesn't seem as a clean solution to me.

0 Answers
Related