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

@@ -11,8 +11,10 @@ GENERATE = [
"Unary -> op: Token, expr: ExprP",
"Call -> expr: ExprP, args: Vec<ExprP>, rparen: Token",
"Get -> expr: ExprP, name: Token",
"Index -> expr: ExprP, index: ExprP, rbracket: Token",
"Primary -> token: Token",
"Function -> lparen: Token, params: Vec<(Token <COMMA> Option<ExprP>)>, return_type: Option<ExprP>, body: Vec<StmtP>, rbrace: Token",
"List -> lbracket: Token, exprs: Vec<ExprP>, rbracket: Token",
],
),
(