In my interpreter, code like the following
x=(y+4)*z
echo x
parses and “optimizes” down to four single operations performed by the interpreter, pretty much assembly-like:
add 4 to y
multiply <last operation result> with z
set x to <last operation result>
echo x
-
In modern interpreters (for example: CPython, Ruby, PHP), how simplified are the “opcodes” for which are in end-effect run by the interpreter?
-
Could I achieve better performance when trying to keep the structures and commands for the interpreter more complex and high-level? That would be surely a lot harder, or?
In Python’s case, you can have it tell you the bytecode for a given function with the dis module.
gives you:
Some of that is extraneous (e.g. the LOAD_CONST and RETURN_VALUE at the end are for the implicit
return Noneinfoo()), but Python appears to push y and 4 onto the stack, add, push z, multiply, and write to x. Then it pushes x and prints