From 4a9d501a71b1b82fe69f13ea7d4155fc7806b673 Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Wed, 11 Dec 2024 13:03:01 +0100 Subject: [PATCH] parse annos --- compiler/ast.ts | 2 +- compiler/main.ts | 6 +++++- compiler/parser.ts | 51 +++++++++++++++++++++++----------------------- examples/annos.slg | 5 +++++ 4 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 examples/annos.slg diff --git a/compiler/ast.ts b/compiler/ast.ts index 810c083..cf92922 100644 --- a/compiler/ast.ts +++ b/compiler/ast.ts @@ -39,7 +39,7 @@ export type StmtKind = params: Param[]; returnType?: EType; body: Expr; - annos?: Anno[]; + anno?: Anno; vtype?: VType; } | { type: "let"; param: Param; value: Expr } diff --git a/compiler/main.ts b/compiler/main.ts index 7ee322e..1b7af94 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -14,9 +14,12 @@ const lexer = new Lexer(text, reporter); const parser = new Parser(lexer, reporter); const ast = parser.parseStmts(); + +console.log(JSON.stringify(ast, null, 4)); + +/* new Resolver(reporter).resolve(ast); new Checker(reporter).check(ast); -// console.log(JSON.stringify(ast, null, 4)) if (reporter.errorOccured()) { console.error("Errors occurred, stopping compilation."); @@ -31,3 +34,4 @@ const program = lowerer.finish(); console.log(JSON.stringify(program)); await Deno.writeTextFile("out.slgbc", JSON.stringify(program)); +*/ diff --git a/compiler/parser.ts b/compiler/parser.ts index 17dfd44..6cccd47 100644 --- a/compiler/parser.ts +++ b/compiler/parser.ts @@ -205,19 +205,26 @@ export class Parser { returnType = this.parseEType(); } - let annos: Anno[] | null = null; + let anno: Anno | null = null; if (this.test("#")) { - annos = this.parseAnnoArgs(); + anno = this.parseAnno(); } if (!this.test("{")) { this.report("expected block"); return this.stmt({ type: "error" }, pos); } const body = this.parseBlock(); - if (returnType === null) { - return this.stmt({ type: "fn", ident, params, body }, pos); - } - return this.stmt({ type: "fn", ident, params, returnType, body }, pos); + return this.stmt( + { + type: "fn", + ident, + params, + returnType: returnType !== null ? returnType : undefined, + body, + anno: anno != null ? anno : undefined, + }, + pos, + ); } public parseAnnoArgs(): Expr[] { @@ -246,34 +253,26 @@ export class Parser { return annoArgs; } - public parseAnnos(): Anno[] { + public parseAnno(): Anno | null { + const pos = this.pos(); this.step(); if (!this.test("[")) { this.report("expected '['"); - return []; + return null; } this.step(); - const annoArgs: Expr[] = []; - if (!this.test(")")) { - if (!this.test("ident")) { - this.report("expected identifier"); - return []; - } - annoArgs.push(this.parseExpr()); - while (this.test(",")) { - this.step(); - if (this.test(")")) { - break; - } - annoArgs.push(this.parseExpr()); - } + if (!this.test("ident")) { + this.report("expected identifier"); + return null; } - if (!this.test(")")) { - this.report("expected ')'"); - return []; + const ident = this.current().identValue!; + const values = this.parseAnnoArgs(); + if (!this.test("]")) { + this.report("expected ']'"); + return null; } this.step(); - return annoArgs; + return { ident, pos, values }; } public parseFnParams(): Param[] { diff --git a/examples/annos.slg b/examples/annos.slg new file mode 100644 index 0000000..cf182cf --- /dev/null +++ b/examples/annos.slg @@ -0,0 +1,5 @@ +fn print(msg: string) #[builtin(print)] {} + +fn main() { + print("hello world!"); +}