munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.82k stars 1.03k forks source link

Suggestion: Add a chapter on building arrays #579

Closed cseidman closed 4 years ago

cseidman commented 4 years ago

It seems like a language without arrays is not quite complete ..!

desoss commented 4 years ago

I know that @munificent has already answered a question like this. Regarding the arrays there's a lot of boilerplate that's not interesting to be read. Moreover, there are some old commits that have this code.

Nevertheless, I'm interested too in the implementation of the arrays. For the sake of completeness, can't you just add them in the code and not in the book?

(@munificent I'm in love with your book! <3)

egordorichev commented 4 years ago

Plus there is a note in the java part about arrays, with full-on implementation :)

jtran commented 4 years ago

See #540.

munificent commented 4 years ago

Yup, I think this is essentially a duplicate of #540. (Though the fact that this comes up does make me question whether I should have included arrays... :( At this point, it's probably too late to add them since it would probably impact a lot of chapters.)

desoss commented 4 years ago

😢

cseidman commented 4 years ago

I'm adding them now to my own code. Don't know if it was all in the spirit of clox, but it seems to work well. I admit I didn't faithfully replicate clox as much as I borrowed some ideas. In fact, I wrote my compiler/VM in Golang ;)

But I think it would have been a good idea to include arrays in clox since I can see how a wrong turn here could make the implementations of arrays a complete disaster. I would have thought it would have been easy to add without messing up previous chapters. All I really had to do was create another OP code that takes an argument containing the expected number of elements, builds the array by popping the x number of elements, and assigns them to the Value object as an ARRAY_OBJ. The ARRAY_OBJ contains the necessary information to reference the array. Most of the building blocks are already there.

FloydATC commented 4 years ago

I'm adding them now to my own code. Don't know if it was all in the spirit of clox, but it seems to work well. I admit I didn't faithfully replicate clox as much as I borrowed some ideas. In fact, I wrote my compiler/VM in Golang ;)

But I think it would have been a good idea to include arrays in clox since I can see how a wrong turn here could make the implementations of arrays a complete disaster. I would have thought it would have been easy to add without messing up previous chapters. All I really had to do was create another OP code that takes an argument containing the expected number of elements, builds the array by popping the x number of elements, and assigns them to the Value object as an ARRAY_OBJ. The ARRAY_OBJ contains the necessary information to reference the array. Most of the building blocks are already there.

Is your code available for peeking anywhere? I have a toy project where I'm using a modified clox as a sort of "backend" script engine that allows me to run the script just a few milliseconds at a time to update OpenGL textures in real time, and arrays are sorely missed for their superior performance over linked lists. It's not so much the OP code bit I'm curious about but rather where you slot the syntax into the language without breaking things like precedence. Ideally, I would want to try implementing Python-like slice syntax for both access and assignment but that's probably well beyond my skills at this point.

About the spirit of clox, I think of it as a sort of Lego playset; the building instructions are just there to help you get started, the real fun begins when you pick it apart, get creative and learn by making mistakes.

jtran commented 4 years ago

@FloydATC, in terms of parsing, literal arrays like [1, 2, 3] should be straight-forward. For parsing indexing as in arr[i], you can treat it almost identically to parsing function calls. Start with that. If you can get that, adding slice syntax shouldn't be bad.

nocturn9x commented 4 years ago

If you want to add slice syntax just add a token for [ and ] and give them PREC_CALL precedence in the precedence table! You can check my implementation where I implemented string slicing (with start:end syntax as well). It's fairly python like, except that there's no step argument and negative indexes aren't supported yet, but the end part of the slice is inclusive rather than exclusive. My implementation (check the nim folder) is https://github.com/nocturn9x/japl