2024-12-10 21:42:15 +01:00
|
|
|
import { Pos } from "./token.ts";
|
|
|
|
import { VType } from "./vtype.ts";
|
2024-11-15 15:20:49 +01:00
|
|
|
|
2024-12-12 12:04:57 +01:00
|
|
|
export type Ast = File[];
|
|
|
|
|
|
|
|
export type File = {
|
|
|
|
path: string;
|
|
|
|
stmts: Stmt[];
|
|
|
|
};
|
|
|
|
|
2024-11-15 15:20:49 +01:00
|
|
|
export type Stmt = {
|
2024-12-06 14:17:52 +01:00
|
|
|
kind: StmtKind;
|
|
|
|
pos: Pos;
|
|
|
|
id: number;
|
2024-11-15 15:20:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export type StmtKind =
|
|
|
|
| { type: "error" }
|
2024-12-12 12:04:57 +01:00
|
|
|
| { type: "import"; path: Expr }
|
2024-12-06 14:17:52 +01:00
|
|
|
| { type: "break"; expr?: Expr }
|
|
|
|
| { type: "return"; expr?: Expr }
|
|
|
|
| {
|
|
|
|
type: "fn";
|
|
|
|
ident: string;
|
|
|
|
params: Param[];
|
|
|
|
returnType?: EType;
|
|
|
|
body: Expr;
|
2024-12-11 13:03:01 +01:00
|
|
|
anno?: Anno;
|
2024-12-09 13:33:33 +01:00
|
|
|
vtype?: VType;
|
2024-12-06 14:17:52 +01:00
|
|
|
}
|
|
|
|
| { type: "let"; param: Param; value: Expr }
|
|
|
|
| { type: "assign"; subject: Expr; value: Expr }
|
|
|
|
| { type: "expr"; expr: Expr };
|
2024-11-15 15:20:49 +01:00
|
|
|
|
|
|
|
export type Expr = {
|
2024-12-06 14:17:52 +01:00
|
|
|
kind: ExprKind;
|
|
|
|
pos: Pos;
|
|
|
|
vtype?: VType;
|
|
|
|
id: number;
|
2024-11-15 15:20:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export type ExprKind =
|
|
|
|
| { type: "error" }
|
2024-12-06 14:17:52 +01:00
|
|
|
| { type: "int"; value: number }
|
|
|
|
| { type: "string"; value: string }
|
|
|
|
| { type: "ident"; value: string }
|
|
|
|
| { type: "group"; expr: Expr }
|
|
|
|
| { type: "field"; subject: Expr; value: string }
|
|
|
|
| { type: "index"; subject: Expr; value: Expr }
|
|
|
|
| { type: "call"; subject: Expr; args: Expr[] }
|
|
|
|
| { type: "unary"; unaryType: UnaryType; subject: Expr }
|
|
|
|
| { type: "binary"; binaryType: BinaryType; left: Expr; right: Expr }
|
|
|
|
| { type: "if"; cond: Expr; truthy: Expr; falsy?: Expr }
|
|
|
|
| { type: "bool"; value: boolean }
|
|
|
|
| { type: "null" }
|
|
|
|
| { type: "loop"; body: Expr }
|
|
|
|
| { type: "block"; stmts: Stmt[]; expr?: Expr }
|
|
|
|
| {
|
|
|
|
type: "sym";
|
|
|
|
ident: string;
|
2024-12-10 14:36:41 +01:00
|
|
|
sym: Sym;
|
2024-12-06 14:17:52 +01:00
|
|
|
};
|
2024-11-22 14:06:28 +01:00
|
|
|
|
2024-12-13 10:26:34 +01:00
|
|
|
export type UnaryType = "not";
|
|
|
|
export type BinaryType =
|
|
|
|
| "+"
|
|
|
|
| "*"
|
|
|
|
| "=="
|
|
|
|
| "-"
|
|
|
|
| "/"
|
|
|
|
| "!="
|
|
|
|
| "<"
|
|
|
|
| ">"
|
|
|
|
| "<="
|
|
|
|
| ">="
|
|
|
|
| "or"
|
|
|
|
| "and";
|
|
|
|
|
|
|
|
export type Param = {
|
|
|
|
ident: string;
|
|
|
|
etype?: EType;
|
|
|
|
pos: Pos;
|
|
|
|
vtype?: VType;
|
|
|
|
};
|
|
|
|
|
2024-11-22 14:06:28 +01:00
|
|
|
export type Sym = {
|
2024-12-06 14:17:52 +01:00
|
|
|
ident: string;
|
|
|
|
pos?: Pos;
|
2024-12-10 14:36:41 +01:00
|
|
|
} & SymKind;
|
|
|
|
|
|
|
|
export type SymKind =
|
|
|
|
| { type: "let"; stmt: Stmt; param: Param }
|
|
|
|
| { type: "let_static"; stmt: Stmt; param: Param }
|
|
|
|
| { type: "fn"; stmt: Stmt }
|
|
|
|
| { type: "fn_param"; param: Param }
|
|
|
|
| { type: "closure"; inner: Sym }
|
2024-12-11 12:36:19 +01:00
|
|
|
| { type: "builtin"; builtinId: number };
|
2024-12-06 14:17:52 +01:00
|
|
|
|
|
|
|
export type EType = {
|
|
|
|
kind: ETypeKind;
|
|
|
|
pos: Pos;
|
|
|
|
id: number;
|
|
|
|
};
|
2024-11-22 14:06:28 +01:00
|
|
|
|
2024-12-06 14:17:52 +01:00
|
|
|
export type ETypeKind =
|
|
|
|
| { type: "error" }
|
|
|
|
| { type: "ident"; value: string }
|
|
|
|
| { type: "array"; inner: EType }
|
|
|
|
| { type: "struct"; fields: Param[] };
|
2024-12-11 12:36:19 +01:00
|
|
|
|
2024-12-13 10:26:34 +01:00
|
|
|
export type ETypeParam = {
|
|
|
|
ident: string;
|
|
|
|
pos: Pos;
|
|
|
|
vtype?: VType;
|
|
|
|
};
|
|
|
|
|
2024-12-11 12:36:19 +01:00
|
|
|
export type Anno = {
|
|
|
|
ident: string;
|
|
|
|
values: Expr[];
|
|
|
|
pos: Pos;
|
|
|
|
};
|