mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-14 16:35:03 +01:00
small improvements
This commit is contained in:
parent
3270e6b501
commit
f0227af982
4 changed files with 36 additions and 32 deletions
|
|
@ -11,8 +11,7 @@ import { exec } from "child_process";
|
|||
|
||||
const input = `
|
||||
function main() = (
|
||||
print("\\3AAA\\n");
|
||||
print("meow\\n");
|
||||
1 + 2 * 3;
|
||||
);
|
||||
`;
|
||||
|
||||
|
|
|
|||
|
|
@ -114,7 +114,9 @@ export function lower(ast: Ast): wasm.Module {
|
|||
|
||||
const HEAP_ALIGN = 0x08;
|
||||
cx.reservedHeapMemoryStart =
|
||||
(mod.datas[0].init.length + (HEAP_ALIGN - 1)) & ~(HEAP_ALIGN - 1);
|
||||
mod.datas.length > 0
|
||||
? (mod.datas[0].init.length + (HEAP_ALIGN - 1)) & ~(HEAP_ALIGN - 1)
|
||||
: 0;
|
||||
|
||||
addRt(cx, ast);
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ import {
|
|||
TypeDef,
|
||||
UNARY_KINDS,
|
||||
UnaryKind,
|
||||
binaryExprPrecedenceClass,
|
||||
foldAst,
|
||||
superFoldExpr,
|
||||
} from "./ast";
|
||||
|
|
@ -507,23 +508,42 @@ function unexpectedToken(token: Token): never {
|
|||
function validateAst(ast: Ast) {
|
||||
const validator: Folder = {
|
||||
...DEFAULT_FOLDER,
|
||||
expr(value: Expr): Expr {
|
||||
if (value.kind === "block") {
|
||||
value.exprs.forEach((expr) => {
|
||||
if (expr.kind === "let") {
|
||||
this.expr(expr.rhs);
|
||||
if (expr.type) {
|
||||
this.type(expr.type);
|
||||
expr(expr: Expr): Expr {
|
||||
if (expr.kind === "block") {
|
||||
expr.exprs.forEach((inner) => {
|
||||
if (inner.kind === "let") {
|
||||
this.expr(inner.rhs);
|
||||
if (inner.type) {
|
||||
this.type(inner.type);
|
||||
}
|
||||
} else {
|
||||
this.expr(expr);
|
||||
this.expr(inner);
|
||||
}
|
||||
});
|
||||
return value;
|
||||
} else if (value.kind === "let") {
|
||||
throw new CompilerError("let is only allowed in blocks", value.span);
|
||||
return expr;
|
||||
} else if (expr.kind === "let") {
|
||||
throw new CompilerError("let is only allowed in blocks", expr.span);
|
||||
} else if (expr.kind === "binary") {
|
||||
const checkPrecedence = (inner: Expr, side: string) => {
|
||||
if (inner.kind === "binary") {
|
||||
const ourClass = binaryExprPrecedenceClass(expr.binaryKind);
|
||||
const innerClass = binaryExprPrecedenceClass(inner.binaryKind);
|
||||
|
||||
if (ourClass !== innerClass) {
|
||||
throw new CompilerError(
|
||||
`mixing operators without parentheses is not allowed. ${side} is ${inner.binaryKind}, which is different from ${expr.binaryKind}`,
|
||||
expr.span
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
checkPrecedence(expr.lhs, "left");
|
||||
checkPrecedence(expr.rhs, "right");
|
||||
|
||||
return superFoldExpr(expr, this);
|
||||
} else {
|
||||
return superFoldExpr(value, this);
|
||||
return superFoldExpr(expr, this);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -641,23 +641,6 @@ export function checkBody(
|
|||
}
|
||||
|
||||
function checkBinary(expr: Expr & ExprBinary): Expr {
|
||||
const checkPrecedence = (inner: Expr, side: string) => {
|
||||
if (inner.kind === "binary") {
|
||||
const ourClass = binaryExprPrecedenceClass(expr.binaryKind);
|
||||
const innerClass = binaryExprPrecedenceClass(inner.binaryKind);
|
||||
|
||||
if (ourClass !== innerClass) {
|
||||
throw new CompilerError(
|
||||
`mixing operators without parentheses is not allowed. ${side} is ${inner.binaryKind}, which is different from ${expr.binaryKind}`,
|
||||
expr.span
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
checkPrecedence(expr.lhs, "left");
|
||||
checkPrecedence(expr.rhs, "right");
|
||||
|
||||
let lhsTy = expr.lhs.ty!;
|
||||
let rhsTy = expr.rhs.ty!;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue