Adding spread operator. starting to build a stdlib. omg

This commit is contained in:
Dustin Swan 2026-02-02 21:20:39 -07:00
parent 216fe6bd30
commit 9edee10508
No known key found for this signature in database
GPG key ID: 30D46587E2100467
7 changed files with 102 additions and 14 deletions

View file

@ -181,14 +181,29 @@ export class Parser {
this.advance();
const elements: Pattern[] = [];
let first = true;
let spreadName: string | null = null;
while (this.current().kind !== 'close-bracket') {
if (!first) this.expect('comma');
first = false;
// Spread
if (this.current().kind === 'dot-dot-dot') {
this.advance();
const nameToken = this.expect('ident');
spreadName = (nameToken as { value: string }).value;
break;
}
elements.push(this.parsePattern());
}
this.expect('close-bracket');
if (spreadName !== null) {
return { kind: 'list-spread', head: elements, spread: spreadName };
}
return { kind: 'list', elements };
}
@ -356,7 +371,7 @@ export class Parser {
if (token.kind === 'open-bracket') {
this.advance();
const items: AST[] = [];
const items: (AST | { spread: AST })[] = [];
let first = true;
while (this.current().kind !== 'close-bracket') {
@ -365,7 +380,14 @@ export class Parser {
}
first = false;
items.push(this.parseExpression());
// Spread
if (this.current().kind === 'dot-dot-dot') {
this.advance();
const expr = this.parseExpression();
items.push({ spread: expr })
} else {
items.push(this.parseExpression());
}
}
this.expect('close-bracket');