Compare commits

..

No commits in common. "6162c8e04206649f5e32a18faa9caf51dcc4acb3" and "87f90c67008b03a9f853e5de62b8b0fbd33246a7" have entirely different histories.

5 changed files with 67 additions and 226 deletions

View File

@ -101,7 +101,7 @@ export class Node {
case "IndexExpr":
return visit(k.value, k.arg);
case "CallExpr":
return visit(k.value, ...k.generics ?? [], ...k.args);
return visit(k.value, ...k.args);
case "UnaryExpr":
return visit(k.expr);
case "BinaryExpr":
@ -117,8 +117,6 @@ export class Node {
return visit(k.ty, k.length);
case "SliceTy":
return visit(k.ty);
case "Generic":
return visit();
}
k satisfies never;
}
@ -133,7 +131,6 @@ export type NodeKind =
| {
tag: "FnStmt";
ident: string;
genericParams: Node[] | null;
params: Node[];
retTy: Node | null;
body: Node;
@ -149,7 +146,7 @@ export type NodeKind =
| { tag: "StrExpr"; value: string }
| { tag: "ArrayExpr"; values: Node[] }
| { tag: "IndexExpr"; value: Node; arg: Node }
| { tag: "CallExpr"; value: Node; generics: Node[] | null; args: Node[] }
| { tag: "CallExpr"; value: Node; args: Node[] }
| { tag: "UnaryExpr"; op: UnaryOp; expr: Node; tok: string }
| { tag: "BinaryExpr"; op: BinaryOp; left: Node; right: Node; tok: string }
| {
@ -161,8 +158,7 @@ export type NodeKind =
| { tag: "IdentTy"; ident: string }
| { tag: "PtrTy" | "PtrMutTy"; ty: Node }
| { tag: "ArrayTy"; ty: Node; length: Node }
| { tag: "SliceTy"; ty: Node }
| { tag: "Generic"; ident: string };
| { tag: "SliceTy"; ty: Node };
export type IntTy =
| "i8"

View File

@ -61,7 +61,6 @@ export class CheckedFn {
class TypeChecker {
private nodeTys = new Map<ast.Node, Ty>();
private generics?: Ty[];
private params!: Ty[];
constructor(
@ -72,12 +71,6 @@ class TypeChecker {
) {}
check(): CheckedFn {
const generics = this.fn.kind.genericParams
?.map((_node, idx) => {
return Ty.Generic(idx);
});
this.generics = generics;
const params = this.fn.kind.params
.map((node) => {
const param = node.as("Param");
@ -201,7 +194,6 @@ class TypeChecker {
case "PtrMutTy":
case "ArrayTy":
case "SliceTy":
case "Generic":
break;
default:
k satisfies never;
@ -213,7 +205,7 @@ class TypeChecker {
const ty = Ty.create("FnStmt", {
stmt: this.fn,
ty: Ty.Fn(params, retTy, generics ?? null),
ty: Ty.create("Fn", { params, retTy }),
});
return new CheckedFn(ty, this.nodeTys);
@ -258,6 +250,24 @@ class TypeChecker {
private checkExpr(expr: ast.Node, expected: Ty): Ty {
const k = expr.kind;
switch (k.tag) {
case "Error":
case "File":
case "Block":
case "ExprStmt":
case "AssignStmt":
case "FnStmt":
case "ReturnStmt":
case "LetStmt":
case "IfStmt":
case "WhileStmt":
case "BreakStmt":
case "Param":
case "IdentTy":
case "PtrTy":
case "PtrMutTy":
case "ArrayTy":
case "SliceTy":
throw new Error(`node '${k.tag}' not an expression`);
case "IdentExpr": {
const sym = this.syms.get(expr);
if (sym.tag === "Fn") {
@ -443,7 +453,12 @@ class TypeChecker {
: null;
if (sym?.tag === "Builtin") {
if (sym.id === "len") {
checkArgs(Ty.Fn([Ty.Any], Ty.USize, null).as("Fn"));
checkArgs(
Ty.create("Fn", {
params: [Ty.Any],
retTy: Ty.USize,
}).as("Fn"),
);
return Ty.USize;
}
if (sym.id === "print") {
@ -468,64 +483,8 @@ class TypeChecker {
}
const callableTy = calleeTy.callableTy();
if (callableTy.kind.generics !== null) {
((() => {
throw new Error("generics not implemented");
}) as () => void)();
let generics: Ty[];
if (k.generics) {
generics = k.generics.map((ty) => this.ty(ty));
if (
generics.length !== callableTy.kind.generics.length
) {
this.reporter.error(
expr.loc,
`expected ${callableTy.kind.generics.length} generic type arguments, got ${generics.length}`,
);
this.reporter.abort();
}
} else {
generics = callableTy.kind.generics
.map((_ty) => Ty.Any);
}
const newCalleeTy = Ty.Fn(
callableTy.kind.params
.map((ty) =>
ty.is("Generic") ? generics[ty.kind.idx] : ty
),
callableTy.kind.retTy.is("Generic")
? generics[callableTy.kind.retTy.kind.idx]
: callableTy.kind.retTy,
null,
).as("Fn");
this.rewriteTree(k.value, newCalleeTy);
checkArgs(
Ty.Fn(
callableTy.kind.params
.map((ty) =>
ty.is("Generic")
? generics[ty.kind.idx]
: ty
),
callableTy.kind.retTy.is("Generic")
? generics[callableTy.kind.retTy.kind.idx]
: callableTy.kind.retTy,
null,
).as("Fn"),
);
return callableTy.kind.retTy;
} else {
if (k.generics) {
this.reporter.error(expr.loc, "no generics expected");
this.reporter.abort();
}
checkArgs(callableTy);
return callableTy.kind.retTy;
}
checkArgs(callableTy);
return callableTy.kind.retTy;
}
case "UnaryExpr": {
switch (k.op) {
@ -619,25 +578,6 @@ class TypeChecker {
}
return Ty.create("Range", {});
}
case "Error":
case "File":
case "Block":
case "ExprStmt":
case "AssignStmt":
case "FnStmt":
case "ReturnStmt":
case "LetStmt":
case "IfStmt":
case "WhileStmt":
case "BreakStmt":
case "Param":
case "IdentTy":
case "PtrTy":
case "PtrMutTy":
case "ArrayTy":
case "SliceTy":
case "Generic":
throw new Error(`node '${k.tag}' not an expression`);
default:
k satisfies never;
throw new Error();
@ -651,6 +591,28 @@ class TypeChecker {
private checkTy(ty: ast.Node): Ty {
const k = ty.kind;
switch (k.tag) {
case "Error":
case "File":
case "Block":
case "ExprStmt":
case "AssignStmt":
case "FnStmt":
case "ReturnStmt":
case "LetStmt":
case "IfStmt":
case "WhileStmt":
case "BreakStmt":
case "Param":
case "IdentExpr":
case "IntExpr":
case "StrExpr":
case "ArrayExpr":
case "IndexExpr":
case "CallExpr":
case "UnaryExpr":
case "BinaryExpr":
case "RangeExpr":
throw new Error(`node '${k.tag}' not a type`);
case "IdentTy": {
const sym = this.syms.get(ty);
if (sym.tag === "BuiltinTy") {
@ -685,12 +647,6 @@ class TypeChecker {
);
}
}
if (sym.tag === "Generic") {
if (!this.generics) {
throw new Error();
}
return this.generics[sym.idx];
}
this.reporter.error(ty.loc, `symbol is not a type`);
return this.reporter.abort();
}
@ -726,29 +682,6 @@ class TypeChecker {
const ty = this.ty(k.ty);
return Ty.create("Slice", { ty });
}
case "Error":
case "File":
case "Block":
case "ExprStmt":
case "AssignStmt":
case "FnStmt":
case "ReturnStmt":
case "LetStmt":
case "IfStmt":
case "WhileStmt":
case "BreakStmt":
case "Param":
case "IdentExpr":
case "IntExpr":
case "StrExpr":
case "ArrayExpr":
case "IndexExpr":
case "CallExpr":
case "UnaryExpr":
case "BinaryExpr":
case "RangeExpr":
case "Generic":
throw new Error(`node '${k.tag}' not a type`);
}
}

View File

@ -71,7 +71,6 @@ export class Parser {
const loc = this.loc();
this.step();
const ident = this.mustEat("ident").value;
const genericParams = this.parseGenericParams();
this.mustEat("(");
const params: ast.Node[] = [];
if (!this.test(")")) {
@ -89,13 +88,7 @@ export class Parser {
retTy = this.parseTy();
}
const body = this.parseBlock();
return ast.Node.create(loc, "FnStmt", {
ident,
genericParams,
params,
retTy,
body,
});
return ast.Node.create(loc, "FnStmt", { ident, params, retTy, body });
}
parseReturnStmt(): ast.Node {
@ -266,12 +259,19 @@ export class Parser {
const arg = this.parseExpr();
this.mustEat("]");
expr = ast.Node.create(loc, "IndexExpr", { value: expr, arg });
} else if (this.test("::<")) {
const generics = this.parseGenericArgs();
this.mustEat("(");
expr = this.parseCallExprTail(expr, loc, generics);
} else if (this.eat("(")) {
expr = this.parseCallExprTail(expr, loc, null);
const args: ast.Node[] = [];
if (!this.test(")")) {
args.push(this.parseExpr());
while (this.eat(",")) {
if (this.done || this.test(")")) {
break;
}
args.push(this.parseExpr());
}
}
this.mustEat(")");
expr = ast.Node.create(loc, "CallExpr", { value: expr, args });
} else {
break;
}
@ -279,29 +279,6 @@ export class Parser {
return expr;
}
parseCallExprTail(
expr: ast.Node,
loc: Loc,
generics: ast.Node[] | null,
): ast.Node {
const args: ast.Node[] = [];
if (!this.test(")")) {
args.push(this.parseExpr());
while (this.eat(",")) {
if (this.done || this.test(")")) {
break;
}
args.push(this.parseExpr());
}
}
this.mustEat(")");
return ast.Node.create(loc, "CallExpr", {
value: expr,
generics,
args,
});
}
parseOperand(): ast.Node {
const loc = this.loc();
if (this.test("ident")) {
@ -384,38 +361,6 @@ export class Parser {
}
}
parseGenericArgs(): ast.Node[] | null {
if (!this.eat("::<")) {
return null;
}
const args: ast.Node[] = [];
while (!this.done && !this.test("<")) {
args.push(this.parseTy());
if (!this.eat(",")) {
break;
}
}
this.mustEat(">");
return args;
}
parseGenericParams(): ast.Node[] | null {
if (!this.eat("<")) {
return null;
}
const params: ast.Node[] = [];
while (!this.done && !this.test("<")) {
const loc = this.loc();
const identTok = this.mustEat("ident");
params.push(ast.create(loc, "Generic", { ident: identTok.value }));
if (!this.eat(",")) {
break;
}
}
this.mustEat(">");
return params;
}
private mustEat(type: string, loc = this.loc()): Tok {
const tok = this.current;
if (tok.type !== type) {
@ -478,7 +423,7 @@ const keywordPattern =
/^(?:(?:fn)|(?:return)|(?:let)|(?:if)|(?:else)|(?:while)|(?:break)|(?:or)|(?:and)|(?:not)|(?:mut))/;
const operatorPattern2 =
/((?:\->)|(?:==)|(?:!=)|(?:<=)|(?:>=)|(?:\:\:<)|(?:<<)|(?:>>)|(?:\.\*)|(?:\.\.)|(?:\.\.=)|[\n\(\)\{\}\[\]\,\.\;\:\!\=\<\>\&\^\|\+\-\*\/\%])/g;
/((?:\->)|(?:==)|(?:!=)|(?:<=)|(?:>=)|(?:<<)|(?:>>)|(?:\.\*)|(?:\.\.)|(?:\.\.=)|[\n\(\)\{\}\[\]\,\.\;\:\!\=\<\>\&\^\|\+\-\*\/\%])/g;
export function tokenize(text: string, reporter: FileReporter): Tok[] {
return new Lexer()

View File

@ -19,12 +19,6 @@ export type Sym =
| { tag: "Bool"; value: boolean }
| { tag: "Builtin"; id: string }
| { tag: "Fn"; stmt: ast.NodeWithKind<"FnStmt"> }
| {
tag: "Generic";
stmt: ast.Node;
generic: ast.NodeWithKind<"Generic">;
idx: number;
}
| {
tag: "FnParam";
stmt: ast.NodeWithKind<"FnStmt">;
@ -67,19 +61,6 @@ export function resolve(
if (k.tag === "FnStmt") {
ast.assertNodeWithKind(node, "FnStmt");
syms = ResolverSyms.forkFrom(syms);
if (k.genericParams) {
for (const [idx, param] of k.genericParams?.entries()) {
ast.assertNodeWithKind(param, "Generic");
const sym: Sym = {
tag: "Generic",
stmt: node,
generic: param,
idx,
};
syms.define(param.kind.ident, sym);
resols.set(param.id, sym);
}
}
for (const [idx, param] of k.params.entries()) {
ast.assertNodeWithKind(param, "Param");
const sym: Sym = { tag: "FnParam", stmt: node, param, idx };

View File

@ -48,15 +48,6 @@ export class Ty {
static Array(ty: Ty, length: number): Ty {
return this.create("Array", { ty, length });
}
static Fn(params: Ty[], retTy: Ty, generics: Ty[] | null): Ty {
return this.create("Fn", { params, retTy, generics });
}
static Generic(idx: number): Ty {
return this.create("Generic", { idx });
}
static Instance(ty: Ty, args: Ty[]): Ty {
return this.create("Instance", { ty, args });
}
/** Only used in type checker. */
static Any = Ty.create("Any", {});
@ -76,9 +67,6 @@ export class Ty {
}
private internHash(): string {
if (this.is("FnStmt")) {
return JSON.stringify({ ...this.kind, stmt: this.kind.stmt.id });
}
return JSON.stringify(this.kind);
}
@ -256,10 +244,8 @@ export type TyKind =
| { tag: "Array"; ty: Ty; length: number }
| { tag: "Slice"; ty: Ty }
| { tag: "Range" }
| { tag: "Fn"; params: Ty[]; retTy: Ty; generics: Ty[] | null }
| { tag: "Fn"; params: Ty[]; retTy: Ty }
| { tag: "FnStmt"; ty: Ty; stmt: ast.NodeWithKind<"FnStmt"> }
| { tag: "Generic"; idx: number }
| { tag: "Instance"; ty: Ty; args: Ty[] }
| { tag: "Any" | "AnyInt" }
| { tag: "AnyIndexable"; ty: Ty }
| { tag: "AnyCallable"; ty: Ty }