From f0227af982c8a9b7586046b2e08df76f28e485dc Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 29 Jul 2023 22:12:05 +0200 Subject: [PATCH] small improvements --- src/index.ts | 3 +-- src/lower.ts | 4 +++- src/parser.ts | 44 ++++++++++++++++++++++++++++++++------------ src/typeck.ts | 17 ----------------- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/index.ts b/src/index.ts index dbfe63c..9accbf7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,8 +11,7 @@ import { exec } from "child_process"; const input = ` function main() = ( - print("\\3AAA\\n"); - print("meow\\n"); + 1 + 2 * 3; ); `; diff --git a/src/lower.ts b/src/lower.ts index a196058..5c5e5a1 100644 --- a/src/lower.ts +++ b/src/lower.ts @@ -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); diff --git a/src/parser.ts b/src/parser.ts index 4f501ad..66a98a3 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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); } }, }; diff --git a/src/typeck.ts b/src/typeck.ts index 323159a..944e9ea 100644 --- a/src/typeck.ts +++ b/src/typeck.ts @@ -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!;