Whoooop!!
I've added arrays to Lox! I'm so chuffed! And the code is (fairly!) clean, so it feels right this time.
I added tokens for '[' and ']', and hooked in new array
and index
functions as prefix and infix operators on '['.
array
uses (a slightly modified) getArguments
to parse a list of ,
seperated expressions onto the stack, and then calls OP_ARRAY
with the count/array length to ask the runtime to create an ObjArray
.
ObjArray
is a tissue thin wrapper around ValueArray
.
index
compiles an expression and consumes the expected ']'. Then it calls either OP_GET_INDEX
or OP_SET_INDEX
depending on if we're in an assignment context or not..
I'm not done with arrays, I can only create fixed size arrays at compile time. I want dynamic arrays (at least push and pop), and to be able to create an empty (nil filled) array with the length known at runtime.
Still, a good start!