diff --git a/calculator.py b/calculator.py index 6b21d6f..8eafa74 100644 --- a/calculator.py +++ b/calculator.py @@ -76,7 +76,8 @@ def _match(self, tokens: List[Token], target_rule: str): # Success! return RuleMatch(target_rule, matched), remaining_tokens - if rules_map.index(target_rule) + 1 < len(rules_map): - return self._match(tokens, rules_map.key_at(rules_map.index(target_rule) + 1)) + idx = rules_map.index(target_rule) + if idx + 1 < len(rules_map): + return self._match(tokens, rules_map.key_at(idx + 1)) return None, None diff --git a/common.py b/common.py index 5928467..722f8bd 100644 --- a/common.py +++ b/common.py @@ -98,7 +98,7 @@ def key_at(self, i): rules_map = ImmutableIndexedDict(( ('asn', ('IDT EQL add',)), - ('add', ('mul ADD add',)), + ('add', ('mul ADD add', 'mui ADD add',)), ('mui', ('pow mul',)), ('mul', ('pow MUL mul',)), ('pow', ('opr POW pow',)), diff --git a/tests.py b/tests.py index dcf51b6..4843270 100644 --- a/tests.py +++ b/tests.py @@ -55,7 +55,7 @@ class MultiplicationTests(unittest.TestCase): def runTest(self): self.assertEqual(evaluate('1 * 1'), 1.0) self.assertEqual(evaluate('2 * 3'), 6.0) - # self.assertEqual(evaluate('-2 * -2'), 4.0) + self.assertEqual(evaluate('-2 * -2'), 4.0) self.assertEqual(evaluate('(2 * 3) (2 * 3)'), 36.0) @@ -116,7 +116,9 @@ def runTest(self): self.assertTrue(sympy.Matrix(evaluate('identity({})'.format(r_dim), False)).equals(sympy.Identity(r_dim))) - for _ in range(5): + for _ in range(1): + print(_) + mat = [[more_zeroes(random.randint(0, 100)) for _ in range(r_dim)] for _ in range(r_dim)] mat_str = '[' + '|'.join([','.join(map(str, line)) for line in mat]) + ']'