Esempio n. 1
0
File: repl.py Progetto: fotcorn/lisp
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))
Esempio n. 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)
Esempio n. 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)
Esempio n. 4
0
def evaluate_file(scope, fname):
    fd = os.open(fname, os.O_RDONLY, 0777)
    data = ""
    while True:
        data_append = os.read(fd, 4096)
        if len(data_append) == 0:
            break
        data += data_append
    os.close(fd)
    
    parsed = parse(data)
    sexps = parsed.to_list()
    return eval_list(scope, sexps)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)