def test_hello_world(self):
        with open('programs/hello_world') as f:
            prog = f.read()
            ast = parse(prog)

            assert len(ast) == 1
            assert ast[0][0] == VISIBLE
            assert ast[0][1][0][0][1] == (VALUE, (YARN, 'Hai world!'))
    def test_loops2(self):
        with open('programs/loops2') as f:
            prog = f.read()
            ast = parse(prog)

            with captured_output() as (out, _):
                inp = LolCodeInterpreter()
                inp.interpret(ast)
                assert out.getvalue() == '0 2 4 6 8 '
    def test_simple_program1(self):
        with open('programs/simple_program1') as f:
            prog = f.read()
            ast = parse(prog)

            with captured_output() as (out, _):
                inp = LolCodeInterpreter()
                inp.interpret(ast)
                assert out.getvalue() == '77lls\nTrueFalse\nFalse\nHELLO12\n'
    def test_if_else(self):
        with open('programs/ifelse') as f:
            prog = f.read()
            ast = parse(prog)

            assert len(ast) == 4
            assert ast[3][0] == IF_ELSE
            assert ast[3][1][0][0][0] == VISIBLE
            assert ast[3][1][1][0][1][0][0] == VISIBLE
    def test_ifelse(self, raw_input):
        with open('programs/ifelse') as f:
            prog = f.read()
            ast = parse(prog)
            raw_input.return_value = 'mouse'

            with captured_output() as (out, _):
                inp = LolCodeInterpreter()
                inp.interpret(ast)
                assert out.getvalue() == 'Hello mouse\nNice to eat you\n'
    def test_assignment(self, raw_input):
        with open('programs/assignment') as f:
            prog = f.read()
            ast = parse(prog)
            raw_input.return_value = '10'

            with captured_output() as (out, _):
                inp = LolCodeInterpreter()
                inp.interpret(ast)
                assert out.getvalue() == 'result is 20\n20\n'
    def test_loop(self):
        with open('programs/loops') as f:
            prog = f.read()
            ast = parse(prog)

            assert len(ast) == 1
            assert ast[0][0] == LOOP
            assert ast[0][1][0][0] == VAR
            assert ast[0][1][2][0] == TIL
            assert ast[0][1][2][1][1][0] == SAME
            assert ast[0][1][3][0][0] == VISIBLE
    def test_assignment(self):
        with open('programs/assignment') as f:
            prog = f.read()
            ast = parse(prog)

            assert len(ast) == 10
            assert ast[0][0] == DECLARE
            assert ast[0][1][0][0] == VAR
            assert ast[0][1][0][1] == 'x'
            assert ast[0][1][1] is None
            assert ast[2][1][1][1][1] == '0'
            assert ast[6][0] == CAST
            assert ast[6][1][0][0] == VAR
            assert ast[6][1][0][1] == 'y'
            assert ast[6][1][1] == NUMBR
    def test_expr(self):
        with open('programs/expr') as f:
            prog = f.read()
            ast = parse(prog)

            assert len(ast) == 3
            assert ast[0][0] == VISIBLE
            assert ast[0][1][0][0][1] == (VALUE, (YARN, 'Hai world!'))
            assert ast[0][1][0][2][1][0] == SUM
            assert ast[0][1][0][3][1][0] == BOTH
            assert ast[0][1][0][3][1][1][0][1] == (VALUE, (TROOF, 'WIN'))
            assert ast[0][1][0][3][1][1][1][1] == (VALUE, (TROOF, 'FAIL'))
            assert ast[0][1][0][4][1][0] == ALL
            assert ast[1][1][0][0][1][0] == SAME
            assert ast[1][1][0][1][1][0] == MAEK
            assert ast[1][1][0][1][1][1][-1] == NUMBR
            assert ast[2][0] == GIMMEH
Exemplo n.º 10
0
        self.vars[local_var_name] = 0

        if args[1] == UPPIN:
            f = lambda x: x + 1
        else:
            f = lambda x: x - 1

        loop_type = args[2][0]
        cond = args[2][1][1]
        statements = args[3]

        if loop_type == TIL:
            stop = lambda x: x
        else:
            stop = lambda x: not x

        while not stop(self.process_expr(cond)):
            self.process_statements(statements)
            self.vars[local_var_name] = f(self.vars[local_var_name])

        self.vars = old_vars


if __name__ == '__main__':
    with open(argv[1]) as f:
        prog = f.read()
        ast = parse(prog)

        inp = LolCodeInterpreter()
        inp.interpret(ast)