munificent / craftinginterpreters

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

Q: wren-like implementation of arrays (and dicts)? #1015

Closed gvwilson closed 2 years ago

gvwilson commented 2 years ago

Has anyone added arrays and dicts to Lox? If so, did you do a native implementation, or did you add List and Dict as classes and get the compiler to initialize instances of them?

degustaf commented 2 years ago

I did a native implementation.

Honestly, I can't think of a way to implement arrays directly in Lox. Once you have arrays, you could implement dicts in lox the same way clox does in c.

gvwilson commented 2 years ago

I'd be very grateful for a pointer if you can share the code - thanks in advance.

degustaf commented 2 years ago

I've made a number of changes, but it's base is clox: https://github.com/degustaf/xan

Basically, it's just a wrapper around the ValueArray that clox already has.

gvwilson commented 2 years ago

Thanks very much - I'll have a look tonight. From what I can tell, Wren implements arrays (and dicts) by having the compiler insert instructions to define a few built-in classes at the start of every program, then invoking the constructors of those classes in special cases like [1, 2, 3]. I'm hoping something like that will work for Lox so that people will be able to derive their own classes from List and Dict.