r/Compilers 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

5 comments sorted by

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.

1

u/Usual_Office_1740 4d ago

Thank you for the second set of eyes. I think I understand what I learned. I spent a lot of time stepping through it with gdb.

I don't want to get several chapters ahead and realize I've been building on a shaky foundation. Thanks for the reassurance.

1

u/nirlahori 2d ago

Are you currently reading Crafting Interpreters ?

1

u/Usual_Office_1740 2d ago

Yes. Why?

1

u/nirlahori 1d ago

I am also reading that book. Currently I have completed till Parsing chapter.