impl unary minus

This commit is contained in:
Tangent Wantwight 2024-05-18 21:53:23 -04:00
parent 5a34b0f6f7
commit 2770a2de3b
2 changed files with 7 additions and 0 deletions

View file

@ -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"],

View file

@ -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 })),