start structs

This commit is contained in:
nora 2023-07-27 20:56:02 +02:00
parent f6f6673721
commit b52abed441
8 changed files with 214 additions and 62 deletions

View file

@ -10,10 +10,15 @@ export type Identifier = {
export type ItemId = number;
export type ItemKind = {
kind: "function";
node: FunctionDef;
};
export type ItemKind =
| {
kind: "function";
node: FunctionDef;
}
| {
kind: "type";
node: TypeDef;
};
export type Item = ItemKind & {
span: Span;
@ -34,6 +39,17 @@ export type FunctionArg = {
span: Span;
};
export type TypeDef = {
name: string;
fields: FieldDef[];
ty?: TyStruct;
};
export type FieldDef = {
name: Identifier;
type: Type;
};
export type ExprEmpty = { kind: "empty" };
export type ExprLet = {
@ -262,7 +278,21 @@ export type TyVar = {
index: number;
};
export type Ty = TyString | TyInt | TyBool | TyList | TyTuple | TyFn | TyVar;
export type TyStruct = {
kind: "struct";
name: string;
fields: [string, Ty][];
};
export type Ty =
| TyString
| TyInt
| TyBool
| TyList
| TyTuple
| TyFn
| TyVar
| TyStruct;
export function tyIsUnit(ty: Ty): ty is TyUnit {
return ty.kind === "tuple" && ty.elems.length === 0;
@ -331,6 +361,19 @@ export function superFoldItem(item: Item, folder: Folder): Item {
id: item.id,
};
}
case "type": {
const fields = item.node.fields.map(({ name, type }) => ({
name,
type: folder.type(type),
}));
return {
kind: "type",
span: item.span,
node: { name: item.node.name, fields },
id: item.id,
};
}
}
}