diff --git a/src/lib/expr.test.ts b/src/lib/expr.test.ts index a4917c9..e998605 100644 --- a/src/lib/expr.test.ts +++ b/src/lib/expr.test.ts @@ -5,6 +5,7 @@ describe("expr", () => { describe("Expr does math", () => { test.each([ ["1", "1"], + ["-1", "-1"], ["1 + 2", "3"], ["1 - 2", "-1"], ["1 * 2", "2"], diff --git a/src/lib/expr.ts b/src/lib/expr.ts index 4af04a1..4bd6b09 100644 --- a/src/lib/expr.ts +++ b/src/lib/expr.ts @@ -73,6 +73,12 @@ const Operators: TokenHandler[] = [ token: NUMBER_TOKEN, parse: (left, matched) => ({ value: Number(matched) }), }, + { + leftBindingPower: -1, + token: MINUS_TOKEN, + parse: (left, matched, parser) => + map(parser.parseSubExpression(0), (right) => ({value: -right})), + }, makeInfixOp(PLUS_TOKEN, 10, 11, (left, right) => ({ value: left + right })), makeInfixOp(MINUS_TOKEN, 10, 11, (left, right) => ({ value: left - right })), makeInfixOp(TIMES_TOKEN, 10, 11, (left, right) => ({ value: left * right })),