implement %

This commit is contained in:
nora 2023-07-30 22:03:24 +02:00
parent b64c02cf4a
commit d9868e3111
4 changed files with 20 additions and 3 deletions

View file

@ -191,6 +191,7 @@ export type BinaryKind =
| "-" | "-"
| "*" | "*"
| "/" | "/"
| "%"
| "&" | "&"
| "|" | "|"
| "<" | "<"
@ -211,13 +212,14 @@ export const COMPARISON_KINDS: BinaryKind[] = [
export const EQUALITY_KINDS: BinaryKind[] = ["==", "!="]; export const EQUALITY_KINDS: BinaryKind[] = ["==", "!="];
export const LOGICAL_KINDS: BinaryKind[] = ["&", "|"]; export const LOGICAL_KINDS: BinaryKind[] = ["&", "|"];
export const ARITH_TERM_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<BinaryKind, number>([ const BINARY_KIND_PREC_CLASS = new Map<BinaryKind, number>([
["+", 0], ["+", 0],
["-", 0], ["-", 0],
["*", 0], ["*", 0],
["/", 0], ["/", 0],
["%", 0],
["&", 1], ["&", 1],
["|", 2], ["|", 2],
["<", 3], ["<", 3],

View file

@ -11,7 +11,15 @@ import { exec } from "child_process";
const input = ` const input = `
function main() = ( 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); function uwu(): (Int, Int) = (0, 0);

View file

@ -24,6 +24,7 @@ export type DatalessToken =
| "-" | "-"
| "*" | "*"
| "/" | "/"
| "%"
| "&" | "&"
| "|" | "|"
| "!" | "!"
@ -76,6 +77,7 @@ const SINGLE_PUNCT: string[] = [
"/", "/",
"&", "&",
"|", "|",
"%",
]; ];
export function tokenize(input: string): Token[] { export function tokenize(input: string): Token[] {

View file

@ -407,6 +407,9 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
case "/": case "/":
kind = `${valty}.div_u`; kind = `${valty}.div_u`;
break; break;
case "%":
kind = `${valty}.rem_u`;
break;
case "&": case "&":
kind = `${valty}.and`; kind = `${valty}.and`;
break; break;
@ -433,7 +436,8 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
kind = `${valty}.ne`; kind = `${valty}.ne`;
break; 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") { } else if (lhsTy.kind === "bool" && rhsTy.kind === "bool") {
let kind: wasm.Instr["kind"]; let kind: wasm.Instr["kind"];
@ -458,6 +462,7 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
case "-": case "-":
case "*": case "*":
case "/": case "/":
case "%":
throw new Error(`Invalid bool binary expr: ${expr.binaryKind}`); throw new Error(`Invalid bool binary expr: ${expr.binaryKind}`);
} }