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 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_simple_3(self): s = "This is.a test" l = tokenize(s) assert len(l) == 6 assert l[0][1] == "This" assert l[1][1] == "is" assert l[2][1] == "." assert l[3][1] == "a" assert l[4][1] == "test"
def test_simple_4(self): s = 'This is "a" test!' l = tokenize(s) assert len(l) == 6 assert l[0][1] == "This" assert l[0][0] == TokenType.IDENTIFIER assert l[1][1] == "is" assert l[1][0] == TokenType.IDENTIFIER assert l[2][1] == "a" assert l[2][0] == TokenType.STRING assert l[3][1] == "test" assert l[3][0] == TokenType.IDENTIFIER assert l[4][1] == "!" assert l[4][0] == TokenType.BANG assert l[5][0] == TokenType.EOL
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)
def test_simple_1(self): s = "This is a test" l = tokenize(s) assert len(l) == 5