slige/compiler/ast.ts

166 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-12-10 21:42:15 +01:00
import { Pos } from "./token.ts";
2024-12-26 03:56:59 +01:00
import { GenericArgsMap, VType } from "./vtype.ts";
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;
genericParams?: GenericParam[];
2024-12-06 14:17:52 +01:00
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"; assignType: AssignType; subject: Expr; value: Expr }
2024-12-06 14:17:52 +01:00
| { type: "expr"; expr: Expr };
2024-11-15 15:20:49 +01:00
export type AssignType = "=" | "+=" | "-=";
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"; ident: string }
| {
type: "sym";
ident: string;
sym: Sym;
}
2024-12-06 14:17:52 +01:00
| { type: "group"; expr: Expr }
| { type: "field"; subject: Expr; ident: string }
2024-12-06 14:17:52 +01:00
| { type: "index"; subject: Expr; value: Expr }
2024-12-26 03:56:59 +01:00
| {
type: "call";
subject: Expr;
args: Expr[];
genericArgs?: GenericArgsMap;
}
| { type: "path"; subject: Expr; ident: string }
| { type: "etype_args"; subject: Expr; etypeArgs: EType[] }
2024-12-06 14:17:52 +01:00
| { type: "unary"; unaryType: UnaryType; subject: Expr }
| { type: "binary"; binaryType: BinaryType; left: Expr; right: Expr }
2024-12-17 10:24:18 +01:00
| { type: "if"; cond: Expr; truthy: Expr; falsy?: Expr; elsePos?: Pos }
2024-12-06 14:17:52 +01:00
| { type: "bool"; value: boolean }
| { type: "null" }
| { type: "loop"; body: Expr }
| { type: "block"; stmts: Stmt[]; expr?: Expr }
| { type: "while"; cond: Expr; body: Expr }
| { type: "for_in"; param: Param; value: Expr; body: Expr }
| {
type: "for";
decl?: Stmt;
cond?: Expr;
incr?: Stmt;
body: Expr;
2024-12-06 14:17:52 +01:00
};
2024-11-22 14:06:28 +01:00
export type UnaryType = "not" | "-";
2024-12-13 10:26:34 +01:00
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-26 02:38:32 +01:00
| { type: "generic"; stmt: Stmt; genericParam: GenericParam };
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: "null" }
| { type: "int" }
| { type: "bool" }
| { type: "string" }
| { type: "ident"; ident: string }
| {
type: "sym";
ident: string;
sym: Sym;
}
2024-12-06 14:17:52 +01:00
| { type: "array"; inner: EType }
| { type: "struct"; fields: Param[] };
2024-12-11 12:36:19 +01:00
export type GenericParam = {
2024-12-26 01:51:05 +01:00
id: number;
2024-12-13 10:26:34 +01:00
ident: string;
pos: Pos;
vtype?: VType;
};
2024-12-11 12:36:19 +01:00
export type Anno = {
ident: string;
values: Expr[];
pos: Pos;
};
export class AstCreator {
private nextNodeId = 0;
public stmt(kind: StmtKind, pos: Pos): Stmt {
const id = this.nextNodeId;
this.nextNodeId += 1;
return { kind, pos, id };
}
public expr(kind: ExprKind, pos: Pos): Expr {
const id = this.nextNodeId;
this.nextNodeId += 1;
return { kind, pos, id };
}
public etype(kind: ETypeKind, pos: Pos): EType {
const id = this.nextNodeId;
this.nextNodeId += 1;
return { kind, pos, id };
}
}