def makeInterpreter(self, text): from spi import Lexer, Parser, Interpreter lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) return interpreter
# print(lexer.get_next_token()) # print(lexer.get_next_token()) # print(lexer.get_next_token()) # print(lexer.get_next_token()) # Token(BEGIN, 'BEGIN') # Token(ID, 'a') # Token(ASSIGN, ':=') # Token(INTEGER, 2) # Token(SEMI, ';') # Token(END, 'END') # Token(DOT, '.') # Token(EOF, None) # parse file with open('test_full.pas', 'r') as file: text = file.read() lexer = Lexer(text) parser = Parser(lexer) interpreter = Interpreter(parser) interpreter.interpret() for k, v in sorted(interpreter.GLOBAL_SCOPE.items()): print('{} = {}'.format(k, v)) # a = 2 # b = 25 # c = 27 # number = 2 # x = 11 # y = 5.997142857142857
def compute(task): lexer = Lexer(task) parser = Parser(lexer) interpreter = Interpreter(parser) result = interpreter.interpret() return result
def main(): lexer = Lexer(open('text.pas', 'r', encoding='utf-8').read()) parser = Parser(lexer) interpreter = Interpreter() interpreter.visit(parser.parse()) print(interpreter.global_scope)