From d9868e31112cd0da85bc0dffa9b728258275c8fb Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 30 Jul 2023 22:03:24 +0200 Subject: [PATCH] implement % --- src/ast.ts | 4 +++- src/index.ts | 10 +++++++++- src/lexer.ts | 2 ++ src/lower.ts | 7 ++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index 96d89fa..45e203d 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -191,6 +191,7 @@ export type BinaryKind = | "-" | "*" | "/" + | "%" | "&" | "|" | "<" @@ -211,13 +212,14 @@ export const COMPARISON_KINDS: BinaryKind[] = [ export const EQUALITY_KINDS: BinaryKind[] = ["==", "!="]; export const LOGICAL_KINDS: BinaryKind[] = ["&", "|"]; export const ARITH_TERM_KINDS: BinaryKind[] = ["+", "-"]; -export const ARITH_FACTOR_KINDS: BinaryKind[] = ["*", "/"]; +export const ARITH_FACTOR_KINDS: BinaryKind[] = ["*", "/", "%"]; const BINARY_KIND_PREC_CLASS = new Map([ ["+", 0], ["-", 0], ["*", 0], ["/", 0], + ["%", 0], ["&", 1], ["|", 2], ["<", 3], diff --git a/src/index.ts b/src/index.ts index 24aa8a9..d8541ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,15 @@ import { exec } from "child_process"; const input = ` function main() = ( - let a = (0,); + prInt(0); + prInt(1); + prInt(9); + prInt(10); + prInt(100); +); + +function prInt(a: Int) = ( + ); function uwu(): (Int, Int) = (0, 0); diff --git a/src/lexer.ts b/src/lexer.ts index 07add2d..3de6b7c 100644 --- a/src/lexer.ts +++ b/src/lexer.ts @@ -24,6 +24,7 @@ export type DatalessToken = | "-" | "*" | "/" + | "%" | "&" | "|" | "!" @@ -76,6 +77,7 @@ const SINGLE_PUNCT: string[] = [ "/", "&", "|", + "%", ]; export function tokenize(input: string): Token[] { diff --git a/src/lower.ts b/src/lower.ts index 8fc0937..58f9d31 100644 --- a/src/lower.ts +++ b/src/lower.ts @@ -407,6 +407,9 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) { case "/": kind = `${valty}.div_u`; break; + case "%": + kind = `${valty}.rem_u`; + break; case "&": kind = `${valty}.and`; break; @@ -433,7 +436,8 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) { kind = `${valty}.ne`; break; } - instrs.push({ kind }); + const instr: wasm.NumericInstr = { kind } as any; // Typescript is buggy. + instrs.push(instr); } else if (lhsTy.kind === "bool" && rhsTy.kind === "bool") { let kind: wasm.Instr["kind"]; @@ -458,6 +462,7 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) { case "-": case "*": case "/": + case "%": throw new Error(`Invalid bool binary expr: ${expr.binaryKind}`); }