craftinginterpreters icon indicating copy to clipboard operation
craftinginterpreters copied to clipboard

Answers for challenges in Chapter 22 is missing!

Open katjonathan opened this issue 4 years ago • 6 comments

The answers section under notes/answers is oddly missing. This should be added.

katjonathan avatar Jun 20 '20 23:06 katjonathan

Yeah, for some of them I just never got around to writing the answers.

munificent avatar Jun 23 '20 01:06 munificent

Closing this because I think the answers to this chapter are fairly straightforward. :)

munificent avatar Jun 30 '20 14:06 munificent

@munificent I second this request, even straightforward answers from your perspective can be a lot of help for less experienced language hackers. If you could drop a hint for challenge n1, I would appreciate it.

lofcz avatar Nov 29 '21 04:11 lofcz

My questions revolve around #4. The obvious answer is to expand the data on the opcode past 1 byte. I think two would work. To paraphrase Bill Gates, 64k locals ought to be enough for anybody. But then it seems to me I have to do some left shifting and right shifting to get the data in and out. And if I only add one more byte, then this opcode becomes 3 bytes total, which means I'm not on a word boundary, and I wonder if that's something that I need to worry about.

Feels like I'm over-complicating the situation, so would appreciate thoughts.

chrisjbreisch avatar May 31 '22 00:05 chrisjbreisch

Actually, for #4, I ended up making _LONG versions of all the variable opcodes, that were 3 byte indexes instead of 1, and then a switch to the _LONG versions if the index exceeded 255. This was similar to an earlier challenge regarding extending the Constants past 1 byte as well. Which was applicable here, since the Global names are stored as Constants anyway. There were pretty extensive changes here in compiler.c, chunk.h, vm.c, and debug.c to get all of those in place.

However, the end solution seems pretty solid. I'm not limited to 256 of anything, and am not wasting bytes for small numbers of variables.

chrisjbreisch avatar May 31 '22 14:05 chrisjbreisch

I ended up having problems with my implementation later on in the book. For now, I have backed out those changes, but will re-visit. It's important to me that I don't limit myself to 256 of anything, although even another byte seems excessive for most things. Will I really need 65,536 local variables? Or 65,536 positions on the stack? If my stack reaches that kind of depth, my user probably messed something up.

chrisjbreisch avatar Jul 04 '22 18:07 chrisjbreisch

Sorry if it might seem a stupid question but how can implement const variables?

davidoskiii avatar Feb 26 '24 15:02 davidoskiii