def test_identifier(self): x = parser.parse_expression('x1') self.assertIsInstance(x, ast.IdentifierExpression) self.assertEqual(x.name, 'x1') x = parser.parse_expression('_') self.assertIsInstance(x, ast.IdentifierExpression) self.assertEqual(x.name, '_') x = parser.parse_expression('Xyz') self.assertIsInstance(x, ast.IdentifierExpression) self.assertEqual(x.name, 'Xyz')
def test_expr_priority(self): x = parser.parse_expression('-1 + 2 * + 3') self.assertIsInstance(x, ast.BinaryOperatorExpression) self.assertEqual(x.range, ast.Range('<empty>', 1, 1, 1, 13)) x_lhs = x.lhs self.assertIsInstance(x_lhs, ast.PrefixOperatorExpression) self.assertEqual(x_lhs.range, ast.Range('<empty>', 1, 1, 1, 3)) self.assertEqual(x_lhs.op, '-') self.assertIsInstance(x_lhs.expr, ast.IntLiteral) self.assertEqual(x_lhs.expr.value, 1) x_rhs = x.rhs self.assertIsInstance(x_rhs, ast.BinaryOperatorExpression) self.assertEqual(x_rhs.range, ast.Range('<empty>', 1, 6, 1, 13)) x_rhs_lhs = x_rhs.lhs self.assertIsInstance(x_rhs_lhs, ast.IntLiteral) self.assertEqual(x_rhs_lhs.range, ast.Range('<empty>', 1, 6, 1, 7)) self.assertEqual(x_rhs_lhs.value, 2) x_rhs_rhs = x_rhs.rhs self.assertIsInstance(x_rhs_rhs, ast.PrefixOperatorExpression) self.assertEqual(x_rhs_rhs.range, ast.Range('<empty>', 1, 10, 1, 13)) self.assertEqual(x_rhs_rhs.op, '+') x_rhs_rhs_expr = x_rhs_rhs.expr self.assertIsInstance(x_rhs_rhs_expr, ast.IntLiteral) self.assertEqual(x_rhs_rhs_expr.range, ast.Range('<empty>', 1, 12, 1, 13)) self.assertEqual(x_rhs_rhs_expr.value, 3)
def test_parse_primary_addition_expression(self): tokens = [ TokenInteger('1'), TokenAdditionOperator('+'), TokenInteger('2'), ] expr = parse_expression(tokens) self.assertEqual(str(expr), "(BinaryOp + (Constant (Int 1)) (Constant (Int 2)))")
def test_parse_binary_operation_1(self): tokens = [ TokenIdentifier('a'), TokenAdditionOperator('+'), TokenInteger('2'), ] expr = parse_expression(tokens) self.assertEqual(str(expr), "(BinaryOp + (Variable a) (Constant (Int 2)))")
def test_function_expression(self): tests = [ # Func calls 'skittles', 'skittles(rainbox)', 'skittles(taste, rainbow)', # Method calls 'rainbow.skittles', 'rainbow.skittles(taste)', 'rainbow.skittles(taste, skookum)', ] for case in tests: result = parser.parse_expression(case) self.assertIsInstance(result, ast.FuncExpression)
def _parse_use_cases(self, docstring): examples = [[e.strip() for e in l.split('==')] for l in docstring.strip().split('\n')] use_cases = [] for exp, outcome_exp in examples: name, args = parser.parse_call(exp) assert name == self.name assert len(args) == self.n_args outcome = parser.parse_expression(outcome_exp) use_cases.append((args, outcome)) return use_cases
def test_parse_binary_operation_2(self): tokens = [ TokenIdentifier('a'), TokenAdditionOperator('+'), TokenInteger('1'), TokenMultiplicationOperator('*'), TokenInteger('2'), # a + 1 * 2 => (+ a (* 1 2)) ] expr = parse_expression(tokens) self.assertEqual( str(expr), "(BinaryOp + a (BinaryOp * (Constant (Int 1)) (Constant (Int 2))))" )
def main(): # database = load_relations() database = restore_state() cmd = "" prompt = "> " cmd_list = [] try: while cmd != "quit": cmd = input(prompt) cmd_list.append(cmd) database, tokens = parse_expression(cmd, database) save_state(database) except: # save_state(database) # save state even if error occurs print("An unknown error occurred.")
def test_parse_constant_expression(self): tokens = [TokenInteger('1')] expr = parse_expression(tokens) self.assertEqual(str(expr), "(Constant (Int 1))")
def eval(self, expr: str) -> int: return self._calc(parse_expression(expr))
def parse_expression(self, string, source, allow_empty): return parser.parse_expression(string, source, allow_empty)