def test_interpreter(): from lox.scanner import Scanner from lox.parser import Parser from lox.ast.ast_printer import ASTPrinter from lox.interpreter import Interpreter source = """ 1 + 999 """ lox_scan = Scanner(source) tokens = lox_scan.scanTokens() print("Tokens:") for tok in tokens: print(tok) print("\n") lox_parser = Parser(tokens) result = lox_parser.parse() printer = ASTPrinter() print(printer.print(result)) print(Interpreter().evaluate(result))
def run(self, source, errors): #print("source : \n{}".format(source)) tokens = [] statements = [] scan_tokens(source, { 'start': 0, 'current': 0, 'line': 1 }, tokens, errors) if len(tokens) is 1: if tokens[0].type is Tk.EOF: LoxError.error(tokens[0], "Source file is empty.") parser = Parser(tokens) # print("Tokens:\n") # for t in tokens: # print(t) #print("Lox: ready to parse") statements = parser.parse() #print("Lox: ready to resolve") resolver = Resolver(self.interpreter) resolver.resolvelist(statements) if LoxError.haderror: print("Syntax errors detected during compilation") return #print("Lox: ready to interpret") self.interpreter.interpret(statements)
def run_code(self, source_code): scanner = Scanner(source_code, self.error) tokens = scanner.scan_tokens() parser = Parser(tokens, self.error_token) expr = parser.parse() if Lox.has_error: return # print(AstPrinter().print(expr)) self.interpreter.interpret(expr)
def run(source): scan = Scanner(source) tokens = scan.scanTokens() # for token in tokens: # print(token) pars = Parser(tokens) result = pars.parse() print(ASTPrinter().print(result)) print(Interpreter().interpret(result))
def run(source: str) -> None: try: scanner = Scanner(source) tokens = scanner.scan_tokens() parser = Parser(tokens) stmts = parser.parse() Lox.interpreter.interpret(stmts) except ParseError as pe: Lox.error(pe.token, str(pe)) except LoxRuntimeError as lre: Lox.runtime_error(lre)
def run(self, source): scanner = Scanner(source, self) tokens = scanner.scan_tokens() statements = Parser(tokens, self).parse() if self.had_error: return resolver = self.resolver.resolve(statements) if self.had_error: return self.interpreter.interpret(statements)
def test_parser(): from lox.scanner import Scanner from lox.parser import Parser from lox.ast.ast_printer import ASTPrinter source = " 1 + 2 >= 133 + \" gggg \"" lox_scan = Scanner(source) tokens = lox_scan.scanTokens() print("Tokens:") for tok in tokens: print(tok) print("\n") lox_parser = Parser(tokens) result = lox_parser.parse() printer = ASTPrinter() print(printer.print(result))