mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-15 00:45:04 +01:00
start structs
This commit is contained in:
parent
f6f6673721
commit
b52abed441
8 changed files with 214 additions and 62 deletions
53
src/ast.ts
53
src/ast.ts
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue