parsing record access and update

This commit is contained in:
Dustin Swan 2026-02-01 01:19:56 -07:00
parent 6e8b5afd3b
commit b6f7e63e49
No known key found for this signature in database
GPG key ID: 30D46587E2100467
3 changed files with 48 additions and 3 deletions

View file

@ -148,16 +148,54 @@ export class Parser {
}
private parseApplication(): AST {
let func = this.parsePrimary();
let func = this.parsePostfix();
while (this.canStartPrimary()) {
const arg = this.parsePrimary();
const arg = this.parsePostfix();
func = { kind: 'apply', func, args: [arg] };
}
return func;
}
private parsePostfix(): AST {
let expr = this.parsePrimary();
while (true) {
if (this.current().kind === 'dot') {
// Record access
this.advance();
const fieldToken = this.expect('ident');
const field = (fieldToken as { value: string }).value;
expr = { kind: 'record-access', record: expr, field };
} else if (this.current().kind === 'open-brace') {
// Record update
this.advance();
const updates: { [key: string]: AST } = {};
let first = true;
while (this.current().kind !== 'close-brace') {
if (!first) {
this.expect('comma');
}
first = false;
const keyToken = this.expect('ident');
const key = (keyToken as { value: string }).value;
this.expect('equals');
updates[key] = this.parseExpression();
}
this.expect('close-brace');
expr = { kind: 'record-update', record: expr, updates }
} else {
break;
}
}
return expr;
}
private parsePrimary(): AST {
const token = this.current();