From a15a2986c95131a13d3707ae50f8a215dcaac564 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 14 Jul 2017 18:13:21 -0400 Subject: Lex and parse addition --- __tests__/parser.test.js | 24 ++++++++++++++++++++++++ src/parser.js | 8 ++++++++ 2 files changed, 32 insertions(+) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index 004e18b..b0dc28d 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -32,4 +32,28 @@ describe('parse', () => { } }) }) + + it('parses constant addition', () => { + expect(parse('1 + 2')).toEqual({ + type: 'add', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 2 } + }) + }) + + it('parses dice addition', () => { + expect(parse('1d6 + 2d8')).toEqual({ + type: 'add', + left: { + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 8 } + } + }) + }) }) diff --git a/src/parser.js b/src/parser.js index 01b5494..c7d91c3 100644 --- a/src/parser.js +++ b/src/parser.js @@ -24,6 +24,14 @@ newSymbol('d', null, 30, (left, parser) => { } }) +newSymbol('+', null, 20, (left, parser) => { + return { + type: 'add', + left: left, + right: parser.expression(20) + } +}) + newSymbol('end', null, -1) const newParser = (tokens) => { -- cgit v1.2.3