impl - * // /

This commit is contained in:
Tangent Wantwight 2024-05-17 20:52:33 -04:00
parent bf44d84e17
commit 1a46130b1d
2 changed files with 30 additions and 4 deletions

View file

@ -5,10 +5,10 @@ describe("expr", () => {
test.each([
["1", "1"],
["1 + 2", "3"],
// ["1 - 2", "-1"],
// ["1 * 2", "2"],
// ["1 / 2", "0.5"],
// ["1 // 2", "0"],
["1 - 2", "-1"],
["1 * 2", "2"],
["1 / 2", "0.5"],
["1 // 2", "0"],
// TODO: operator precedence
// TODO: parentheses
// TODO; eror reporting

View file

@ -66,6 +66,32 @@ const Operators: TokenHandler[] = [
parse: ({ value: left }, matched, parser) =>
map(parser.parseSubExpression(11), (right) => ({ value: left + right })),
},
{
leftBindingPower: 10,
token: MINUS_TOKEN,
parse: ({ value: left }, matched, parser) =>
map(parser.parseSubExpression(11), (right) => ({ value: left - right })),
},
{
leftBindingPower: 20,
token: TIMES_TOKEN,
parse: ({ value: left }, matched, parser) =>
map(parser.parseSubExpression(11), (right) => ({ value: left * right })),
},
{
leftBindingPower: 20,
token: FLOOR_TOKEN,
parse: ({ value: left }, matched, parser) =>
map(parser.parseSubExpression(11), (right) => ({
value: Math.floor(left / right),
})),
},
{
leftBindingPower: 20,
token: DIV_TOKEN,
parse: ({ value: left }, matched, parser) =>
map(parser.parseSubExpression(11), (right) => ({ value: left / right })),
},
];
const ZERO = { value: 0 };