slige/compiler/ast.ts

122 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-12-10 20:42:15 +00:00
import { Pos } from "./token.ts";
import { VType } from "./vtype.ts";
2024-11-15 14:20:49 +00:00
2024-12-12 11:04:57 +00:00
export type Ast = File[];
export type File = {
path: string;
stmts: Stmt[];
};
2024-11-15 14:20:49 +00:00
export type Stmt = {
2024-12-06 13:17:52 +00:00
kind: StmtKind;
pos: Pos;
id: number;
2024-11-15 14:20:49 +00:00
};
export type StmtKind =
| { type: "error" }
2024-12-12 11:04:57 +00:00
| { type: "import"; path: Expr }
2024-12-06 13:17:52 +00:00
| { type: "break"; expr?: Expr }
| { type: "return"; expr?: Expr }
| {
type: "fn";
ident: string;
params: Param[];
returnType?: EType;
body: Expr;
2024-12-11 12:03:01 +00:00
anno?: Anno;
2024-12-09 12:33:33 +00:00
vtype?: VType;
2024-12-06 13:17:52 +00:00
}
| { type: "let"; param: Param; value: Expr }
| { type: "assign"; subject: Expr; value: Expr }
| { type: "expr"; expr: Expr };
2024-11-15 14:20:49 +00:00
export type Expr = {
2024-12-06 13:17:52 +00:00
kind: ExprKind;
pos: Pos;
vtype?: VType;
id: number;
2024-11-15 14:20:49 +00:00
};
export type ExprKind =
| { type: "error" }
2024-12-06 13:17:52 +00: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 13:36:41 +00:00
sym: Sym;
2024-12-06 13:17:52 +00:00
};
2024-11-22 13:06:28 +00:00
2024-12-13 09:26:34 +00:00
export type UnaryType = "not";
export type BinaryType =
| "+"
| "*"
| "=="
| "-"
| "/"
| "!="
| "<"
| ">"
| "<="
| ">="
| "or"
| "and";
export type Param = {
ident: string;
etype?: EType;
pos: Pos;
vtype?: VType;
};
2024-11-22 13:06:28 +00:00
export type Sym = {
2024-12-06 13:17:52 +00:00
ident: string;
pos?: Pos;
2024-12-10 13:36:41 +00: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 11:36:19 +00:00
| { type: "builtin"; builtinId: number };
2024-12-06 13:17:52 +00:00
export type EType = {
kind: ETypeKind;
pos: Pos;
id: number;
};
2024-11-22 13:06:28 +00:00
2024-12-06 13:17:52 +00:00
export type ETypeKind =
| { type: "error" }
| { type: "ident"; value: string }
| { type: "array"; inner: EType }
| { type: "struct"; fields: Param[] };
2024-12-11 11:36:19 +00:00
2024-12-13 09:26:34 +00:00
export type ETypeParam = {
ident: string;
pos: Pos;
vtype?: VType;
};
2024-12-11 11:36:19 +00:00
export type Anno = {
ident: string;
values: Expr[];
pos: Pos;
};