parser stuff

This commit is contained in:
Mikkel Kongsted 2024-11-18 10:21:30 +01:00
parent b96dc867ab
commit 35f2574957
4 changed files with 39 additions and 9 deletions

30
example-no-types.slg Normal file
View File

@ -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;
// }

View File

@ -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)
}

View File

@ -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())

View File

@ -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)
}