compiler: unit enums

This commit is contained in:
sfja 2025-02-23 02:56:11 +01:00
parent 66fc0ec0b5
commit 5c9e3ce73f
5 changed files with 31 additions and 26 deletions

View File

@ -405,6 +405,7 @@ export class Checker {
return exhausted(data.kind);
}
case "variant": {
const { item, kind } = res.kind;
const data = res.kind.variant.data;
switch (data.kind.tag) {
case "error":
@ -416,7 +417,7 @@ export class Checker {
);
return Ty({ tag: "error" });
case "unit":
return todo();
return this.enumItemTy(item, kind);
case "tuple":
this.report(
"expected value, got struct type",

View File

@ -311,7 +311,27 @@ export class FnLowerer {
return { tag: "error" };
case "enum":
case "struct":
case "variant":
return todo();
case "variant": {
const ty = this.ch.enumItemTy(re.kind.item, re.kind.kind);
const data = re.kind.variant.data;
switch (data.kind.tag) {
case "error":
return { tag: "error" };
case "struct":
throw new Error("should not check");
case "unit":
return {
tag: "adt",
ty,
fields: [],
variant: re.kind.variant,
};
case "tuple":
return todo();
}
return exhausted(data.kind);
}
case "field":
return todo();
case "fn":

View File

@ -1,13 +1,11 @@
enum Abc {
B(int),
C { v: int },
enum S {
A,
B,
}
fn main() {
let b: Abc = Abc::B(123);
let c = Abc::C { v: 123 };
let v = S::A;
}

View File

@ -178,6 +178,7 @@ export class Resolver implements ast.Visitor {
visitStructItem(item: ast.Item, kind: ast.StructItem): ast.VisitRes {
this.syms.defTy(item.ident, { tag: "struct", item, kind });
this.syms.defVal(item.ident, { tag: "struct", item, kind });
const outerSyms = this.syms;
this.syms = new ItemSyms(this.syms);
ast.visitVariantData(this, kind.data);
@ -241,23 +242,6 @@ export class Resolver implements ast.Visitor {
return "stop";
}
visitCallExpr(expr: ast.Expr, kind: ast.CallExpr): ast.VisitRes {
if (
kind.expr.kind.tag === "path" &&
kind.expr.kind.path.segments.length === 1
) {
const res = this.resolveTyPath(kind.expr.kind.path);
if (res.kind.tag === "struct") {
this.exprResols.set(kind.expr.id, res);
for (const arg of kind.args) {
ast.visitExpr(this, arg);
}
return "stop";
}
}
// otherwise, just continue as usual
}
visitStructExpr(expr: ast.Expr, kind: ast.StructExpr): ast.VisitRes {
if (!kind.path) {
return todo();

View File

@ -200,7 +200,9 @@ export class MirFnStringifyer {
case "error":
return "<error>";
case "unit":
return todo();
return `${tyk.item.ident.text}::${
rval.variant!.ident.text
}`;
case "tuple": {
const name = `${tyk.item.ident.text}::${
rval.variant!.ident.text