m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-09-02 00:47:46 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-09-02 01:02:16 -0400
commit1cc6b80cfd4875f289837421cdbe81e3dfcf0ed1 (patch)
tree0fb04bc04f27620f6def5dc310953a637e22ae07 /src
parent650f7d82197cba35df3654abf548e07e213c1b14 (diff)
Implement repeat
Diffstat (limited to 'src')
-rw-r--r--src/dice.js14
-rw-r--r--src/lexer.js1
-rw-r--r--src/parser.js2
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) => {