diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-15 23:04:50 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-15 23:09:26 -0400 |
commit | 80df0f9425e8b64b8a3fcca308cb784d02d52f62 (patch) | |
tree | 9135b9cae1c34e6851f8c3bcfa5b59b8a33ac861 | |
parent | e0ab43bcdc050ca3cac2adea927817d99700737e (diff) |
Implement the unary 'd' operation
-rw-r--r-- | __tests__/parser.test.js | 21 | ||||
-rw-r--r-- | src/parser.js | 8 |
2 files changed, 21 insertions, 8 deletions
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index f3c6e97..47944d2 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -8,8 +8,7 @@ describe('parse', () => { }) it('throws when two binary operations follow each other', () => { - expect(() => { parse('1dd2') }).toThrow(/Syntax error/) - expect(() => { parse('1+d2') }).toThrow(/Syntax error/) + expect(() => { parse('1++2') }).toThrow(/Syntax error/) }) it('throws when two dice not combined with a binary operation', () => { @@ -22,6 +21,14 @@ describe('parse', () => { expect(parse('5')).toEqual({ type: 'constant', value: 5 }) }) + it('parses a simple die with no left side', () => { + expect(parse('d6')).toEqual({ + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } + }) + }) + it('parses a simple die (1d6)', () => { expect(parse('1d6')).toEqual({ type: 'd', @@ -59,17 +66,17 @@ describe('parse', () => { }) it('parses dice addition', () => { - expect(parse('1d6 + 2d8')).toEqual({ + expect(parse('2d8 + d6')).toEqual({ type: 'add', left: { type: 'd', - left: { type: 'constant', value: 1 }, - right: { type: 'constant', value: 6 } + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 8 } }, right: { type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 8 } + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } } }) }) diff --git a/src/parser.js b/src/parser.js index fc9add3..3437f1a 100644 --- a/src/parser.js +++ b/src/parser.js @@ -33,7 +33,13 @@ newSymbol('(', function(parser) { newSymbol(')') -newSymbol('d', null, 30, (left, parser) => { +newSymbol('d', (parser) => { + return { + type: 'd', + left: { type: 'constant', value: 1 }, + right: parser.expression(29) + } +}, 30, (left, parser) => { return { type: 'd', left: left, |