slige/compiler/ast.ts

101 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-11-15 15:20:49 +01:00
import { Pos } from "./Token.ts";
2024-12-06 14:17:52 +01:00
import { VType } from "./vtypes.ts";
2024-11-15 15:20:49 +01:00
2024-11-27 15:10:48 +01:00
export type UnaryType = "not";
2024-12-06 14:17:52 +01:00
export type BinaryType =
| "+"
| "*"
| "=="
| "-"
| "/"
| "!="
| "<"
| ">"
| "<="
| ">="
| "or"
| "and";
2024-11-15 15:20:49 +01:00
export type Param = {
2024-12-06 14:17:52 +01:00
ident: string;
etype?: EType;
pos: Pos;
vtype?: VType;
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-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-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
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 }
| { type: "builtin" };
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[] };