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) {
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" } },

View File

@ -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("[")) {

View File

@ -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 {
if >= i 10 {
break;
}
i += 1;
i = + i 1;
}

View File

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