Language now supports top level definitions. no more last-expression-is-a-value thing, it's also a def. Host knows to run the special os def
This commit is contained in:
parent
9b8916eb72
commit
31ef279f16
4 changed files with 36 additions and 8 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import type { Token } from './lexer'
|
||||
import type { AST, MatchCase, Pattern } from './ast'
|
||||
import type { AST, MatchCase, Pattern, Definition } from './ast'
|
||||
import { ParseError } from './error'
|
||||
|
||||
export class Parser {
|
||||
|
|
@ -114,8 +114,29 @@ export class Parser {
|
|||
}
|
||||
}
|
||||
|
||||
parse(): AST {
|
||||
return this.parseExpression();
|
||||
parse(): Definition[] {
|
||||
const definitions: Definition[] = [];
|
||||
|
||||
while (this.current().kind !== 'eof') {
|
||||
definitions.push(this.parseDefinition());
|
||||
}
|
||||
|
||||
return definitions;
|
||||
}
|
||||
|
||||
private parseDefinition(): Definition {
|
||||
const nameToken = this.expect('ident');
|
||||
const name = (nameToken as { value: string }).value;
|
||||
|
||||
this.expect('equals');
|
||||
|
||||
const body = this.parseExpression();
|
||||
|
||||
if (this.current().kind !== 'eof') {
|
||||
this.expect('semicolon');
|
||||
}
|
||||
|
||||
return { kind: 'definition', name, body, ...this.getPos(nameToken) };
|
||||
}
|
||||
|
||||
private parseExpression(): AST {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue