Exemplo n.º 1
0
def main():
    interpreter = Interpreter()

    while True:
        try:
            buffer = raw_input('serval> ')
        except EOFError:
            print
            break
        lexer = Lexer(buffer)

        try:
            expressions = Parser(lexer).parse()
            if not expressions:
                continue
            expression = expressions[0]

            if is_load(expression):
                load(interpreter, expression)
                continue

            result_expr = interpreter.interpret(expression)
            if procedure.is_compound_procedure(result_expr):
                print procedure.get_procedure_repr(result_expr)
            else:
                print result_expr
        except Exception as e:
            print str(e)
            continue
Exemplo n.º 2
0
    def test_load_procedure(self):
        from serval.lexer import Lexer
        from serval.parser import Parser
        from serval.interpreter import Interpreter
        from serval.expression.util import load

        interp = Interpreter()
        filepath = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), 'data', 'test.ss')
        expr = Parser(Lexer('(load "%s")' % filepath)).parse()[0]
        load(interp, expr)

        expr = Parser(Lexer('c')).parse()[0]
        result = interp.interpret(expr)
        self.assertEquals(str(result), '(1 2 3)')
Exemplo n.º 3
0
    def test_load_procedure(self):
        from serval.lexer import Lexer
        from serval.parser import Parser
        from serval.interpreter import Interpreter
        from serval.expression.util import load

        interp = Interpreter()
        filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                'data', 'test.ss')
        expr = Parser(Lexer('(load "%s")' % filepath)).parse()[0]
        load(interp, expr)

        expr = Parser(Lexer('c')).parse()[0]
        result = interp.interpret(expr)
        self.assertEquals(str(result), '(1 2 3)')