Add lists

This introduces:

* new syntax for list literals, put comma-separated values between
  braces for your new list
* new syntax for indexing, do `foo[index]` to get the value in `foo` at
  `index`. Lists also allow negative indices too. Any type that wants to
  be indexed can include their own __index__ function as well.
* new VM instruction, BuildList. List literals were a lot easier to
  implement using this rather than creating a new list, creating a
  temporary stack value, and then duplicating + pushing to that
  temporary value over and over.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-09-30 16:33:58 -07:00
parent 0a21f01ea7
commit dab474a037
9 changed files with 403 additions and 17 deletions

View File

@@ -184,6 +184,21 @@ pub fn init_types() {
Obj {
//__call__ => BuiltinFunction::create("__call__",
},
List {
// Conversion methods
to_repr => BuiltinFunction::create("to_repr", List::to_repr, 1),
// Constructor
__call__ => BuiltinFunction::create("__call__", List::do_call, 2),
__init__ => BuiltinFunction::create("__init__", List::init, 2),
// Operators
__index__ => BuiltinFunction::create("__index__", List::index, 2),
// Methods
push => BuiltinFunction::create("push", List::push, 2),
pop => BuiltinFunction::create("pop", List::pop, 1),
},
Str {
// Conversion methods
to_str => BuiltinFunction::create("to_str", Str::to_str, 1),
@@ -198,7 +213,9 @@ pub fn init_types() {
// Operators
__add__ => BuiltinFunction::create("__add__", Str::add, 2),
__mul__ => BuiltinFunction::create("__mul__", Str::mul, 2),
// .lower, .upper, .slice, etc
// Methods
// TODO Str methods - .lower, .upper, .slice, etc
},
Int {
// Conversion methods