Пример #1
0
def repl():
    print("Write 'exit' to exit repl")

    history_file = os.path.join(os.path.expanduser("~"), ".fotcorn_lisp_history")
    try:
        readline.read_history_file(history_file)
        readline.set_history_length(1000)
    except FileNotFoundError:
        pass
    atexit.register(readline.write_history_file, history_file)

    readline.parse_and_bind('')

    interpreter = Interpreter(interpreter_builtins.builtins)

    while True:
        try:
            line = input('>>> ')
        except EOFError:
            sys.exit(0)
        except KeyboardInterrupt:
            print()
            continue
        if line == 'exit':
            sys.exit(0)
        try:
            tokens = lex(line)
            ast = parse(tokens)
            value = interpreter.run(ast)
            if value:
                print(value)
        except (LexerException, ParseError, InterpreterException) as ex:
            print('Error: ', str(ex))
Пример #2
0
    def assert_stdout(self, program, stdout):
        tokens = lex(program)
        ast = parse(tokens)

        stdout_file = StringIO()
        interpreter = Interpreter(ast, interpreter_builtins.builtins, stdout=stdout_file)
        interpreter.run()

        self.assertEqual(stdout_file.getvalue(), stdout)
Пример #3
0
    def assert_stdout(self, program, stdout):
        tokens = lex(program)
        ast = parse(tokens)

        stdout_file = StringIO()
        interpreter = Interpreter(interpreter_builtins.builtins,
                                  stdout=stdout_file)
        interpreter.run(ast)

        self.assertEqual(stdout_file.getvalue(), stdout)
Пример #4
0
import os
import sys

from lisp.interpreter import run
from lisp.lexer import lex
from lisp.parser import parse
from lisp.repl import repl

if __name__ == '__main__':
    if len(sys.argv) not in (1, 2):
        print('Run file:   python -m lisp <file.lisp>')
        print('Start repl: python -m lisp')
        sys.exit(1)

    if len(sys.argv) == 1:
        repl()
    else:
        filename = sys.argv[1]
        if not os.path.exists(filename):
            print('Source file {} does not exist'.format(filename))
            sys.exit(1)

        with open(filename, encoding='utf-8') as f:
            program = f.read().strip()
        tokens = lex(program)
        ast = parse(tokens)
        run(ast)
Пример #5
0
    def assert_exception(self, program, exception_type):
        tokens = lex(program)
        ast = parse(tokens)

        interpreter = Interpreter(ast, interpreter_builtins.builtins)
        self.assertRaises(exception_type, interpreter.run)
Пример #6
0
    def assert_exception(self, program, exception_type):
        tokens = lex(program)
        ast = parse(tokens)

        interpreter = Interpreter(interpreter_builtins.builtins)
        self.assertRaises(exception_type, interpreter.run, ast)