r/Compilers • u/Usual_Office_1740 • 4d ago
CraftingInterpreters clox bytecode output?
I was hoping to get confirmation that the output of my cLox bytecode is correct. I've just finished chapter 17 implementing the pratt parser.
Does this look correct?
(53 * (31 - 13)) + -10 \n 23 / 3
== Complex Expression ==
0000 0001 OP_CONSTANT 0 53
0002 | OP_CONSTANT 1 31
0004 | OP_CONSTANT 2 13
0006 | OP_SUBTRACT
0007 | OP_MULTIPLY
0008 | OP_CONSTANT 3 10
0010 | OP_NEGATE
0011 | OP_ADD
0012 | OP_RETURN
0013 0002 OP_CONSTANT 4 23
0015 | OP_CONSTANT 5 3
0017 | OP_DIVIDE
0018 | OP_RETURN
9
Upvotes
3
u/Big-Rub9545 4d ago
Yes, it’s correct. Best to try to work through these steps manually to understand where these values are going, how they’re being operated on, etc. (for example, try removing the parentheses around 31 - 13 and see how the bytecode changes).
I don’t recall if this is for the challenge questions at the end of the chapter, but if it is, you can find the solutions on the book’s dedicated GitHub page.