impl modulo (handle negative divisors how people expect)
This commit is contained in:
parent
2b6aff0dab
commit
ce991fd12a
2 changed files with 7 additions and 0 deletions
src/lib
|
@ -32,6 +32,7 @@ const MINUS_TOKEN = /\s*(\-)/y;
|
|||
const TIMES_TOKEN = /\s*(\*)/y;
|
||||
const FLOOR_TOKEN = /\s*(\/\/)/y;
|
||||
const DIV_TOKEN = /\s*(\/)/y;
|
||||
const MOD_TOKEN = /\s*(\%)/y;
|
||||
|
||||
const NUMBER_TOKEN = /\s*(\d+)/y;
|
||||
|
||||
|
@ -84,6 +85,7 @@ const Operators: TokenHandler[] = [
|
|||
makeInfixOp(TIMES_TOKEN, 20, 21, (left, right) => ({ value: left * right })),
|
||||
makeInfixOp(FLOOR_TOKEN, 20, 21, (left, right) => ({ value: Math.floor(left / right) })),
|
||||
makeInfixOp(DIV_TOKEN, 20, 21, (left, right) => ({ value: left / right })),
|
||||
makeInfixOp(MOD_TOKEN, 20, 21, (left, right) => ({ value: ((left % right) + right) % right })),
|
||||
];
|
||||
|
||||
const ZERO = { value: 0 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue