fix checker

This commit is contained in:
Mikkel Kongsted 2024-12-09 13:57:48 +01:00
parent 9e6e92b520
commit 6ab45ffbed
4 changed files with 35 additions and 16 deletions

View File

@ -98,7 +98,6 @@ export class Checker {
if (param.etype === undefined) { if (param.etype === undefined) {
this.report("parameter types must be defined", param.pos); this.report("parameter types must be defined", param.pos);
stmt.kind.vtype = { type: "error" }; stmt.kind.vtype = { type: "error" };
return;
} }
const vtype = this.checkEType(param.etype!); const vtype = this.checkEType(param.etype!);
param.vtype = vtype; param.vtype = vtype;
@ -129,8 +128,8 @@ export class Checker {
if (!vtypesEqual(value, paramVtype)) { if (!vtypesEqual(value, paramVtype)) {
this.report( this.report(
`incompatible value type` + `incompatible value type` +
`, got '${value}'` + `, got '${vtypeToString(value)}'` +
`, expected '${paramVtype}'`, `, expected '${vtypeToString(paramVtype)}'`,
pos, pos,
); );
return; return;
@ -572,7 +571,20 @@ export class Checker {
} }
private report(msg: string, pos: Pos) { 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 // arithmetic
{ binaryType: "+", operand: { type: "int" } }, { binaryType: "+", operand: { type: "int" } },
{ binaryType: "+", operand: { type: "string" } },
{ binaryType: "-", operand: { type: "int" } }, { binaryType: "-", operand: { type: "int" } },
{ binaryType: "*", operand: { type: "int" } }, { binaryType: "*", operand: { type: "int" } },
{ binaryType: "/", operand: { type: "int" } }, { binaryType: "/", operand: { type: "int" } },

View File

@ -257,6 +257,7 @@ export class Parser {
const ident = this.current().identValue!; const ident = this.current().identValue!;
this.step(); this.step();
if (this.test(":")) { if (this.test(":")) {
this.step();
const etype = this.parseEType(); const etype = this.parseEType();
return { ok: true, value: { ident, etype, pos } }; return { ok: true, value: { ident, etype, pos } };
} }
@ -500,6 +501,7 @@ export class Parser {
const pos = this.pos(); const pos = this.pos();
if (this.test("ident")) { if (this.test("ident")) {
const ident = this.current().identValue!; const ident = this.current().identValue!;
this.step()
return this.etype({ type: "ident", value: ident }, pos); return this.etype({ type: "ident", value: ident }, pos);
} }
if (this.test("[")) { if (this.test("[")) {

View File

@ -1,18 +1,20 @@
fn println(str: string) {
fn add(a: int, b: int) -> int {
a + b
} }
add(2,3); // -> 5 fn sum(a: int, b: int) -> int {
+ a b
}
sum(2,3); // -> 5
let a: string = "Hello"; let a: string = "Hello";
let b = "world"; let b = "world";
println(a + " " + b + "!"); // -> "Hello world!" println(+ + + a " " b "!"); // -> "Hello world!"
if a == b { if == a b {
println("whaaaat"); println("whaaaat");
} }
else { else {
@ -22,9 +24,9 @@ else {
loop { loop {
let i = 0; let i = 0;
if i >= 10 { if >= i 10 {
break; break;
} }
i += 1; i = + i 1;
} }

View File

@ -1,8 +1,9 @@
import { Checker } from "./Checker.ts";
import { Lexer } from "./Lexer.ts"; import { Lexer } from "./Lexer.ts";
import { Parser } from "./Parser.ts"; import { Parser } from "./Parser.ts";
import { Resolver } from "./Syms.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 text = await Deno.readTextFile("example.slg");
const lexer = new Lexer(text); const lexer = new Lexer(text);
@ -16,4 +17,5 @@ const lexer = new Lexer(text);
const parser = new Parser(lexer) const parser = new Parser(lexer)
const ast = parser.parseStmts() const ast = parser.parseStmts()
new Resolver().resolve(ast) new Resolver().resolve(ast)
console.log(JSON.stringify(ast, null, 4)) new Checker().check(ast)
// console.log(JSON.stringify(ast, null, 4))