parser stuff

This commit is contained in:
Mikkel Kongsted 2024-11-20 15:41:20 +01:00
parent 3d89031748
commit d3f27a7101
4 changed files with 64 additions and 51 deletions

View File

@ -25,6 +25,16 @@ export class Lexer {
this.step(); this.step();
} }
switch (value) { switch (value) {
case "break":
return this.token("break", pos);
case "return":
return this.token("return", pos);
case "let":
return this.token("let", pos);
case "fn":
return this.token("fn", pos);
case "loop":
return this.token("loop", pos);
case "if": case "if":
return this.token("if", pos); return this.token("if", pos);
case "else": case "else":

View File

@ -25,6 +25,14 @@ export class Parser {
private report(msg: string, pos = this.pos()) { private report(msg: string, pos = this.pos()) {
console.log(`Parser: ${msg} at ${pos.line}:${pos.col}`); console.log(`Parser: ${msg} at ${pos.line}:${pos.col}`);
class ReportNotAnError extends Error { constructor() { super("ReportNotAnError"); } }
try {
throw new ReportNotAnError();
} catch (error) {
if (!(error instanceof ReportNotAnError))
throw error;
console.log(error)
}
} }
private stmt(kind: StmtKind, pos: Pos): Stmt { private stmt(kind: StmtKind, pos: Pos): Stmt {
@ -65,7 +73,7 @@ export class Parser {
private eatSemicolon() { private eatSemicolon() {
if (!this.test(";")) { if (!this.test(";")) {
this.report("expected ';'"); this.report(`expected ';', got '${this.currentToken?.type ?? "eof"}'`);
return; return;
} }
this.step(); this.step();
@ -285,20 +293,23 @@ export class Parser {
const subject = this.parsePrefix(); const subject = this.parsePrefix();
return this.expr({ type: "unary", unaryType: "not", subject }, pos); return this.expr({ type: "unary", unaryType: "not", subject }, pos);
} }
["+", "*", "==", "-", "/", "!=", "<", ">", "<=", ">=", "or", "and"].forEach((binaryType) => { for (const binaryType of ["+", "*", "==", "-", "/", "!=", "<", ">", "<=", ">=", "or", "and"]) {
this.parseBinary(binaryType as BinaryType, pos) const subject = this.parseBinary(binaryType as BinaryType, pos)
if (subject !== null) {
}) return subject
}
}
return this.parsePostfix(); return this.parsePostfix();
} }
public parseBinary(binaryType: BinaryType, pos: Pos) { public parseBinary(binaryType: BinaryType, pos: Pos): Expr | null {
if (this.test(binaryType)) { if (this.test(binaryType)) {
this.step(); this.step();
const left = this.parsePrefix(); const left = this.parsePrefix();
const right = this.parsePrefix(); const right = this.parsePrefix();
return this.expr({ type: "binary", binaryType, left, right }, pos); return this.expr({ type: "binary", binaryType, left, right }, pos);
} }
return null
} }
public parsePostfix(): Expr { public parsePostfix(): Expr {

View File

@ -1,30 +1,29 @@
fn sum(a, b) {
fn add(a, b) { + 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;
// //
// add(2,3); // -> 5 // if >= i 10 {
// break;
// }
// //
// let a = "Hello"; // i = i + 1;
// }
// 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

@ -5,20 +5,13 @@ import { Parser } from "./Parser.ts";
const text = await Deno.readTextFile("example.slg"); const text = await Deno.readTextFile("example.slg");
const lexer = new Lexer(text); const lexer = new Lexer(text);
console.log("type\tindex\tline:col");
let current = lexer.next(); // while (token !== null) {
while (current) { // const value = token.identValue ?? token.intValue ?? token.stringValue ?? "";
console.log( // console.log(`${token.type}\t${value}`)
`${ // token = lexer.next();
current.identValue ?? current.type // }
}\t${current.pos.index}\t${current.pos.line}:${current.pos.col}`,
); const parser = new Parser(lexer)
current = lexer.next(); const result = parser.parseStmts()
} console.log(JSON.stringify(result, null, 4))
const pos = lexer.currentPos();
console.log(`eof\t${pos.index}\t${pos.line}:${pos.col}`);
//const parser = new Parser(lexer);
//while (!parser.done()) {
// const result = parser.parseExpr();
// console.log(result);
//}