m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-08-23 21:30:01 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-08-23 21:30:01 -0400
commitfe1bb829ba1feeb4d8f384afe0ed62669d82407f (patch)
tree667520ac5019a24398255086d34460517421b8c8
parent5bb027bde84e0a8fdb01bfa680b511ad842498ef (diff)
Lex and parse threshold
-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 5ae84cc..21ae3b0 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -194,4 +194,16 @@ describe('lex', () => {
])
})
})
+
+ describe('threshold', () => {
+ test('8T3d10', () => {
+ expect(lex('8T3d10')).toEqual([
+ { type: 'constant', value: 8 },
+ { type: 'T' },
+ { type: 'constant', value: 3 },
+ { type: 'd' },
+ { type: 'constant', value: 10 }
+ ])
+ })
+ })
})
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index 6bf5958..70ad98d 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -211,6 +211,18 @@ describe('parse', () => {
})
})
+ it('parases dice with threshold', () => {
+ expect(parse('7T4d8')).toEqual({
+ type: 'T',
+ left: { type: 'constant', value: 7 },
+ right: {
+ type: 'd',
+ left: { type: 'constant', value: 4 },
+ right: { type: 'constant', value: 8 }
+ }
+ })
+ })
+
describe('parsing parentheses', () => {
test('(1d6)d6', () => {
expect(parse('(1d6)d6')).toEqual({
diff --git a/index.js b/index.js
index fa4a993..e117b7c 100644
--- a/index.js
+++ b/index.js
@@ -15,6 +15,8 @@ const interpret = tree => {
return D.keepLow(interpret(tree.left), interpret(tree.right))
case 'A':
return D.again(interpret(tree.left), interpret(tree.right))
+ case 'T':
+ return D.threshold(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 a83ee2b..f316147 100644
--- a/src/lexer.js
+++ b/src/lexer.js
@@ -26,6 +26,7 @@ newLexemeType('E', 'E')
newLexemeType('K', 'K')
newLexemeType('k', 'k')
newLexemeType('A', 'A')
+newLexemeType('T', 'T')
const lex = (expressionString) => {
let lexemes = []
diff --git a/src/parser.js b/src/parser.js
index 1b678e4..0201d05 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -64,6 +64,7 @@ newDieOperation('E')
newDieOperation('K')
newDieOperation('k')
newDieOperation('A')
+newDieOperation('T')
newInfix('bigPlus', 20, { type: 'add' })
newInfix('bigMinus', 20, { type: 'subtract' })