:: krowemoh

Saturday | 19 APR 2025
Posts Links Other About Now

previous
next

Another Run at Crafting Interpreters

2025-04-16

You would think I wouldn't need a 3rd go around to write my own language but you would be wrong. Something about it ultimately isn't clicking and I can't do it from scratch properly.

This time I'm going through it in the plans of using C so hopefully I learn a bit more. It is a testament to the book that it is that readable over and over again and that the code is so well written that I learn something each time.

Hopefully this is the last time I need to read it though and I can start building an intuition.

Chapter 1

The first chapter of the bytecode section is the idea of chunks. A chunk is code that has already been transformed into a byte array of op codes and constants. This means that I already have an array of bytes to go through. This chapter lays the groundwork for that. I think calling it a chunk is tripping me up and the other thing is where we're starting.

Starting at the level where we already have bytecode is a bit disorienting.

For example, this chapter is really starting with the following:

constants = [1.2]
code = [OP_CONSTANT,0,OP_RETURN]

There is a constants array where constants are put into and then the constant position is the operand of the OP_CONSTANT.

The goal is basically to go through the code array and process it. This chapter ends with us building a dissasembler that goes through the code array and prints out each opcode and its operands. This is also the essence of the VM. The vm will loop through and do stuff based on the opcodes.

Chapter 2

This chapter has the VM getting created. The VM is ultimately just a stack and an instruction pointer aimed inside the chunk. The chunk is added to the vm and the vm kicks off the run function.

The run function loops on the code array and begins processing the opcodes.

For example in the above example, the vm will see OP_CONSTANT and then take the next byte and use that to get the constant from the constants array. It will add this constant to the stack.

The OP_RETURN will then get processed. In this chapter, all it does is print out the last value on the stack.

This is the essence of the VM step but it requires a bit of set up.

Adding an opcode involves adding it to the chunk.h file and then in vm.c we actually do the processing. In theory this is quite simple but it's still a bit disorienting because I don't see where the code is getting transformed.

The next chapter being about scanning should come in handy. I really want to see where chunks get formed.

Chapter 3

I lost the thread here as this chapter is painfully dull. As much as I want to learn scanning, it is terribly boring.

I think doing chapter 4 and then I'll write a little guide to write a base language of just getting PRINT 24 to compile. This will require doing quite a bit of work to see what I can distill down.

The first step is to obviously scan the line create tokens for the PRINT and constant. The next step would be to go through the tokens and emit bytes.

Once the byte code is generated, then we can run then process the bytes and do things based off it.

The extension to this would be to handle strings. Then to add arithmetic. And so on and so forth.

Chapter 4