From 80df0f9425e8b64b8a3fcca308cb784d02d52f62 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sat, 15 Jul 2017 23:04:50 -0400 Subject: Implement the unary 'd' operation --- __tests__/parser.test.js | 21 ++++++++++++++------- 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, -- cgit v1.2.3