3

I am in the process of creating a small language that is compiled to bytecode and run on a custom VM, the architecture of which has largely been influenced by what I've read about Python and Lua. There are two stacks - a data stack that stores function arguments, local variables and temporary values, and a frame stack that contains one entry per active function call. Each entry on the frame stack contains information such as the current function, instruction pointer (indexes into the bytecode array for the current function) and a base pointer (indexes into the data stack - marks where the function's args/locals begin).

Where I've become unstuck is implementing a REPL, or more specifically, the implementation of eval(). The idea so far has been to continuously evaluate user input within the same stack frame - but I can't see a clean way to allow new local variables to be created inside eval(). Because temporary data is always above locals on the stack (the stack grows upwards) the only approach I've been able to think of is to somehow notice that new locals have been created by eval() and then use some hackery to rearrange the stack - but this creates problems in the general case. For example, if there was a recursive function that conditionally used eval() I would need to walk the frame stack and possibly adjust the data stack for each frame.

Is my VM capable of supporting a sensible implementation of eval()? If yes, is the approach outlined above sensible? If no, what architectural changes are required?

jaz303
  • 1,136
  • 9
  • 11

0 Answers0