spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[ParseEngine] starting work on processing simple multiplication, eg: 5*5
author
Ben Vogt <[email protected]>
date
2017-12-09 22:36:12
stats
2 file(s) changed, 36 insertions(+), 20 deletions(-)
files
src/Parser/ParseEngine.ts
tests/Parser/ParseEngineTest.ts
  1diff --git a/src/Parser/ParseEngine.ts b/src/Parser/ParseEngine.ts
  2index 42f5c22..9257846 100644
  3--- a/src/Parser/ParseEngine.ts
  4+++ b/src/Parser/ParseEngine.ts
  5@@ -328,7 +328,8 @@ const enum ReduceActions {
  6   INVERT_NUMBER = 29,
  7   EXPRESSION = 30,
  8   AS_ARRAY = 31,
  9-  REFLEXIVE_REDUCE = 31
 10+  REFLEXIVE_REDUCE = 31,
 11+  RETURN_LAST_AS_NUMBER = 32
 12 };
 13 
 14 /**
 15@@ -388,7 +389,8 @@ const enum Tree {
 16   // CLOSE_ARRAY = 26,
 17   INVERT_NEXT = 27,
 18   EXPRESSION = 28,
 19-  TERMINATE = 29
 20+  TERMINATE_NUMBER = 29,
 21+  TERMINATE = 30
 22 }
 23 
 24 /**
 25@@ -403,30 +405,31 @@ productions[ReduceActions.NO_ACTION] = null;
 26 productions[ReduceActions.RETURN_LAST] = new ReductionPair(Tree.NUMBER, 2);
 27 productions[ReduceActions.CALL_VARIABLE] = new ReductionPair(Tree.VARIABLE, 1);
 28 productions[ReduceActions.AS_NUMBER] = new ReductionPair(Tree.NUMBER, 1);
 29-productions[ReduceActions.INVERT_NUMBER] = new ReductionPair(Tree.VARIABLE, 1);
 30+productions[ReduceActions.INVERT_NUMBER] = new ReductionPair(Tree.NUMBER, 1);
 31 productions[ReduceActions.AS_STRING] = new ReductionPair(Tree.VARIABLE, 1);
 32 productions[ReduceActions.AMPERSAND] = new ReductionPair(Tree.AMPERSAND, 3);
 33 productions[ReduceActions.EQUALS] = new ReductionPair(Tree.EQUALS, 3);
 34 productions[ReduceActions.PLUS] = new ReductionPair(Tree.PLUS, 3);
 35 productions[ReduceActions.LAST_NUMBER] = new ReductionPair(Tree.NUMBER, 3);
 36-productions[ReduceActions.LTE] = new ReductionPair(Tree.VARIABLE, 4);
 37-productions[ReduceActions.GTE] = new ReductionPair(Tree.VARIABLE, 4);
 38-productions[ReduceActions.NOT_EQ] = new ReductionPair(Tree.VARIABLE, 4);
 39-productions[ReduceActions.GT] = new ReductionPair(Tree.VARIABLE, 3);
 40-productions[ReduceActions.LT] = new ReductionPair(Tree.VARIABLE, 3);
 41-productions[ReduceActions.MINUS] = new ReductionPair(Tree.VARIABLE, 3);
 42-productions[ReduceActions.MULTIPLY] = new ReductionPair(Tree.VARIABLE, 3);
 43-productions[ReduceActions.DIVIDE] = new ReductionPair(Tree.VARIABLE, 3);
 44-productions[ReduceActions.TO_POWER] = new ReductionPair(Tree.VARIABLE, 3);
 45-productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(Tree.VARIABLE, 2);
 46+productions[ReduceActions.LTE] = new ReductionPair(Tree.NUMBER, 4);
 47+productions[ReduceActions.GTE] = new ReductionPair(Tree.NUMBER, 4);
 48+productions[ReduceActions.NOT_EQ] = new ReductionPair(Tree.NUMBER, 4);
 49+productions[ReduceActions.GT] = new ReductionPair(Tree.NUMBER, 3);
 50+productions[ReduceActions.LT] = new ReductionPair(Tree.NUMBER, 3);
 51+productions[ReduceActions.MINUS] = new ReductionPair(Tree.NUMBER, 3);
 52+productions[ReduceActions.MULTIPLY] = new ReductionPair(Tree.NUMBER, 3);
 53+productions[ReduceActions.DIVIDE] = new ReductionPair(Tree.NUMBER, 3);
 54+productions[ReduceActions.TO_POWER] = new ReductionPair(Tree.NUMBER, 3);
 55+productions[ReduceActions.TO_NUMBER_NAN_AS_ZERO] = new ReductionPair(Tree.NUMBER, 2);
 56 productions[ReduceActions.FIXED_CELL_VAL] = new ReductionPair(Tree.VARIABLE, 1);
 57 productions[ReduceActions.FIXED_CELL_RANGE_VAL] = new ReductionPair(Tree.VARIABLE, 3);
 58 productions[ReduceActions.CELL_VALUE] = new ReductionPair(Tree.VARIABLE, 1);
 59 productions[ReduceActions.CELL_RANGE_VALUE] = new ReductionPair(Tree.VARIABLE, 3);
 60-productions[ReduceActions.PERCENT] = new ReductionPair(Tree.VARIABLE, 3);
 61+productions[ReduceActions.PERCENT] = new ReductionPair(Tree.NUMBER, 3);
 62 productions[ReduceActions.AS_ERROR] = new ReductionPair(Tree.ERROR, 1);
 63 productions[ReduceActions.AS_ARRAY] = new ReductionPair(Tree.VARIABLE, 1);
 64 productions[ReduceActions.REFLEXIVE_REDUCE] = new ReductionPair(Tree.VARIABLE, 1);
 65+productions[ReduceActions.RETURN_LAST_AS_NUMBER] = new ReductionPair(Tree.NUMBER, 2);
 66 const PRODUCTIONS = productions;
 67 
 68 
 69@@ -441,15 +444,23 @@ let table = [];
 70 table[Tree.START] = ObjectBuilder
 71   .add(Symbol.NUMBER, Tree.NUMBER)
 72   .add(Symbol.WHITE_SPACE, Tree.START)
 73-  .add(Symbol.END, Tree.TERMINATE)
 74+  .add(Symbol.END, Tree.TERMINATE_NUMBER)
 75   .build();
 76 table[Tree.NUMBER] = ObjectBuilder
 77+  .add(Symbol.ASTERISK, [SHIFT, ReduceActions.MULTIPLY])
 78   .add(Symbol.WHITE_SPACE, Tree.NUMBER)
 79-  .add(Symbol.END, [REDUCE, ReduceActions.AS_NUMBER])
 80+  .add(Symbol.END, Tree.TERMINATE_NUMBER)
 81+  .build();
 82+table[Tree.ASTERISK] = ObjectBuilder
 83+  .add(Symbol.WHITE_SPACE, Tree.ASTERISK)
 84+  .add(Symbol.END, Tree.TERMINATE_NUMBER)
 85   .build();
 86 table[Tree.TERMINATE] = ObjectBuilder
 87   .add(Symbol.END, [REDUCE, ReduceActions.RETURN_LAST])
 88   .build();
 89+table[Tree.TERMINATE_NUMBER] = ObjectBuilder
 90+  .add(Symbol.END, [REDUCE, ReduceActions.RETURN_LAST_AS_NUMBER])
 91+  .build();
 92 const ACTION_TABLE = table;
 93 
 94 
 95@@ -479,6 +490,8 @@ let Parser = (function () {
 96         switch (reduceActionToPerform) {
 97           case ReduceActions.RETURN_LAST:
 98             return virtualStack[vsl - 1];
 99+          case ReduceActions.RETURN_LAST_AS_NUMBER:
100+            return sharedStateYY.handler.helper.number(virtualStack[vsl - 1]);
101           case ReduceActions.CALL_VARIABLE:
102             this.$ = sharedStateYY.handler.helper.callVariable.call(this, virtualStack[vsl]);
103             break;
104diff --git a/tests/Parser/ParseEngineTest.ts b/tests/Parser/ParseEngineTest.ts
105index 6ff8809..efc9170 100644
106--- a/tests/Parser/ParseEngineTest.ts
107+++ b/tests/Parser/ParseEngineTest.ts
108@@ -317,4 +317,9 @@ parser.setObj("A1");
109 
110 test("Declare number", function () {
111   assertEquals(parser.parse('5'), 5);
112+  assertEquals(parser.parse('  5  '), 5);
113+});
114+
115+test("Number multiplication", function () {
116+  assertEquals(parser.parse('5*5'), 25);
117 });
118\ No newline at end of file