Пример #1
0
    def test_invalid_variable_reference(self):
        c = self.compiler

        ast = parse('+ n 0')[0]
        with self.assertRaises(SyntaxError):
            c.compile_expression(ast)

        ast = parse('< n 0')[0]
        with self.assertRaises(SyntaxError):
            c.compile_expression(ast)
Пример #2
0
    def test_compile_begin(self):
        c = self.compiler
        ast = parse('''
(begin
    (set i 0)
    (while (< i 5)
        (puts "hello")
        (inc i))
    0)
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [
                    'i = 0;',
                    'while (i < 5) {',
                    [
                        'puts("hello");',
                        'while1000 = (++i);',
                        ],
                    '}',
                    ]
                )

        self.assertEqual(
                ce.exp,
                '0',
                )
Пример #3
0
    def test_compile_for(self):
        c = self.compiler
        ast = parse('''
(for (set i 1) (< i 10) (inc i)
  (printf "%d\n" i))
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [
                    'i = 1;',
                    'while (i < 10) {',
                    [
                        'for1000 = (printf("%d\n", i));',
                        '++i;',
                        ],
                    '}',
                    ],
                )

        self.assertEqual(
                ce.exp,
                'for1000',
                )
Пример #4
0
    def test_compile_if_comparison(self):
        c = self.compiler
        ast = parse('(if (< 1 2) 1 0)')[0]
        ce = c.compile_if(*ast)
        self.assertEqual(
                ce.pre,
                [
                    'if (1 < 2) {',
                    [
                        'if1000 = 1;',
                        ],
                    '} else {',
                    [
                        'if1000 = 0;',
                        ],
                    '}',
                    ],
                )
        self.assertEqual(
                ce.exp,
                'if1000',
                )

        self.assertTrue(
                'if1000' in c.current_scope(),
                )
Пример #5
0
    def test_compile_if_default_alternative(self):
        c = self.compiler
        ast = parse('(if 1 1)')[0]
        ce = c.compile_if(*ast)
        self.assertEqual(
                ce.pre,
                [
                    'if (1) {',
                    [
                        'if1000 = 1;',
                        ],
                    '} else {',
                    [
                        'if1000 = 0;',
                        ],
                    '}',
                    ],
                )
        self.assertEqual(
                ce.exp,
                'if1000',
                )

        self.assertTrue(
                'if1000' in c.current_scope(),
                )
Пример #6
0
    def test_compile_each(self):
        c = self.compiler
        ast = parse('''
each n (range 10)
    printf "%d\n" n
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [
                    'n = 0;',
                    'while (n < 10) {',
                    [
                        'each1000 = (printf("%d\n", n));',
                        'n += 1;',
                        ],
                    '}',
                    ]
                )

        self.assertEqual(
                ce.exp,
                'each1000',
                )
Пример #7
0
    def test_compile_prefix(self):
        c = self.compiler

        ast = parse('(set i 0)')[0]
        ce = c.compile_expression(ast)

        ast = parse('(inc i)')[0]
        ce = c.compile_expression(ast)
        self.assertEqual(ce.pre, [],)
        self.assertEqual(ce.exp, '++i',)

        ast = parse('(dec i)')[0]
        ce = c.compile_expression(ast)
        self.assertEqual(ce.pre, [],)
        self.assertEqual(ce.exp, '--i',)


        ast = parse('(post-inc i)')[0]
        ce = c.compile_expression(ast)
        self.assertEqual(ce.pre, [],)
        self.assertEqual(ce.exp, 'i++',)

        ast = parse('(post-dec i)')[0]
        ce = c.compile_expression(ast)
        self.assertEqual(ce.pre, [],)
        self.assertEqual(ce.exp, 'i--',)

        ast = parse('(not (+ 1 1))')[0]
        ce = c.compile_expression(ast)
        self.assertEqual(ce.pre, [],)
        self.assertEqual(ce.exp, '!(1 + 1)',)
Пример #8
0
    def test_addition(self):
        c = self.compiler
        ast = parse('(+ 0 1 2)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '0 + 1 + 2',
                )
Пример #9
0
    def test_comparison_operator_many_arguments_expression(self):
        c = self.compiler
        ast = parse('(< 0 (+ 0 1) 2)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                ['comp_exp1000 = (0 + 1);'],
                )

        self.assertEqual(
                ce.exp,
                '(0 < comp_exp1000) && (comp_exp1000 < 2)',
                )
Пример #10
0
    def test_comparison_operator_many_arguments(self):
        c = self.compiler
        ast = parse('(< 0 1 2)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '(0 < 1) && (1 < 2)',
                )
Пример #11
0
    def test_comparison_operator(self):
        c = self.compiler
        ast = parse('(< 0 1)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '0 < 1',
                )
Пример #12
0
    def test_chained_assignment(self):
        c = self.compiler
        ast = parse('(set a b 5)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                'a = b = 5',
                )
Пример #13
0
    def test_compile_subtitution_two_arguments(self):
        c = self.compiler
        eq_ast = parse('(- 5 5)')[0]
        ce = c.compile_expression(eq_ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '5 - 5',
                )
Пример #14
0
    def test_compile_infix_and(self):
        c = self.compiler
        and_ast = parse('(and 1 0)')[0]
        ce = c.compile_expression(and_ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '1 && 0',
                )
Пример #15
0
    def test_compile_infix_eq(self):
        c = self.compiler
        eq_ast = parse('(= 1 0)')[0]
        ce = c.compile_expression(eq_ast)

        self.assertEqual(
                ce.pre,
                []
                )

        self.assertEqual(
                ce.exp,
                '1 == 0',
                )
Пример #16
0
    def test_compile_prefix_not(self):
        c = self.compiler
        not_ast = parse('(not 1)')[0]
        ce = c.compile_expression(not_ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '!1',
                )
Пример #17
0
    def test_compile_infix_or(self):
        c = self.compiler
        or_ast = parse('(or 1 0)')[0]
        ce = c.compile_expression(or_ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '1 || 0',
                )
Пример #18
0
    def test_substitution(self):
        c = self.compiler
        ast = parse('(- 0 1 2)')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [],
                )

        self.assertEqual(
                ce.exp,
                '0 - 1 - 2',
                )
Пример #19
0
    def test_compile_exp_in_range(self):
        c = self.compiler
        ast = parse('''
in (+ 1 2) (range 10)
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                ['in1000 = (1 + 2);']
                )

        self.assertEqual(
                ce.exp,
                '(0 <= in1000) && (in1000 < 10)',
                )
Пример #20
0
    def test_compile_exp_in_string(self):
        c = self.compiler
        ast = parse('''
in (+ 'a' 1) "abc"
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                ["in1000 = ('a' + 1);"]
                )

        self.assertEqual(
                ce.exp,
                "(in1000 == 'a') || (in1000 == 'b') || (in1000 == 'c')"
                )
Пример #21
0
    def test_compile_in_range(self):
        c = self.compiler
        ast = parse('''
in 1 (range 10)
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                []
                )

        self.assertEqual(
                ce.exp,
                '(0 <= 1) && (1 < 10)',
                )
Пример #22
0
    def test_compile_in_string(self):
        c = self.compiler
        ast = parse('''
in 'c' "abc"
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                []
                )

        self.assertEqual(
                ce.exp,
                "('c' == 'a') || ('c' == 'b') || ('c' == 'c')"
                )
Пример #23
0
    def test_compile_identity(self):
        c = self.compiler
        ast = parse('''
puts
    $ "hello"
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                []
                )

        self.assertEqual(
                ce.exp,
                'puts("hello")'
                )
Пример #24
0
    def test_compile_case(self):
        c = self.compiler
        ast = parse('''
(case '1'
  ('0' 0)
  ('1' 1)
  ('2' (+ 1 1))
  (default -1))
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [
                    "switch ('1') {",
                    [
                        "case '0':",
                        [
                            'case1000 = (0);',
                            'break;',
                            ],
                        "case '1':",
                        [
                            'case1000 = (1);',
                            'break;',
                            ],
                        "case '2':",
                        [
                            'case1000 = (1 + 1);',
                            'break;',
                            ],
                        'default:',
                        [
                            'case1000 = (-1);',
                            'break;',
                            ],
                    ],
                    '}',
                    ]
                )

        self.assertEqual(
                ce.exp,
                'case1000',
                )
Пример #25
0
    def test_compile_variable(self):
        c = self.compiler

        tests = (
                ('argc int', 'int argc'),
                ('argv (* * char)', 'char **argv'),
                ('argv (* CArray char)', 'char (*argv)[]'),
                )

        for code_input, c_output in tests:
            token_name, tokens_type = parse(code_input)[0]
            var_name = token_name.value
            var_type = parse_type(tokens_type)

            self.assertEqual(
                    c.compile_variable_declaration(var_name, var_type),
                    c_output,
                    )
Пример #26
0
    def test_compile_infix_pre(self):
        c = self.compiler
        eq_ast = parse('(+ 1 (if 1 1 1))')[0]
        ce = c.compile_expression(eq_ast)

        self.assertEqual(
                ce.pre,
                [
                    'if (1) {',
                    ['if1000 = 1;'],
                    '} else {',
                    ['if1000 = 1;'],
                    '}',
                    ]
                )

        self.assertEqual(
                ce.exp,
                '1 + (if1000)',
                )
Пример #27
0
    def test_compile_cond(self):
        c = self.compiler
        ast = parse('''
(cond (true 1)
      (true 2)
      (true 3)
      (else 4))
''')[0]
        ce = c.compile_cond(*ast)
        self.assertEqual(
                ce.pre,
                [
                    'if (true) {',
                    [
                        'cond1000 = 1;',
                        ],
                    '} else if (true) {',
                    [
                        'cond1000 = 2;',
                        ],
                    '} else if (true) {',
                    [
                        'cond1000 = 3;',
                        ],
                    '} else {',
                    [
                        'cond1000 = 4;',
                        ],
                    '}',
                    ],
                )

        self.assertEqual(
                ce.exp,
                'cond1000',
                )

        self.assertTrue(
                'cond1000' in c.current_scope(),
                )
Пример #28
0
    def test_compile_while(self):
        c = self.compiler
        ast = parse('''
(while 1
  1)
''')[0]
        ce = c.compile_expression(ast)

        self.assertEqual(
                ce.pre,
                [
                    'while (1) {',
                    [
                        'while1000 = 1;',
                        ],
                    '}',
                    ],
                )

        self.assertEqual(
                ce.exp,
                'while1000',
                )
Пример #29
0
def parse_to_values(code):
    return map_tree_to_values(parse(code))
Пример #30
0
    def test_comparison(self):
        c = self.compiler

        ast = parse('< 0')[0]
        with self.assertRaises(SyntaxError):
            c.compile_expression(ast)