m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-09-11 15:47:50 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-09-11 15:47:50 -0400
commit9368b1c33db290d7c80c93db5c867cdb7fcaf14d (patch)
treec6170c157985624b373a13bb540bd4070321c18c /src
parentda6582f328c17e1842a4632d88e06d851e7c4b5f (diff)
Implement collecting
Diffstat (limited to 'src')
-rw-r--r--src/dice.js5
-rw-r--r--src/lexer.js2
-rw-r--r--src/parser.js12
3 files changed, 19 insertions, 0 deletions
diff --git a/src/dice.js b/src/dice.js
index 0197f25..1e15937 100644
--- a/src/dice.js
+++ b/src/dice.js
@@ -222,6 +222,10 @@ const repeat = (die1, die2) => {
}
}
+const collect = die => {
+ return () => [() => roll(die)]
+}
+
exports.pool = pool
exports.roll = roll
exports.constant = constant
@@ -244,3 +248,4 @@ exports.againUnder = againUnder
exports.thresholdHigh = thresholdHigh
exports.thresholdLow = thresholdLow
exports.repeat = repeat
+exports.collect = collect
diff --git a/src/lexer.js b/src/lexer.js
index 6b75129..88bd188 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -26,6 +26,8 @@ newLexemeType('times', '\\*')
newLexemeType('divide', '/')
newLexemeType('(', '\\(')
newLexemeType(')', '\\)')
+newLexemeType('[', '\\[')
+newLexemeType(']', '\\]')
newLexemeType('E', 'E')
newLexemeType('e', 'e')
newLexemeType('K', 'K')
diff --git a/src/parser.js b/src/parser.js
index acae9ad..3cf6d55 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -44,6 +44,16 @@ newSymbol('constant', function() {
return { type: 'constant', value: this.value }
})
+const newBrackets = (openSymbol, closeSymbol, valueWrapper) => {
+ newSymbol(openSymbol, function(parser) {
+ const value = parser.expression(1)
+ parser.match(closeSymbol)
+ return valueWrapper(value)
+ })
+
+ newSymbol(closeSymbol)
+}
+
newSymbol('(', function(parser) {
const value = parser.expression(1)
parser.match(')')
@@ -52,6 +62,8 @@ newSymbol('(', function(parser) {
newSymbol(')')
+newBrackets('[', ']', value => ({ type: 'collect', value: value }))
+
newDieOperation('d')
newSymbol('d', (parser) => {
return {