impl unary minus
This commit is contained in:
parent
5a34b0f6f7
commit
2770a2de3b
2 changed files with 7 additions and 0 deletions
|
@ -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"],
|
||||
|
|
|
@ -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 })),
|
||||
|
|
Loading…
Reference in a new issue