parse annos

This commit is contained in:
Theis Pieter Hollebeek 2024-12-11 13:03:01 +01:00
parent 072e44eaa8
commit 4a9d501a71
4 changed files with 36 additions and 28 deletions

View File

@ -39,7 +39,7 @@ export type StmtKind =
params: Param[]; params: Param[];
returnType?: EType; returnType?: EType;
body: Expr; body: Expr;
annos?: Anno[]; anno?: Anno;
vtype?: VType; vtype?: VType;
} }
| { type: "let"; param: Param; value: Expr } | { type: "let"; param: Param; value: Expr }

View File

@ -14,9 +14,12 @@ const lexer = new Lexer(text, reporter);
const parser = new Parser(lexer, reporter); const parser = new Parser(lexer, reporter);
const ast = parser.parseStmts(); const ast = parser.parseStmts();
console.log(JSON.stringify(ast, null, 4));
/*
new Resolver(reporter).resolve(ast); new Resolver(reporter).resolve(ast);
new Checker(reporter).check(ast); new Checker(reporter).check(ast);
// console.log(JSON.stringify(ast, null, 4))
if (reporter.errorOccured()) { if (reporter.errorOccured()) {
console.error("Errors occurred, stopping compilation."); console.error("Errors occurred, stopping compilation.");
@ -31,3 +34,4 @@ const program = lowerer.finish();
console.log(JSON.stringify(program)); console.log(JSON.stringify(program));
await Deno.writeTextFile("out.slgbc", JSON.stringify(program)); await Deno.writeTextFile("out.slgbc", JSON.stringify(program));
*/

View File

@ -205,19 +205,26 @@ export class Parser {
returnType = this.parseEType(); returnType = this.parseEType();
} }
let annos: Anno[] | null = null; let anno: Anno | null = null;
if (this.test("#")) { if (this.test("#")) {
annos = this.parseAnnoArgs(); anno = this.parseAnno();
} }
if (!this.test("{")) { if (!this.test("{")) {
this.report("expected block"); this.report("expected block");
return this.stmt({ type: "error" }, pos); return this.stmt({ type: "error" }, pos);
} }
const body = this.parseBlock(); const body = this.parseBlock();
if (returnType === null) { return this.stmt(
return this.stmt({ type: "fn", ident, params, body }, pos); {
} type: "fn",
return this.stmt({ type: "fn", ident, params, returnType, body }, pos); ident,
params,
returnType: returnType !== null ? returnType : undefined,
body,
anno: anno != null ? anno : undefined,
},
pos,
);
} }
public parseAnnoArgs(): Expr[] { public parseAnnoArgs(): Expr[] {
@ -246,34 +253,26 @@ export class Parser {
return annoArgs; return annoArgs;
} }
public parseAnnos(): Anno[] { public parseAnno(): Anno | null {
const pos = this.pos();
this.step(); this.step();
if (!this.test("[")) { if (!this.test("[")) {
this.report("expected '['"); this.report("expected '['");
return []; return null;
} }
this.step(); this.step();
const annoArgs: Expr[] = [];
if (!this.test(")")) {
if (!this.test("ident")) { if (!this.test("ident")) {
this.report("expected identifier"); this.report("expected identifier");
return []; return null;
} }
annoArgs.push(this.parseExpr()); const ident = this.current().identValue!;
while (this.test(",")) { const values = this.parseAnnoArgs();
this.step(); if (!this.test("]")) {
if (this.test(")")) { this.report("expected ']'");
break; return null;
}
annoArgs.push(this.parseExpr());
}
}
if (!this.test(")")) {
this.report("expected ')'");
return [];
} }
this.step(); this.step();
return annoArgs; return { ident, pos, values };
} }
public parseFnParams(): Param[] { public parseFnParams(): Param[] {

5
examples/annos.slg Normal file
View File

@ -0,0 +1,5 @@
fn print(msg: string) #[builtin(print)] {}
fn main() {
print("hello world!");
}