add int types
All checks were successful
Check / Explore-Gitea-Actions (push) Successful in 8s

This commit is contained in:
sfja 2026-03-17 21:29:27 +01:00
parent 4f9ea23d84
commit 5faabe93c2
4 changed files with 69 additions and 12 deletions

View File

@ -267,18 +267,27 @@ class Checker {
}
if (node.is("IdentTy")) {
switch (node.kind.ident) {
case "void":
return Ty.Void;
case "int":
return Ty.Int;
case "bool":
return Ty.Bool;
case "str":
return Ty.Str;
default:
this.error(node.loc, `unknown type '${node.kind.ident}'`);
const sym = this.syms.get(node);
if (sym.tag === "BuiltinTy") {
switch (sym.ident) {
case "void":
return Ty.Void;
case "int":
return Ty.Int;
case "bool":
return Ty.Bool;
case "str":
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")) {

View File

@ -30,7 +30,8 @@ export type Sym =
stmt: ast.NodeWithKind<"LetStmt">;
param: ast.NodeWithKind<"Param">;
}
| { tag: "Loop"; stmt: ast.Node };
| { tag: "Loop"; stmt: ast.Node }
| { tag: "BuiltinTy"; ident: string };
export function resolve(
file: ast.Node,
@ -103,6 +104,15 @@ export function resolve(
}
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;
}
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;
}
}

View File

@ -24,6 +24,8 @@ export class Ty {
static Error = Ty.create("Error", {});
static Void = Ty.create("Void", {});
static Int = Ty.create("Int", {});
static IntLiteral = Ty.create("IntLiteral", {});
static U8 = Ty.create("UInt", { size: 8 });
static Bool = Ty.create("Bool", {});
static Str = Ty.create("Str", {});
@ -57,6 +59,10 @@ export class Ty {
if (this.is("Int")) {
return other.is("Int");
}
if (this.is("IntLiteral")) {
// return other.is("Int") || other.is("UInt");
return false;
}
if (this.is("Bool")) {
return other.is("Bool");
}
@ -126,6 +132,8 @@ export class Ty {
throw new Error(`'${this.kind.tag}' not handled`);
}
// convertibleTo(other: Ty): boolean {}
isSized(): boolean {
if (this.is("Slice") || this.is("Str")) {
return false;
@ -141,6 +149,10 @@ export class Ty {
return "void";
case "Int":
return "int";
case "IntLiteral":
return "{integer}";
case "UInt":
return `u${this.kind.size}`;
case "Bool":
return "bool";
case "Str":
@ -180,6 +192,8 @@ export type TyKind =
| { tag: "Error" }
| { tag: "Void" }
| { tag: "Int" }
| { tag: "IntLiteral" }
| { tag: "UInt"; size: IntSize }
| { tag: "Bool" }
| { tag: "Str" }
| { tag: "Ptr"; ty: Ty }
@ -189,3 +203,5 @@ export type TyKind =
| { tag: "Range" }
| { tag: "Fn"; params: Ty[]; retTy: Ty }
| { tag: "FnStmt"; ty: Ty; stmt: ast.NodeWithKind<"FnStmt"> };
export type IntSize = 8 | 16 | 32 | 64;

7
tests/int.ethlang Normal file
View File

@ -0,0 +1,7 @@
fn main()
{
// let a: u8 = 0;
}
// vim: syntax=rust commentstring=//\ %s