예제 #1
0
파일: run.py 프로젝트: hculpan/pylox
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()
예제 #2
0
 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)
예제 #3
0
파일: run.py 프로젝트: hculpan/pylox
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))
예제 #4
0
 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)
예제 #5
0
 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)
예제 #6
0
 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)
예제 #7
0
 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)
예제 #8
0
 def test_grouping(self):
     print("Running {0}".format(sys._getframe().f_code.co_name))
     tokens = tokenize('(-12.09);', 0)
     ast = parse(tokens)
     print(ast)