Daily Log: Day 9 & 10

rj722

Rahul Jha

Posted on March 30, 2021

Daily Log: Day 9 & 10
  • 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));
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

and chunk.values:

1.2
456
Enter fullscreen mode Exit fullscreen mode

Now, notice that OP_RETURN was executed before the last OP_CONSTANT. Still debugging why.

πŸ’– πŸ’ͺ πŸ™… 🚩
rj722
Rahul Jha

Posted on March 30, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Daily Log: Day 9 & 10
100daysofcs Daily Log: Day 9 & 10

March 30, 2021

Implementing the heart of clox's VM
100daysofcs Implementing the heart of clox's VM

March 27, 2021

Day 003: Crafting Interpreters
100daysofcs Day 003: Crafting Interpreters

March 21, 2021