From 6ab45ffbed2ba3c9a3fbba37f388c8ffe871a235 Mon Sep 17 00:00:00 2001 From: Mikkel Kongsted Date: Mon, 9 Dec 2024 13:57:48 +0100 Subject: [PATCH] fix checker --- compiler/Checker.ts | 21 +++++++++++++++++---- compiler/Parser.ts | 2 ++ compiler/example.slg | 22 ++++++++++++---------- compiler/main.ts | 6 ++++-- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/compiler/Checker.ts b/compiler/Checker.ts index a264257..f004723 100644 --- a/compiler/Checker.ts +++ b/compiler/Checker.ts @@ -98,7 +98,6 @@ export class Checker { if (param.etype === undefined) { this.report("parameter types must be defined", param.pos); stmt.kind.vtype = { type: "error" }; - return; } const vtype = this.checkEType(param.etype!); param.vtype = vtype; @@ -129,8 +128,8 @@ export class Checker { if (!vtypesEqual(value, paramVtype)) { this.report( `incompatible value type` + - `, got '${value}'` + - `, expected '${paramVtype}'`, + `, got '${vtypeToString(value)}'` + + `, expected '${vtypeToString(paramVtype)}'`, pos, ); return; @@ -572,7 +571,20 @@ export class Checker { } private report(msg: string, pos: Pos) { - console.error(`${msg} at ${pos.line}:${pos.col}`); + console.error(`Checker: ${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); + } } } @@ -591,6 +603,7 @@ const simpleBinaryOperations: { }[] = [ // arithmetic { binaryType: "+", operand: { type: "int" } }, + { binaryType: "+", operand: { type: "string" } }, { binaryType: "-", operand: { type: "int" } }, { binaryType: "*", operand: { type: "int" } }, { binaryType: "/", operand: { type: "int" } }, diff --git a/compiler/Parser.ts b/compiler/Parser.ts index ddf61e1..8c0fbf1 100644 --- a/compiler/Parser.ts +++ b/compiler/Parser.ts @@ -257,6 +257,7 @@ export class Parser { const ident = this.current().identValue!; this.step(); if (this.test(":")) { + this.step(); const etype = this.parseEType(); return { ok: true, value: { ident, etype, pos } }; } @@ -500,6 +501,7 @@ export class Parser { const pos = this.pos(); if (this.test("ident")) { const ident = this.current().identValue!; + this.step() return this.etype({ type: "ident", value: ident }, pos); } if (this.test("[")) { diff --git a/compiler/example.slg b/compiler/example.slg index 4eac52e..aa9fd79 100755 --- a/compiler/example.slg +++ b/compiler/example.slg @@ -1,18 +1,20 @@ - -fn add(a: int, b: int) -> int { - a + b +fn println(str: string) { } -add(2,3); // -> 5 +fn sum(a: int, b: int) -> int { + + a b +} + +sum(2,3); // -> 5 let a: string = "Hello"; - + let b = "world"; -println(a + " " + b + "!"); // -> "Hello world!" +println(+ + + a " " b "!"); // -> "Hello world!" -if a == b { +if == a b { println("whaaaat"); } else { @@ -22,9 +24,9 @@ else { loop { let i = 0; - if i >= 10 { - break; + if >= i 10 { + break; } - i += 1; + i = + i 1; } \ No newline at end of file diff --git a/compiler/main.ts b/compiler/main.ts index 2de6dd6..1183ac6 100644 --- a/compiler/main.ts +++ b/compiler/main.ts @@ -1,8 +1,9 @@ +import { Checker } from "./Checker.ts"; import { Lexer } from "./Lexer.ts"; import { Parser } from "./Parser.ts"; import { Resolver } from "./Syms.ts"; -const text = await Deno.readTextFile("example-no-types.slg"); +const text = await Deno.readTextFile("example.slg"); // const text = await Deno.readTextFile("example.slg"); const lexer = new Lexer(text); @@ -16,4 +17,5 @@ const lexer = new Lexer(text); const parser = new Parser(lexer) const ast = parser.parseStmts() new Resolver().resolve(ast) -console.log(JSON.stringify(ast, null, 4)) +new Checker().check(ast) +// console.log(JSON.stringify(ast, null, 4))