m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
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 {