m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--__tests__/lexer.test.js12
-rw-r--r--__tests__/parser.test.js12
-rw-r--r--index.js2
-rw-r--r--src/lexer.js1
-rw-r--r--src/parser.js1
5 files changed, 28 insertions, 0 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index 6900764..d7a4368 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -161,4 +161,16 @@ describe('lex', () => {
])
})
})
+
+ describe('bonusAdd', () => {
+ test('3d4+1', () => {
+ expect(lex('3d4+1')).toEqual([
+ { type: 'constant', value: 3 },
+ { type: 'd' },
+ { type: 'constant', value: 4 },
+ { type: 'plus' },
+ { type: 'constant', value: 1 }
+ ])
+ })
+ })
})
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index bc88894..350af03 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -139,6 +139,18 @@ describe('parse', () => {
})
})
+ it('parses additive bonuses', () => {
+ expect(parse('3d4+1')).toEqual({
+ type: 'bonusAdd',
+ left: {
+ type: 'd',
+ left: { type: 'constant', value: 3 },
+ right: { type: 'constant', value: 4 }
+ },
+ right: { type: 'constant', value: 1 }
+ })
+ })
+
describe('parsing parentheses', () => {
test('(1d6)d6', () => {
expect(parse('(1d6)d6')).toEqual({
diff --git a/index.js b/index.js
index 4a58e4a..2ddad5b 100644
--- a/index.js
+++ b/index.js
@@ -17,6 +17,8 @@ const interpret = tree => {
return D.subtract(interpret(tree.left), interpret(tree.right))
case 'negative':
return D.negative(interpret(tree.value))
+ case 'bonusAdd':
+ return D.bonusAdd(interpret(tree.left), interpret(tree.right))
}
}
diff --git a/src/lexer.js b/src/lexer.js
index 8635e4a..2b3b25a 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -18,6 +18,7 @@ newValueLexeme('constant', '\\d+', Number)
newLexemeType('d', 'd')
newLexemeType('bigPlus', ' \\+ ' )
newLexemeType('bigMinus', ' - ')
+newLexemeType('plus', '\\+')
newLexemeType('minus', '-')
newLexemeType('(', '\\(')
newLexemeType(')', '\\)')
diff --git a/src/parser.js b/src/parser.js
index 588f480..c55fee6 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -64,6 +64,7 @@ newDieOperation('E')
newDieOperation('K')
newInfix('bigPlus', 20, { type: 'add' })
+newInfix('plus', 20, { type: 'bonusAdd' })
newInfix('bigMinus', 20, { type: 'subtract' })
newSymbol('minus', (parser) => {
return {