Master the fundamental concepts of build a bytecode vm through this focused micro-challenge.
Functions need a call stack separate from the operand stack. Each invocation pushes a frame storing the return address, locals, and a link to the caller's frame. CPython's `PyFrameObject` and the JVM's per-method frame are production versions of what you build here.
Recursion works because each call gets fresh locals. Python's default recursion limit of 1000 frames exists because this stack is finite.
For example, `factorial(5)` nests five frames, each with its own `n` local, before unwinding via `RETURN`.
Frame layout:
Size each frame's local array to the function's needs and zero it on entry. Stale locals from a previous invocation are a subtle bug that only shows up when callers reuse overlapping stack slots without clearing state.
This exercise asks you to add `CALL` and `RETURN` with a `CallFrame` struct. You will implement recursive factorial in bytecode to prove frames push and pop correctly under nested calls.
Add function call support to your VM in C.
Requirements:
Test:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github