Closed jtran closed 4 years ago
I assume you left constant-time access arrays out of the language for a reason.
Yes. Mostly because implementing them is a decent amount of grunt work, but there isn't anything technically very interesting about them. It's just a couple of native functions and maybe some syntax. An early version of Lox did actually have them, but I removed them because the code to implement them just didn't carry its weight in terms of teaching value.
If I were adding them to Lox, syntactically I would follow JS, Python, Ruby, etc.: Add a [1, 2, 3]
literal syntax and list[1]
syntax for accessing elements.
I walk through a simple implementation of them in one of the challenge answers here: https://github.com/munificent/craftinginterpreters/blob/master/note/answers/chapter13_inheritance/3.md
I didn't see anything in the book about arrays. I assume you left constant-time access arrays out of the language for a reason. My question is, if arrays were omitted purely for length of the book, can you add a challenge to add arrays where array behavior is specified?
You did this for a number of other features. Even if you didn't dictate all the edge cases, a few guides were hinted at. This way, there would basically be an informal spec of what arrays in the language should look like, if someone chose to implement them.
One approach is to follow other languages and add bracket notation as in
array[index] = value;
and make arrays intrinsic. Most languages also add syntax for creating new literals like[1, 2, 3]
. But that's just one way of doing it. Another way is that the syntax is just sugar for calling a method on an object, and there is a native value which is the Array class. Or maybe there is just the class, without new syntax.Basically, I've already implemented an interpreter. I would like to add arrays to make it more useful, but I would like to do it in a way that is still in the spirit of Lox, likely to be (sort of) compatible with other implementations. I've found it to be interesting to look at other people's implementations and see how they've done the same thing in different ways.