Repository Public
1import { describe, expect, it } from "vitest";2import { evaluateExpr, ExprError, MAX_EXPR_LENGTH, parseExpr, tryParseExpr } from "@/sim/core/expr";34const ev = (src: string, scope: Record<string, number> = {}) => evaluateExpr(parseExpr(src), scope);5const err = (src: string): string => { const r = tryParseExpr(src); return r.ok ? "" : r.error; };67describe("expression parser", () => {8 it("respects operator precedence and associativity", () => {9 expect(ev("1 + 2 * 3")).toBe(7);10 expect(ev("(1 + 2) * 3")).toBe(9);11 expect(ev("10 - 4 - 3")).toBe(3);12 expect(ev("24 / 4 / 2")).toBe(3);13 expect(ev("2 ^ 3 ^ 2")).toBe(512);14 expect(ev("-2 ^ 2")).toBe(-4);15 expect(ev("(-2) ^ 2")).toBe(4);16 expect(ev("2 ^ -1")).toBe(0.5);17 expect(ev("2 * -3")).toBe(-6);18 expect(ev("--3")).toBe(3);19 expect(ev("+3 - -3")).toBe(6);20 expect(ev("1 + 2 ^ 2 * 3")).toBe(13);21 });22 it("reads numbers, constants, x and named parameters", () => {23 expect(ev("1.5e2 + .5")).toBe(150.5);24 expect(err("2e")).toMatch(/unexpected "e"/);25 expect(ev("2 * e")).toBe(2 * Math.E);26 expect(ev("pi")).toBe(Math.PI);27 expect(ev("e ^ 1")).toBe(Math.E);28 expect(ev("a * x ^ 2 + b", { a: 2, x: 3, b: 1 })).toBe(19);29 expect(parseExpr("a * sin(w * x) + a + pi").variables).toEqual(["a", "w", "x"]);30 expect(ev("x2 + 1", { x2: 4 })).toBe(5);31 });32 it("evaluates the function list", () => {33 expect(ev("exp(0) + log(e) + ln(e) + log10(1000)")).toBeCloseTo(6, 12);34 expect(ev("sqrt(16) + abs(-2)")).toBe(6);35 expect(ev("sin(0) + cos(0) + tan(0) + tanh(0)")).toBe(1);36 expect(ev("sigmoid(0)")).toBe(0.5);37 expect(ev("relu(-3) + relu(3)")).toBe(3);38 expect(ev("max(1, 5, 3) - min(4, 2)")).toBe(3);39 expect(ev("pow(2, 10)")).toBe(1024);40 expect(ev("floor(2.7) + floor(-2.2)")).toBe(-1);41 expect(ev("max(x, 0)", { x: -2 })).toBe(0);42 });43 it("turns non-finite results into gaps (NaN)", () => {44 expect(ev("1 / x", { x: 0 })).toBeNaN();45 expect(ev("log(x)", { x: -1 })).toBeNaN();46 expect(ev("sqrt(x)", { x: -4 })).toBeNaN();47 expect(ev("exp(x)", { x: 1e6 })).toBeNaN();48 expect(ev("1 / (1 + exp(x))", { x: 1e6 })).toBe(0);49 });50 it("rejects anything that is not arithmetic", () => {51 expect(err("constructor")).toMatch(/not allowed/);52 expect(err("x.constructor")).toMatch(/property access|not allowed/);53 expect(err("x.y")).toMatch(/property access/);54 expect(err("__proto__")).toMatch(/letters and digits/);55 expect(err("a.__proto__")).toMatch(/property access/);56 expect(err("this")).toMatch(/not allowed/);57 expect(err("eval(1)")).toMatch(/not allowed/);58 expect(err("Function(1)")).toMatch(/not allowed/);59 expect(err("alert(1)")).toMatch(/unknown function "alert"/);60 expect(err("foo(x)")).toMatch(/unknown function "foo"/);61 expect(err("x[0]")).toMatch(/unexpected character "\["/);62 expect(err("'a'")).toMatch(/unexpected character/);63 expect(err("x = 1")).toMatch(/unexpected character "="/);64 expect(err("x; y")).toMatch(/unexpected character ";"/);65 expect(err("`x`")).toMatch(/unexpected character/);66 expect(err("x => x")).toMatch(/unexpected character/);67 expect(err("$x")).toMatch(/letters and digits/);68 });69 it("reports syntax errors clearly", () => {70 expect(err("")).toMatch(/empty/);71 expect(err(" ")).toMatch(/empty/);72 expect(err("1 +")).toMatch(/unexpected end of expression/);73 expect(err("(1 + 2")).toMatch(/expected "\)"/);74 expect(err("1 2")).toMatch(/unexpected "2" at position 3/);75 expect(err("2 x")).toMatch(/unexpected "x"/);76 expect(err("sin")).toMatch(/is a function/);77 expect(err("sin()")).toMatch(/takes 1 argument, got 0/);78 expect(err("pow(1)")).toMatch(/takes 2 arguments, got 1/);79 expect(err("sin(1, 2)")).toMatch(/takes 1 argument, got 2/);80 expect(err("1 * * 2")).toMatch(/unexpected "\*"/);81 expect(() => parseExpr("1 +")).toThrow(ExprError);82 });83 it("caps length and nesting depth", () => {84 expect(err("1+".repeat(200) + "1")).toMatch(/too long/);85 expect(err("x".repeat(MAX_EXPR_LENGTH + 1))).toMatch(/too long/);86 expect(err("(".repeat(60) + "x" + ")".repeat(60))).toMatch(/nested too deeply/);87 expect(err("-".repeat(100) + "x")).toMatch(/nested too deeply/);88 expect(tryParseExpr("1+".repeat(140) + "1").ok).toBe(true); // long but flat: fine89 expect(tryParseExpr("((((((((x))))))))").ok).toBe(true);90 });91 it("refuses to evaluate a name that has no value, and never reads inherited properties", () => {92 expect(() => ev("a + 1")).toThrow(/no value for "a"/);93 expect(() => ev("toFixed + 1")).toThrow(/no value/);94 expect(() => evaluateExpr(parseExpr("hasOwn + 1"), Object.create({ hasOwn: 5 }) as Record<string, number>)).toThrow(/no value/);95 });96});97