def runLine(lineNo, line): tokens = tokenize(line, lineNo) statements = parse(tokens) if len(statements) == 1: return statements[0].evaluate() else: for statement in statements: statement.evaluate()
def test_primary_1(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize("true;", 0) ast = parse(tokens) assert ast.type == ExprType.EXPRESSION assert ast.left is None assert ast.right.type == ExprType.LITERAL print(ast)
def runProgram(prog): tokens = tokenize(prog) try: statements = parse(tokens) initEnvironment() for statement in statements: statement.evaluate() except ParseException as err: if err.token is None: print("Parse error: {0}".format(err.message)) else: print("Parse error: {0} [Line {1}]".format(err.message, err.token.lineNo)) except RuntimeException as err: if err.expr is None or err.expr.token is None or err.expr.token.lineNo == 0: print("Runtime error: {0}".format(err.message)) else: print("Runtime error: {0} [Line {1}]".format( err.message, err.expr.token.lineNo))
def test_equality(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize('20- 3 *4 > 15 + 2 == 14 / 3;', 0) ast = parse(tokens) print(ast)
def test_comparison(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize('20- 3 *4 > 15 + 2 ;', 0) ast = parse(tokens) print(ast)
def test_addition3(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize('20- 3 *4 ;', 0) ast = parse(tokens) print(ast)
def test_multiplication2(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize('2* 3 * 4;', 0) ast = parse(tokens) print(ast)
def test_grouping(self): print("Running {0}".format(sys._getframe().f_code.co_name)) tokens = tokenize('(-12.09);', 0) ast = parse(tokens) print(ast)