This commit is contained in:
parent
4f9ea23d84
commit
5faabe93c2
@ -267,18 +267,27 @@ class Checker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node.is("IdentTy")) {
|
if (node.is("IdentTy")) {
|
||||||
switch (node.kind.ident) {
|
const sym = this.syms.get(node);
|
||||||
case "void":
|
if (sym.tag === "BuiltinTy") {
|
||||||
return Ty.Void;
|
switch (sym.ident) {
|
||||||
case "int":
|
case "void":
|
||||||
return Ty.Int;
|
return Ty.Void;
|
||||||
case "bool":
|
case "int":
|
||||||
return Ty.Bool;
|
return Ty.Int;
|
||||||
case "str":
|
case "bool":
|
||||||
return Ty.Str;
|
return Ty.Bool;
|
||||||
default:
|
case "str":
|
||||||
this.error(node.loc, `unknown type '${node.kind.ident}'`);
|
return Ty.Str;
|
||||||
|
case "u8":
|
||||||
|
return Ty.U8;
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
`unknown type '${node.kind.ident}'`,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
this.error(node.loc, `symbol is not a type`);
|
||||||
|
this.fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.is("PtrTy")) {
|
if (node.is("PtrTy")) {
|
||||||
|
|||||||
@ -30,7 +30,8 @@ export type Sym =
|
|||||||
stmt: ast.NodeWithKind<"LetStmt">;
|
stmt: ast.NodeWithKind<"LetStmt">;
|
||||||
param: ast.NodeWithKind<"Param">;
|
param: ast.NodeWithKind<"Param">;
|
||||||
}
|
}
|
||||||
| { tag: "Loop"; stmt: ast.Node };
|
| { tag: "Loop"; stmt: ast.Node }
|
||||||
|
| { tag: "BuiltinTy"; ident: string };
|
||||||
|
|
||||||
export function resolve(
|
export function resolve(
|
||||||
file: ast.Node,
|
file: ast.Node,
|
||||||
@ -103,6 +104,15 @@ export function resolve(
|
|||||||
}
|
}
|
||||||
resols.set(node.id, { tag: "Loop", stmt: loopNode });
|
resols.set(node.id, { tag: "Loop", stmt: loopNode });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (k.tag === "IdentTy") {
|
||||||
|
const sym = syms.resolveTy(k.ident);
|
||||||
|
if (sym === null) {
|
||||||
|
reporter.error(node.loc, `undefined symbol '${k.ident}'`);
|
||||||
|
reporter.abort();
|
||||||
|
}
|
||||||
|
resols.set(node.id, sym);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -148,4 +158,19 @@ class ResolverSyms {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
resolveTy(ident: string): Sym | null {
|
||||||
|
const builtins: string[] = ["void", "int", "bool", "str", "u8"];
|
||||||
|
if (builtins.includes(ident)) {
|
||||||
|
return { tag: "BuiltinTy", ident } as Sym;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.syms.has(ident)) {
|
||||||
|
return this.syms.get(ident)!;
|
||||||
|
}
|
||||||
|
if (this.parent) {
|
||||||
|
return this.parent.resolveExpr(ident);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/ty.ts
16
src/ty.ts
@ -24,6 +24,8 @@ export class Ty {
|
|||||||
static Error = Ty.create("Error", {});
|
static Error = Ty.create("Error", {});
|
||||||
static Void = Ty.create("Void", {});
|
static Void = Ty.create("Void", {});
|
||||||
static Int = Ty.create("Int", {});
|
static Int = Ty.create("Int", {});
|
||||||
|
static IntLiteral = Ty.create("IntLiteral", {});
|
||||||
|
static U8 = Ty.create("UInt", { size: 8 });
|
||||||
static Bool = Ty.create("Bool", {});
|
static Bool = Ty.create("Bool", {});
|
||||||
static Str = Ty.create("Str", {});
|
static Str = Ty.create("Str", {});
|
||||||
|
|
||||||
@ -57,6 +59,10 @@ export class Ty {
|
|||||||
if (this.is("Int")) {
|
if (this.is("Int")) {
|
||||||
return other.is("Int");
|
return other.is("Int");
|
||||||
}
|
}
|
||||||
|
if (this.is("IntLiteral")) {
|
||||||
|
// return other.is("Int") || other.is("UInt");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (this.is("Bool")) {
|
if (this.is("Bool")) {
|
||||||
return other.is("Bool");
|
return other.is("Bool");
|
||||||
}
|
}
|
||||||
@ -126,6 +132,8 @@ export class Ty {
|
|||||||
throw new Error(`'${this.kind.tag}' not handled`);
|
throw new Error(`'${this.kind.tag}' not handled`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertibleTo(other: Ty): boolean {}
|
||||||
|
|
||||||
isSized(): boolean {
|
isSized(): boolean {
|
||||||
if (this.is("Slice") || this.is("Str")) {
|
if (this.is("Slice") || this.is("Str")) {
|
||||||
return false;
|
return false;
|
||||||
@ -141,6 +149,10 @@ export class Ty {
|
|||||||
return "void";
|
return "void";
|
||||||
case "Int":
|
case "Int":
|
||||||
return "int";
|
return "int";
|
||||||
|
case "IntLiteral":
|
||||||
|
return "{integer}";
|
||||||
|
case "UInt":
|
||||||
|
return `u${this.kind.size}`;
|
||||||
case "Bool":
|
case "Bool":
|
||||||
return "bool";
|
return "bool";
|
||||||
case "Str":
|
case "Str":
|
||||||
@ -180,6 +192,8 @@ export type TyKind =
|
|||||||
| { tag: "Error" }
|
| { tag: "Error" }
|
||||||
| { tag: "Void" }
|
| { tag: "Void" }
|
||||||
| { tag: "Int" }
|
| { tag: "Int" }
|
||||||
|
| { tag: "IntLiteral" }
|
||||||
|
| { tag: "UInt"; size: IntSize }
|
||||||
| { tag: "Bool" }
|
| { tag: "Bool" }
|
||||||
| { tag: "Str" }
|
| { tag: "Str" }
|
||||||
| { tag: "Ptr"; ty: Ty }
|
| { tag: "Ptr"; ty: Ty }
|
||||||
@ -189,3 +203,5 @@ export type TyKind =
|
|||||||
| { tag: "Range" }
|
| { tag: "Range" }
|
||||||
| { tag: "Fn"; params: Ty[]; retTy: Ty }
|
| { tag: "Fn"; params: Ty[]; retTy: Ty }
|
||||||
| { tag: "FnStmt"; ty: Ty; stmt: ast.NodeWithKind<"FnStmt"> };
|
| { tag: "FnStmt"; ty: Ty; stmt: ast.NodeWithKind<"FnStmt"> };
|
||||||
|
|
||||||
|
export type IntSize = 8 | 16 | 32 | 64;
|
||||||
|
|||||||
7
tests/int.ethlang
Normal file
7
tests/int.ethlang
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
// let a: u8 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// vim: syntax=rust commentstring=//\ %s
|
||||||
Loading…
x
Reference in New Issue
Block a user