- The stack we talked about in the last post is to hold intermediary values generated by our interpreter, e.g.:
fun echo(n): print n; return n; print echo(echo(1) + echo(2)) + (echo(4) + echo(5));
While our interpreter is computing echo(2)
, it would need some space in memory to hold the output from echo(1)
-- That's where the stack comes in. Similarly, it would need to store the output of echo(1) + echo(2)
, while it computes echo(4) + echo(5)
. These "produced" values are all pushed in a stack.
- After implementing the stack, noticed that the order of execution of opcodes is not correct in the last output:
== Test Chunk == 0000 122 OP_CONSANT 0 '1.2' 0002 123 OP_CONSANT 1 '456' 0004 124 OP_RETURN 0001 | OP_CONSANT 0 '1.2' 1.2 0003 | OP_RETURN 456 0005 0 OP_CONSANT 0 '1.2'
The chunk.code
should look like this:
OP_CONSTANT 0 // Address of 1.2 in chunk.values OP_CONSTANT 1 // Address of 456 in chunk.values OP_RETURN
and chunk.values
:
1.2 456
Now, notice that OP_RETURN
was executed before the last OP_CONSTANT
. Still debugging why.
Top comments (0)