This commit is contained in:
nora 2023-07-23 14:02:53 +02:00
parent 91b183c002
commit 4e95bc05a3
9 changed files with 640 additions and 132 deletions

View file

@ -1,33 +1,116 @@
import { Span } from "./error";
export type ItemKind = {
kind: "function",
node: FunctionDef,
kind: "function";
node: FunctionDef;
};
export type Item = ItemKind & {
span: Span,
}
span: Span;
};
export type FunctionDef = {
name: string,
args: FunctionArg[],
body: Expr,
}
name: string;
args: FunctionArg[];
body: Expr;
};
export type FunctionArg = {
name: string,
span: Span,
}
name: string;
span: Span;
};
export type ExprKind = {
kind: "lit_string",
value: string,
} | {
kind: "ident",
value: string,
}
export type ExprKind =
| { kind: "empty" }
| { kind: "let"; name: string; rhs: Expr; after: Expr }
| { kind: "block"; exprs: Expr[] }
| {
kind: "literal";
value: Literal;
}
| {
kind: "ident";
value: string;
}
| {
kind: "binary";
binaryKind: BinaryKind;
lhs: Expr;
rhs: Expr;
}
| {
kind: "unary",
unaryKind: UnaryKind,
rhs: Expr,
}
| {
kind: "call",
lhs: Expr,
args: Expr[],
};
export type Expr = ExprKind & {
span: Span,
span: Span;
};
export type Literal =
| {
kind: "str";
value: string;
}
| {
kind: "int";
value: number;
};
export type BinaryKind =
| "+"
| "-"
| "*"
| "/"
| "&"
| "|"
| "<"
| ">"
| "=="
| "<="
| ">="
| "!=";
export const COMPARISON_KINDS: BinaryKind[] = [
">",
"<",
"==",
"<=",
">=",
"!=",
];
export const LOGICAL_KINDS: BinaryKind[] = ["&", "|"];
export const ARITH_TERM_KINDS: BinaryKind[] = ["+", "-"];
export const ARITH_FACTOR_KINDS: BinaryKind[] = ["*", "/"];
const BINARY_KIND_PREC_CLASS = new Map<BinaryKind, number>([
["+", 0],
["-", 0],
["*", 0],
["/", 0],
["&", 1],
["|", 2],
["<", 3],
[">", 4],
["==", 5],
["<=", 6],
[">=", 7],
["!=", 8],
]);
export function binaryExprPrecedenceClass(k: BinaryKind): number {
const cls = BINARY_KIND_PREC_CLASS.get(k);
if (!cls) {
throw new Error(`Invalid binary kind: ${k}`);
}
return cls;
}
export type UnaryKind = '!' | '-';
export const UNARY_KINDS: UnaryKind[] = ['!', '-'];