m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__tests__/lexer.test.js9
-rw-r--r--__tests__/parser.test.js14
-rw-r--r--index.js2
-rw-r--r--src/lexer.js1
-rw-r--r--src/parser.js1
5 files changed, 26 insertions, 1 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index d7a4368..1e80c25 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -160,6 +160,15 @@ describe('lex', () => {
{ type: 'constant', value: 20 }
])
})
+ test('1k2d20', () => {
+ expect(lex('1k2d20')).toEqual([
+ { type: 'constant', value: 1 },
+ { type: 'k' },
+ { type: 'constant', value: 2 },
+ { type: 'd' },
+ { type: 'constant', value: 20 }
+ ])
+ })
})
describe('bonusAdd', () => {
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index 350af03..873972b 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -127,7 +127,7 @@ describe('parse', () => {
})
})
- it('parses dice with keep', () => {
+ it('parses dice with keep high', () => {
expect(parse('1K2d20')).toEqual({
type: 'K',
left: { type: 'constant', value: 1 },
@@ -139,6 +139,18 @@ describe('parse', () => {
})
})
+ it('parses dice with keep low', () => {
+ expect(parse('1k2d20')).toEqual({
+ type: 'k',
+ left: { type: 'constant', value: 1 },
+ right: {
+ type: 'd',
+ left: { type: 'constant', value: 2 },
+ right: { type: 'constant', value: 20 }
+ }
+ })
+ })
+
it('parses additive bonuses', () => {
expect(parse('3d4+1')).toEqual({
type: 'bonusAdd',
diff --git a/index.js b/index.js
index 2ddad5b..980c6b4 100644
--- a/index.js
+++ b/index.js
@@ -11,6 +11,8 @@ const interpret = tree => {
return D.explode(interpret(tree.left), interpret(tree.right))
case 'K':
return D.keepHigh(interpret(tree.left), interpret(tree.right))
+ case 'k':
+ return D.keepLow(interpret(tree.left), interpret(tree.right))
case 'add':
return D.add(interpret(tree.left), interpret(tree.right))
case 'subtract':
diff --git a/src/lexer.js b/src/lexer.js
index 2b3b25a..b95ebc5 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -24,6 +24,7 @@ newLexemeType('(', '\\(')
newLexemeType(')', '\\)')
newLexemeType('E', 'E')
newLexemeType('K', 'K')
+newLexemeType('k', 'k')
const lex = (expressionString) => {
let lexemes = []
diff --git a/src/parser.js b/src/parser.js
index c55fee6..48de2ab 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -62,6 +62,7 @@ newSymbol('d', (parser) => {
})
newDieOperation('E')
newDieOperation('K')
+newDieOperation('k')
newInfix('bigPlus', 20, { type: 'add' })
newInfix('plus', 20, { type: 'bonusAdd' })