diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-08-23 20:47:54 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-08-23 20:47:54 -0400 |
commit | 3a99beea32cda8850022458f39ad640966d1477d (patch) | |
tree | 474826b6c4eb72c54ba10f0167cedc73dffa03c6 | |
parent | d6051158e52db719ae506fc48a45d22975b16a8b (diff) |
Lex and parse again
-rw-r--r-- | __tests__/lexer.test.js | 12 | ||||
-rw-r--r-- | __tests__/parser.test.js | 12 | ||||
-rw-r--r-- | index.js | 2 | ||||
-rw-r--r-- | src/lexer.js | 1 | ||||
-rw-r--r-- | src/parser.js | 1 |
5 files changed, 28 insertions, 0 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index 1e80c25..5ae84cc 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -182,4 +182,16 @@ describe('lex', () => { ]) }) }) + + describe('again', () => { + test('6A3d6', () => { + expect(lex('6A3d6')).toEqual([ + { type: 'constant', value: 6 }, + { type: 'A' }, + { type: 'constant', value: 3 }, + { type: 'd' }, + { type: 'constant', value: 6 } + ]) + }) + }) }) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index 25edbfc..6bf5958 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -199,6 +199,18 @@ describe('parse', () => { }) }) + it('parases dice with again', () => { + expect(parse('10A3d10')).toEqual({ + type: 'A', + left: { type: 'constant', value: 10 }, + right: { + type: 'd', + left: { type: 'constant', value: 3 }, + right: { type: 'constant', value: 10 } + } + }) + }) + describe('parsing parentheses', () => { test('(1d6)d6', () => { expect(parse('(1d6)d6')).toEqual({ @@ -13,6 +13,8 @@ const interpret = tree => { return D.keepHigh(interpret(tree.left), interpret(tree.right)) case 'k': return D.keepLow(interpret(tree.left), interpret(tree.right)) + case 'A': + return D.again(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 b95ebc5..a83ee2b 100644 --- a/src/lexer.js +++ b/src/lexer.js @@ -25,6 +25,7 @@ newLexemeType(')', '\\)') newLexemeType('E', 'E') newLexemeType('K', 'K') newLexemeType('k', 'k') +newLexemeType('A', 'A') const lex = (expressionString) => { let lexemes = [] diff --git a/src/parser.js b/src/parser.js index a4665ac..1b678e4 100644 --- a/src/parser.js +++ b/src/parser.js @@ -63,6 +63,7 @@ newSymbol('d', (parser) => { newDieOperation('E') newDieOperation('K') newDieOperation('k') +newDieOperation('A') newInfix('bigPlus', 20, { type: 'add' }) newInfix('bigMinus', 20, { type: 'subtract' }) |