From 35f257495703cc1b96e09e2e46f674a59b77dd26 Mon Sep 17 00:00:00 2001 From: Mikkel Kongsted Date: Mon, 18 Nov 2024 10:21:30 +0100 Subject: [PATCH] parser stuff --- example-no-types.slg | 30 ++++++++++++++++++++++++++++++ src/Lexer.ts | 2 +- src/Parser.ts | 4 ++-- src/main.ts | 12 ++++++------ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 example-no-types.slg diff --git a/example-no-types.slg b/example-no-types.slg new file mode 100644 index 0000000..4b002c7 --- /dev/null +++ b/example-no-types.slg @@ -0,0 +1,30 @@ + + +fn add(a, b) { + + a b +} +// +// add(2,3); // -> 5 +// +// let a = "Hello"; +// +// let b = "world"; +// +// println(a + " " + b + "!"); // -> "Hello world!" +// +// if a == b { +// println("whaaaat"); +// } +// else { +// println(":o"); +// } +// +// loop { +// let i = 0; +// +// if i >= 10 { +// break; +// } +// +// i = i + 1; +// } \ No newline at end of file diff --git a/src/Lexer.ts b/src/Lexer.ts index 63fca6c..01d003c 100644 --- a/src/Lexer.ts +++ b/src/Lexer.ts @@ -109,7 +109,7 @@ export class Lexer { if (this.test("/")) { while (!this.done() && !this.test("\n")) this.step(); - return this.token("//", pos) + return this.next() } return this.token("/", pos) } diff --git a/src/Parser.ts b/src/Parser.ts index b4681da..750315c 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -2,7 +2,7 @@ import { Expr, ExprKind, Param, Stmt, StmtKind, BinaryType} from "./ast.ts"; import { Lexer } from "./Lexer.ts"; import { Pos, Token } from "./Token.ts"; -class Parser { +export class Parser { private currentToken: Token | null; private nextNodeId = 0; @@ -11,7 +11,7 @@ class Parser { } private step() { this.currentToken = this.lexer.next() } - private done(): boolean { return this.currentToken == null; } + public done(): boolean { return this.currentToken == null; } private current(): Token { return this.currentToken!; } private pos(): Pos { if (this.done()) diff --git a/src/main.ts b/src/main.ts index 806c992..9d92784 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,13 @@ import { Lexer } from "./Lexer.ts"; import { readFileSync } from 'node:fs'; +import { Parser } from "./Parser.ts"; -const text = readFileSync("example.slg").toString() +const text = readFileSync("example-no-types.slg").toString() const lexer = new Lexer(text); -let token = lexer.next(); -while (token !== null) { - const value = token.identValue ?? token.intValue ?? token.stringValue ?? ""; - console.log(`${token.type}\t${value}`) - token = lexer.next(); +const parser = new Parser(lexer) +while (!parser.done()) { + const result = parser.parseExpr() + console.log(result) }