craftinginterpreters
craftinginterpreters copied to clipboard
Answers for challenges in Chapter 22 is missing!
The answers section under notes/answers is oddly missing. This should be added.
Yeah, for some of them I just never got around to writing the answers.
Closing this because I think the answers to this chapter are fairly straightforward. :)
@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.
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.
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.
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.
Sorry if it might seem a stupid question but how can implement const variables?