Compare commits
No commits in common. "c8aa3e7c1c1c1fba01a03425a1920ce85569dd95" and "4d7998eb9f642178396853078645ac62c0ade572" have entirely different histories.
c8aa3e7c1c
...
4d7998eb9f
@ -2,37 +2,32 @@ import * as ast from "../ast.ts";
|
||||
import { FileReporter, Loc } from "../diagnostics.ts";
|
||||
import { Ty } from "../ty.ts";
|
||||
import { builtins } from "./builtins.ts";
|
||||
import { Syms } from "./resolve.ts";
|
||||
import { ResolveMap } from "./resolve.ts";
|
||||
|
||||
export class Tys {
|
||||
// export class Tys {
|
||||
// private nodeTys = new Map<number, Ty>();
|
||||
//
|
||||
// expr(expr: ast.Node): Ty {}
|
||||
// }
|
||||
|
||||
export class Checker {
|
||||
private nodeTys = new Map<number, Ty>();
|
||||
private checker: Checker;
|
||||
|
||||
constructor(
|
||||
private syms: Syms,
|
||||
private reporter: FileReporter,
|
||||
) {
|
||||
this.checker = new Checker(this, this.syms, this.reporter);
|
||||
}
|
||||
|
||||
expr(node: ast.Node): Ty {
|
||||
if (this.nodeTys.has(node.id)) {
|
||||
return this.nodeTys.get(node.id)!;
|
||||
}
|
||||
const ty = this.checker.checkExpr(node);
|
||||
this.nodeTys.set(node.id, ty);
|
||||
return ty;
|
||||
}
|
||||
}
|
||||
|
||||
class Checker {
|
||||
constructor(
|
||||
private tys: Tys,
|
||||
private syms: Syms,
|
||||
private resols: ResolveMap,
|
||||
private reporter: FileReporter,
|
||||
) {}
|
||||
|
||||
checkExpr(node: ast.Node): Ty {
|
||||
check(node: ast.Node): Ty {
|
||||
if (this.nodeTys.has(node.id)) {
|
||||
return this.nodeTys.get(node.id)!;
|
||||
}
|
||||
const ty = this.checkNode(node);
|
||||
this.nodeTys.set(node.id, ty);
|
||||
return ty;
|
||||
}
|
||||
|
||||
private checkNode(node: ast.Node): Ty {
|
||||
const k = node.kind;
|
||||
|
||||
if (node.is("FnStmt")) {
|
||||
@ -40,12 +35,12 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("Param")) {
|
||||
const sym = this.syms.get(node);
|
||||
const sym = this.resols.get(node);
|
||||
|
||||
if (sym.tag === "Let") {
|
||||
const exprTy = this.tys.expr(sym.stmt.kind.expr);
|
||||
const exprTy = this.check(sym.stmt.kind.expr);
|
||||
if (node.kind.ty) {
|
||||
const explicitTy = this.tys.expr(node.kind.ty);
|
||||
const explicitTy = this.check(node.kind.ty);
|
||||
this.assertCompatible(
|
||||
exprTy,
|
||||
explicitTy,
|
||||
@ -59,16 +54,16 @@ class Checker {
|
||||
this.error(node.loc, `parameter must have a type`);
|
||||
this.fail();
|
||||
}
|
||||
return this.tys.expr(node.kind.ty);
|
||||
return this.check(node.kind.ty);
|
||||
}
|
||||
|
||||
throw new Error(`'${sym.tag}' not handled`);
|
||||
}
|
||||
|
||||
if (node.is("IdentExpr")) {
|
||||
const sym = this.syms.get(node);
|
||||
const sym = this.resols.get(node);
|
||||
if (sym.tag === "Fn") {
|
||||
return this.tys.expr(sym.stmt);
|
||||
return this.check(sym.stmt);
|
||||
}
|
||||
if (sym.tag === "Bool") {
|
||||
return Ty.Bool;
|
||||
@ -77,10 +72,10 @@ class Checker {
|
||||
return builtins.find((s) => s.id === sym.id)!.ty;
|
||||
}
|
||||
if (sym.tag === "FnParam") {
|
||||
return this.tys.expr(sym.param);
|
||||
return this.check(sym.param);
|
||||
}
|
||||
if (sym.tag === "Let") {
|
||||
return this.tys.expr(sym.param);
|
||||
return this.check(sym.param);
|
||||
}
|
||||
throw new Error(`'${sym.tag}' not handled`);
|
||||
}
|
||||
@ -92,7 +87,7 @@ class Checker {
|
||||
if (node.is("ArrayExpr")) {
|
||||
let ty: Ty | null = null;
|
||||
for (const value of node.kind.values) {
|
||||
const valueTy = this.tys.expr(value);
|
||||
const valueTy = this.check(value);
|
||||
if (ty) {
|
||||
this.assertCompatible(ty, valueTy, value.loc);
|
||||
} else {
|
||||
@ -108,8 +103,8 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("IndexExpr")) {
|
||||
const exprTy = this.tys.expr(node.kind.expr);
|
||||
const argTy = this.tys.expr(node.kind.arg);
|
||||
const exprTy = this.check(node.kind.expr);
|
||||
const argTy = this.check(node.kind.arg);
|
||||
if (
|
||||
(exprTy.is("Array") || exprTy.is("Slice")) &&
|
||||
argTy.compatibleWith(Ty.Int)
|
||||
@ -134,7 +129,7 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("UnaryExpr")) {
|
||||
const exprTy = this.tys.expr(node.kind.expr);
|
||||
const exprTy = this.check(node.kind.expr);
|
||||
if (node.kind.op === "Negate" && exprTy.compatibleWith(Ty.Int)) {
|
||||
return Ty.Int;
|
||||
}
|
||||
@ -160,8 +155,8 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("BinaryExpr")) {
|
||||
const left = this.tys.expr(node.kind.left);
|
||||
const right = this.tys.expr(node.kind.right);
|
||||
const left = this.check(node.kind.left);
|
||||
const right = this.check(node.kind.right);
|
||||
const binaryOp = binaryOpPatterns
|
||||
.find((pat) =>
|
||||
pat.op === node.kind.op &&
|
||||
@ -180,7 +175,7 @@ class Checker {
|
||||
|
||||
if (node.is("RangeExpr")) {
|
||||
for (const operandExpr of [node.kind.begin, node.kind.end]) {
|
||||
const operandTy = operandExpr && this.tys.expr(operandExpr);
|
||||
const operandTy = operandExpr && this.check(operandExpr);
|
||||
if (operandTy && !operandTy.compatibleWith(Ty.Int)) {
|
||||
this.error(
|
||||
operandExpr.loc,
|
||||
@ -206,17 +201,17 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("PtrTy")) {
|
||||
const ty = this.tys.expr(node.kind.ty);
|
||||
const ty = this.check(node.kind.ty);
|
||||
return Ty.create("Ptr", { ty });
|
||||
}
|
||||
if (node.is("PtrMutTy")) {
|
||||
const ty = this.tys.expr(node.kind.ty);
|
||||
const ty = this.check(node.kind.ty);
|
||||
return Ty.create("PtrMut", { ty });
|
||||
}
|
||||
|
||||
if (node.is("ArrayTy")) {
|
||||
const ty = this.tys.expr(node.kind.ty);
|
||||
const lengthTy = this.tys.expr(node.kind.length);
|
||||
const ty = this.check(node.kind.ty);
|
||||
const lengthTy = this.check(node.kind.length);
|
||||
if (!lengthTy.compatibleWith(Ty.Int)) {
|
||||
this.error(
|
||||
node.kind.length.loc,
|
||||
@ -236,7 +231,7 @@ class Checker {
|
||||
}
|
||||
|
||||
if (node.is("SliceTy")) {
|
||||
const ty = this.tys.expr(node.kind.ty);
|
||||
const ty = this.check(node.kind.ty);
|
||||
return Ty.create("Slice", { ty });
|
||||
}
|
||||
|
||||
@ -246,14 +241,14 @@ class Checker {
|
||||
private checkFnStmt(stmt: ast.NodeWithKind<"FnStmt">): Ty {
|
||||
const k = stmt.kind;
|
||||
|
||||
const params = k.params.map((param) => this.tys.expr(param));
|
||||
const retTy = k.retTy ? this.tys.expr(k.retTy) : Ty.Void;
|
||||
const params = k.params.map((param) => this.check(param));
|
||||
const retTy = k.retTy ? this.check(k.retTy) : Ty.Void;
|
||||
|
||||
k.body.visit({
|
||||
visit: (node) => {
|
||||
if (node.is("ReturnStmt")) {
|
||||
const ty = node.kind.expr
|
||||
? this.tys.expr(node.kind.expr)
|
||||
? this.check(node.kind.expr)
|
||||
: Ty.Void;
|
||||
if (!ty.compatibleWith(retTy)) {
|
||||
this.error(
|
||||
@ -275,7 +270,7 @@ class Checker {
|
||||
}
|
||||
|
||||
private checkCall(node: ast.NodeWithKind<"CallExpr">): Ty {
|
||||
const calleeTy = this.tys.expr(node.kind.expr);
|
||||
const calleeTy = this.check(node.kind.expr);
|
||||
|
||||
const callableTy = calleeTy.is("Fn")
|
||||
? calleeTy
|
||||
@ -292,7 +287,7 @@ class Checker {
|
||||
}
|
||||
|
||||
const args = node.kind.args
|
||||
.map((arg) => this.tys.expr(arg));
|
||||
.map((arg) => this.check(arg));
|
||||
const params = callableTy.kind.params;
|
||||
if (args.length !== params.length) {
|
||||
this.error(
|
||||
|
||||
@ -2,7 +2,7 @@ import * as ast from "../ast.ts";
|
||||
import { FileReporter } from "../diagnostics.ts";
|
||||
import { builtins } from "./builtins.ts";
|
||||
|
||||
export class Syms {
|
||||
export class ResolveMap {
|
||||
constructor(
|
||||
private resols: Map<number, Sym>,
|
||||
) {}
|
||||
@ -35,7 +35,7 @@ export type Sym =
|
||||
export function resolve(
|
||||
file: ast.Node,
|
||||
reporter: FileReporter,
|
||||
): Syms {
|
||||
): ResolveMap {
|
||||
let syms = ResolverSyms.root();
|
||||
const resols = new Map<number, Sym>();
|
||||
|
||||
@ -88,7 +88,7 @@ export function resolve(
|
||||
},
|
||||
});
|
||||
|
||||
return new Syms(resols);
|
||||
return new ResolveMap(resols);
|
||||
}
|
||||
|
||||
class ResolverSyms {
|
||||
|
||||
@ -12,8 +12,8 @@ const text = await Deno.readTextFile(filename);
|
||||
const fileRep = reporter.ofFile({ filename, text });
|
||||
|
||||
const fileAst = front.parse(text, fileRep);
|
||||
const syms = front.resolve(fileAst, fileRep);
|
||||
const tys = new front.Tys(syms, fileRep);
|
||||
const resols = front.resolve(fileAst, fileRep);
|
||||
const checker = new front.Checker(resols, fileRep);
|
||||
|
||||
let mainFn: ast.NodeWithKind<"FnStmt"> | null = null;
|
||||
|
||||
@ -36,7 +36,7 @@ if (!mainFn) {
|
||||
Deno.exit(1);
|
||||
}
|
||||
|
||||
const m = new middle.MiddleLowerer(syms, tys);
|
||||
const m = new middle.MiddleLowerer(resols, checker);
|
||||
const mainMiddleFn = m.lowerFn(mainFn);
|
||||
|
||||
if (!Deno.args.includes("--test")) {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import * as ast from "./ast.ts";
|
||||
import { Syms, Tys } from "./front/mod.ts";
|
||||
import { Checker, ResolveMap } from "./front/mod.ts";
|
||||
import { Ty } from "./ty.ts";
|
||||
import { BasicBlock, BinaryOp, Fn, Inst, InstKind } from "./mir.ts";
|
||||
|
||||
@ -7,15 +7,15 @@ export class MiddleLowerer {
|
||||
private fns = new Map<number, Fn>();
|
||||
|
||||
constructor(
|
||||
private resols: Syms,
|
||||
private tys: Tys,
|
||||
private resols: ResolveMap,
|
||||
private checker: Checker,
|
||||
) {}
|
||||
|
||||
lowerFn(stmt: ast.FnStmt): Fn {
|
||||
if (this.fns.has(stmt.id)) {
|
||||
return this.fns.get(stmt.id)!;
|
||||
}
|
||||
const fn = new FnLowerer(this, this.resols, this.tys, stmt).lower();
|
||||
const fn = new FnLowerer(this, this.resols, this.checker, stmt).lower();
|
||||
this.fns.set(stmt.id, fn);
|
||||
return fn;
|
||||
}
|
||||
@ -28,13 +28,13 @@ class FnLowerer {
|
||||
|
||||
constructor(
|
||||
private lowerer: MiddleLowerer,
|
||||
private resols: Syms,
|
||||
private tys: Tys,
|
||||
private resols: ResolveMap,
|
||||
private checker: Checker,
|
||||
private stmt: ast.FnStmt,
|
||||
) {}
|
||||
|
||||
lower(): Fn {
|
||||
const ty = this.tys.expr(this.stmt);
|
||||
const ty = this.checker.check(this.stmt);
|
||||
this.lowerBlock(this.stmt.kind.body.as("Block"));
|
||||
this.pushInst(Ty.Void, "Return", { source: this.makeVoid() });
|
||||
this.bbs[0].insts.unshift(...this.allocs);
|
||||
@ -49,7 +49,7 @@ class FnLowerer {
|
||||
|
||||
private lowerStmt(stmt: ast.Node) {
|
||||
if (stmt.is("LetStmt")) {
|
||||
const ty = this.tys.expr(stmt.kind.param);
|
||||
const ty = this.checker.check(stmt.kind.param);
|
||||
const expr = this.lowerExpr(stmt.kind.expr);
|
||||
const local = new Inst(Ty.create("Ptr", { ty }), { tag: "Alloca" });
|
||||
this.allocs.push(local);
|
||||
@ -136,12 +136,12 @@ class FnLowerer {
|
||||
}
|
||||
|
||||
if (place.is("IndexExpr")) {
|
||||
const exprTy = this.tys.expr(place.kind.expr);
|
||||
const exprTy = this.checker.check(place.kind.expr);
|
||||
if (!exprTy.is("Array") && !exprTy.is("Slice")) {
|
||||
throw new Error(exprTy.pretty());
|
||||
}
|
||||
const arg = place.kind.arg;
|
||||
const argTy = this.tys.expr(arg);
|
||||
const argTy = this.checker.check(arg);
|
||||
const exprInst = this.lowerExpr(place.kind.expr);
|
||||
if (argTy.is("Int")) {
|
||||
const argInst = this.lowerExpr(arg);
|
||||
@ -183,7 +183,7 @@ class FnLowerer {
|
||||
return this.pushInst(fn.ty, "Fn", { fn });
|
||||
}
|
||||
if (sym.tag === "FnParam") {
|
||||
const ty = this.tys.expr(sym.param);
|
||||
const ty = this.checker.check(sym.param);
|
||||
return this.pushInst(ty, "Param", { idx: sym.idx });
|
||||
}
|
||||
if (sym.tag === "Builtin") {
|
||||
@ -205,19 +205,19 @@ class FnLowerer {
|
||||
return this.pushInst(Ty.Int, "Int", { value: expr.kind.value });
|
||||
}
|
||||
if (expr.is("ArrayExpr")) {
|
||||
const ty = this.tys.expr(expr);
|
||||
const ty = this.checker.check(expr);
|
||||
const values = expr.kind.values
|
||||
.map((value) => this.lowerExpr(value));
|
||||
return this.pushInst(ty, "Array", { values });
|
||||
}
|
||||
if (expr.is("IndexExpr")) {
|
||||
const ty = this.tys.expr(expr.kind.expr);
|
||||
const ty = this.checker.check(expr.kind.expr);
|
||||
const arg = this.lowerExpr(expr.kind.arg);
|
||||
const value = this.lowerExpr(expr.kind.expr);
|
||||
return this.pushInst(ty, "Index", { value, arg });
|
||||
}
|
||||
if (expr.is("CallExpr")) {
|
||||
const ty = this.tys.expr(expr);
|
||||
const ty = this.checker.check(expr);
|
||||
const args = expr.kind.args
|
||||
.map((arg) => this.lowerExpr(arg));
|
||||
|
||||
@ -239,8 +239,8 @@ class FnLowerer {
|
||||
return this.pushInst(ty, "Call", { callee, args });
|
||||
}
|
||||
if (expr.is("UnaryExpr")) {
|
||||
const resultTy = this.tys.expr(expr);
|
||||
const operandTy = this.tys.expr(expr.kind.expr);
|
||||
const resultTy = this.checker.check(expr);
|
||||
const operandTy = this.checker.check(expr.kind.expr);
|
||||
if (
|
||||
expr.kind.op === "Negate" &&
|
||||
operandTy.compatibleWith(Ty.Int) &&
|
||||
@ -273,7 +273,7 @@ class FnLowerer {
|
||||
);
|
||||
}
|
||||
if (place.is("IndexExpr")) {
|
||||
const placeTy = this.tys.expr(place);
|
||||
const placeTy = this.checker.check(place);
|
||||
const placeInst = this.lowerPlace(place);
|
||||
if (placeTy.is("Slice")) {
|
||||
return placeInst;
|
||||
@ -295,9 +295,9 @@ class FnLowerer {
|
||||
);
|
||||
}
|
||||
if (expr.is("BinaryExpr")) {
|
||||
const resultTy = this.tys.expr(expr);
|
||||
const leftTy = this.tys.expr(expr.kind.left);
|
||||
const rightTy = this.tys.expr(expr.kind.right);
|
||||
const resultTy = this.checker.check(expr);
|
||||
const leftTy = this.checker.check(expr.kind.left);
|
||||
const rightTy = this.checker.check(expr.kind.right);
|
||||
const binaryOp = binaryOpPatterns
|
||||
.find((pat) =>
|
||||
expr.kind.op === pat.op &&
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
|
||||
fn main()
|
||||
{
|
||||
let array: [int; 3] = [1, 2, 3];
|
||||
let array: [int; 3] = [1, 2, false];
|
||||
|
||||
let a = 4;
|
||||
let b = a;
|
||||
array[0] = a;
|
||||
print_int(array[0]);
|
||||
// let a = 4;
|
||||
// let b = a;
|
||||
// array[0] = a;
|
||||
// print_int(array[0]);
|
||||
|
||||
// let elem: int = array[0];
|
||||
// // e xpect: 1
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user