Exemple #1
0
 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
Exemple #3
0
 def compute(task):
     lexer = Lexer(task)
     parser = Parser(lexer)
     interpreter = Interpreter(parser)
     result = interpreter.interpret()
     return result
Exemple #4
0
 def compute(task):
     lexer = Lexer(task)
     parser = Parser(lexer)
     interpreter = Interpreter(parser)
     result = interpreter.interpret()
     return result
Exemple #5
0
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)