diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dice.js | 14 | ||||
-rw-r--r-- | src/lexer.js | 1 | ||||
-rw-r--r-- | src/parser.js | 2 |
3 files changed, 17 insertions, 0 deletions
diff --git a/src/dice.js b/src/dice.js index d47455e..5a484b0 100644 --- a/src/dice.js +++ b/src/dice.js @@ -193,6 +193,19 @@ const thresholdLow = (die1, die2) => { return threshold(lessThanOrEqual, die1, die2) } +const repeat = (die1, die2) => { + return () => { + const times = roll(die2) + + let results = [] + for (let i = 0; i < times; i++) { + results = results.concat(die1()) + } + + return results + } +} + exports.pool = pool exports.roll = roll exports.constant = constant @@ -212,3 +225,4 @@ exports.againAbove = againAbove exports.againUnder = againUnder exports.thresholdHigh = thresholdHigh exports.thresholdLow = thresholdLow +exports.repeat = repeat diff --git a/src/lexer.js b/src/lexer.js index 59fba16..248a9b2 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -32,6 +32,7 @@ newLexemeType('A', 'A') newLexemeType('a', 'a') newLexemeType('T', 'T') newLexemeType('t', 't') +newLexemeType(' x ', ' x ') const lex = (expressionString) => { let lexemes = [] diff --git a/src/parser.js b/src/parser.js index fd0fd59..b1f6ebb 100644 --- a/src/parser.js +++ b/src/parser.js @@ -82,6 +82,8 @@ newSymbol('minus', (parser) => { } }) +newInfix(' x ', 24, { type: 'repeat' }) + newSymbol('end', null, -1) const newParser = (tokens) => { |