From 13e482fefb3090645a941a643c73eadcbf0a1a34 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Wed, 30 Aug 2017 18:27:55 -0400 Subject: Implement division --- README.md | 8 +++++++- __tests__/dice.test.js | 22 ++++++++++++++++++++++ __tests__/lexer.test.js | 24 ++++++++++++++++++++++++ __tests__/parser.test.js | 16 ++++++++++++++++ index.js | 2 ++ src/dice.js | 7 +++++++ src/lexer.js | 1 + src/parser.js | 1 + 8 files changed, 80 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aecf2fe..c140d91 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ The parser recognizes the following grammar: | Die ' + ' Die | Die ' - ' Die | Die ' * ' Die + | Die ' / ' Die | Die '+' Die | Die '-' Die | '-' Die @@ -109,6 +110,8 @@ Semantics are defined in terms of the `pool` function. * `D - E` is equivalent to `D + (-E)`. * `D * E` generates a dicepool with a single value - the produc of `roll(D)` and `roll(E)`. +* `D / E` generates a dicepool with a single value - the result of integer + division of `roll(D)` and `roll(E)`. * `D+E` is the additive bonus operation. For each die in `D`'s pool, the die is rolled and `roll(E)` is added to its result. * `D-E` is equivalent to `D+(-E)`. @@ -140,7 +143,10 @@ Semantics are defined in terms of the `pool` function. Additionally: -* The binary arithmetic operations (` + `, ` - `) are left associative. +* The binary arithmetic operations (` + `, ` - `, ` * `, ` / `) are left + associative. +* The binary arithmetic operations bind as expected (multiplication and division + bind stronger than addition and subtraction). * The bonus operations (`+`, `-`) are left associative. They bind stronger than arithmetic operations. * The die operations (`d`, `E`, `K`, etc.) are right associative (`1d2d3` is diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 4e89031..d397d4c 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -5,6 +5,7 @@ const { add, subtract, multiply, + divide, bonusAdd, bonusSubtract, negative, @@ -338,6 +339,27 @@ describe('multiply', () => { }) }) +describe('divide', () => { + describe('1d8 / 2', () => { + const die = divide(d(constant(1), constant(8)), constant(2)) + testDie(die, { + diceCount: 1, + average: { + average: 2 + }, + variance: { + variance: 1.5 + }, + bounds: { + low: 0, + high: 4, + expectLow: true, + expectHigh: true + } + }) + }) +}) + describe('bonusAdd', () => { describe('1d20+3', () => { diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index db7ab50..0c615d5 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -121,6 +121,30 @@ describe('lex', () => { }) }) + describe('lexes division', () => { + it('1d6 / 1d4', () => { + expect(lex('1d6 / 1d4')).toEqual([ + { type: 'constant', value: 1 }, + { type: 'd' }, + { type: 'constant', value: 6 }, + { type: 'bigDivide' }, + { type: 'constant', value: 1 }, + { type: 'd' }, + { type: 'constant', value: 4 } + ]) + }) + + it('2d17 / 4', () => { + expect(lex('2d17 / 4')).toEqual([ + { type: 'constant', value: 2 }, + { type: 'd' }, + { type: 'constant', value: 17 }, + { type: 'bigDivide' }, + { type: 'constant', value: 4 } + ]) + }) + }) + describe('lexes parentheses', () => { it('(1d6)d6', () => { expect(lex('(1d6)d6')).toEqual([ diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index 5d4288d..bef11c5 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -113,6 +113,22 @@ describe('parse', () => { }) }) + it('parses dice division', () => { + expect(parse('1d6 / 2d8')).toEqual({ + type: 'divide', + left: { + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 8 } + } + }) + }) + it('parses additive bonuses', () => { expect(parse('3d4+1')).toEqual({ type: 'bonusAdd', diff --git a/index.js b/index.js index 9a4534b..37883cc 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,8 @@ const interpret = tree => { return D.subtract(interpret(tree.left), interpret(tree.right)) case 'multiply': return D.multiply(interpret(tree.left), interpret(tree.right)) + case 'divide': + return D.divide(interpret(tree.left), interpret(tree.right)) case 'negative': return D.negative(interpret(tree.value)) case 'bonusAdd': diff --git a/src/dice.js b/src/dice.js index abe3c4b..d47455e 100644 --- a/src/dice.js +++ b/src/dice.js @@ -50,6 +50,12 @@ const multiply = (die1, die2) => { } } +const divide = (die1, die2) => { + return () => { + return [() => Math.floor(roll(die1) / roll(die2))] + } +} + const bonusAdd = (die1, die2) => { return () => { return die1().map(die => { @@ -194,6 +200,7 @@ exports.d = d exports.add = add exports.subtract = subtract exports.multiply = multiply +exports.divide = divide exports.bonusAdd = bonusAdd exports.bonusSubtract = bonusSubtract exports.negative = negative diff --git a/src/lexer.js b/src/lexer.js index c3eec8a..59fba16 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -19,6 +19,7 @@ newLexemeType('d', 'd') newLexemeType('bigPlus', ' \\+ ' ) newLexemeType('bigMinus', ' - ') newLexemeType('bigTimes', ' \\* ') +newLexemeType('bigDivide', ' / ') newLexemeType('plus', '\\+') newLexemeType('minus', '-') newLexemeType('(', '\\(') diff --git a/src/parser.js b/src/parser.js index 1a1e5d4..fd0fd59 100644 --- a/src/parser.js +++ b/src/parser.js @@ -72,6 +72,7 @@ newDieOperation('t') newInfix('bigPlus', 20, { type: 'add' }) newInfix('bigMinus', 20, { type: 'subtract' }) newInfix('bigTimes', 23, { type: 'multiply' }) +newInfix('bigDivide', 23, { type: 'divide' }) newInfix('plus', 25, { type: 'bonusAdd' }) newInfix('minus', 25, { type: 'bonusSubtract' }) newSymbol('minus', (parser) => { -- cgit v1.2.3