Beispiel #1
0
    def test_compile_func_call(self):
        # This also tests scope_begin and scope_over

        opcodes = [
            OpCode("func_decl", "hello---", ""),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"World"', None),
            OpCode("scope_over", "", ""),
            OpCode("func_call", "hello---", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["hello", "var", "function"],
            2: ['"World"', "string", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "#include <stdio.h>",
                "",
                "void hello(void) {",
                '\tprintf("World");',
                "}",
                "\thello();",
                "",
            ],
        )
Beispiel #2
0
    def test_compile_while(self):
        opcodes = [
            OpCode("var_assign", "i---0", "int"),
            OpCode("while", "i < 10", None),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"%d", i', None),
            OpCode("scope_over", "", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["i", "int", "variable"],
            2: ["0", "int", "constant"],
            3: ["10", "int", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "#include <stdio.h>",
                "\tint i = 0;",
                "\twhile(i < 10) {",
                '\tprintf("%d", i);',
                "}",
                "",
            ],
        )
Beispiel #3
0
    def test_compile_for(self):
        opcodes = [
            OpCode("for", "i&&&1&&&10&&&+&&&<&&&1", None),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"%d", i', None),
            OpCode("scope_over", "", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["i", "int", "variable"],
            2: ["1", "int", "constant"],
            3: ["10", "int", "constant"],
            4: ["1", "int", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "#include <stdio.h>",
                "\tfor(int i = 1; i < 10; i+=1) {",
                '\tprintf("%d", i);',
                "}",
                "",
            ],
        )
Beispiel #4
0
    def test_assign_statement_array_only_assign_no_error(self):
        tokens_list = [
            Token("MAIN", "", 1),
            Token("newline", "", 1),
            Token("var", "", 2),
            Token("id", 1, 2),
            Token("left_bracket", "", 2),
            Token("number", 2, 2),
            Token("right_bracket", "", 2),
            Token("newline", "", 2),
            Token("id", 1, 3),
            Token("assignment", "", 3),
            Token("left_brace", "", 3),
            Token("number", 3, 3),
            Token("plus", "", 3),
            Token("number", 4, 3),
            Token("right_brace", "", 3),
            Token("newline", "", 3),
            Token("END_MAIN", "", 4),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "var", "variable", "", ""],
            2: ["2", "int", "constant", "", ""],
            3: ["1", "int", "constant", "", ""],
            4: ["2", "int", "constant", "", ""],
        }

        opcodes = parse(tokens_list, table)

        self.assertEqual(
            opcodes[2],
            OpCode("array_only_assign", "a--- = (int [2]){1 + 2}", ""))
Beispiel #5
0
    def test_assign_statement_ptr_only_assign_no_error(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("assignment", "", 1),
            Token("number", 2, 1),
            Token("newline", "", 1),
            Token("var", "", 2),
            Token("multiply", "", 2),
            Token("id", 3, 2),
            Token("assignment", "", 2),
            Token("bitwise_and", "", 2),
            Token("id", 1, 2),
            Token("newline", "", 2),
            Token("multiply", "", 3),
            Token("id", 3, 3),
            Token("assignment", "", 3),
            Token("number", 4, 3),
            Token("newline", "", 3),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["b", "var", "variable", "", ""],
            2: ["1", "int", "constant", "", ""],
            3: ["a", "var", "variable", "", ""],
            4: ["1", "int", "constant", "", ""],
        }

        opcodes = parse(tokens_list, table)

        self.assertEqual(opcodes[2],
                         OpCode("ptr_only_assign", "a---=---1---1", ""))
Beispiel #6
0
    def test_expression_unknown_variable_error(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("assignment", "", 1),
            Token("number", 2, 1),
            Token("newline", "", 1),
            Token("print", "", 2),
            Token("left_paren", "", 2),
            Token("string", 3, 2),
            Token("right_paren", "", 2),
            Token("call_end", "", 2),
            Token("newline", "", 2),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "var", "variable", "", ""],
            2: ["1", "int", "constant", "", ""],
            3: ['"value = {a}"', "string", "constant", "", ""],
        }

        self.__suppress_print()

        with self.assertRaises(SystemExit):
            _ = expression(tokens=tokens_list, i=7, table=table, msg="")

        self.__release_print()
Beispiel #7
0
    def test_assign_statement_array_only_assign_error(self):
        tokens_list = [
            Token("MAIN", "", 1),
            Token("newline", "", 1),
            Token("var", "", 2),
            Token("id", 1, 2),
            Token("left_bracket", "", 2),
            Token("number", 2, 2),
            Token("right_bracket", "", 2),
            Token("newline", "", 2),
            Token("id", 1, 3),
            Token("assignment", "", 3),
            Token("number", 3, 3),
            Token("plus", "", 3),
            Token("number", 4, 3),
            Token("newline", "", 3),
            Token("END_MAIN", "", 4),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "var", "variable", "", ""],
            2: ["2", "int", "constant", "", ""],
            3: ["1", "int", "constant", "", ""],
            4: ["2", "int", "constant", "", ""],
        }

        self.__suppress_print()

        with self.assertRaises(SystemExit):
            _ = parse(tokens_list, table)

        self.__release_print()
Beispiel #8
0
    def test_compile_ptr_only_assign(self):
        ## TODO: Fix this test after bug #23 gets fixed
        opcodes = [
            OpCode("var_assign", "a---1", "int"),
            OpCode("ptr_assign", "n---&a---1", "int"),
            OpCode("ptr_only_assign", "n---=---2---1", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "int", "variable"],
            2: ["1", "int", "constant"],
            3: ["n", "int", "variable"],
            4: ["2", "int", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            ["#include <stdio.h>", "\tint a = 1;", "\tint *n = &a;", "\t**n = =;", ""],
        )
Beispiel #9
0
    def test_compile_if_else_if_else(self):
        # Testing if, else if, else

        opcodes = [
            OpCode("var_assign", "i---0", "int"),
            OpCode("if", "i == 1", None),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"%d", 1', None),
            OpCode("scope_over", "", ""),
            OpCode("else_if", "i == 2", None),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"%d", 2', None),
            OpCode("scope_over", "", ""),
            OpCode("else", "", ""),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"Else"', None),
            OpCode("scope_over", "", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["i", "int", "variable"],
            2: ["0", "int", "constant"],
            3: ["1", "int", "constant"],
            4: ["1", "int", "constant"],
            5: ["2", "int", "constant"],
            6: ["2", "int", "constant"],
            7: ['"Else"', "string", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "#include <stdio.h>",
                "\tint i = 0;",
                "\tif(i == 1) {",
                '\tprintf("%d", 1);',
                "}",
                "\telse if(i == 2) {",
                '\tprintf("%d", 2);',
                "}",
                "\telse {",
                '\tprintf("Else");',
                "}",
                "",
            ],
        )
Beispiel #10
0
    def test_compile_ptr_no_assign(self):
        opcodes = [OpCode("ptr_no_assign", "a", None)]
        table = SymbolTable()
        table.symbol_table = {1: ["a", "declared", "variable"]}

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(data, ["", "\tdeclared *a;", ""])
Beispiel #11
0
    def test_compile_unary(self):
        opcodes = [OpCode("var_assign", "a---1", "int"), OpCode("unary", "a ++ ", None)]
        table = SymbolTable()
        table.symbol_table = {1: ["a", "int", "variable"], 2: ["1", "int", "constant"]}

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(data, ["", "\tint a = 1;", "\ta++;", ""])
Beispiel #12
0
    def test_compile_exit(self):
        opcodes = [OpCode("exit", "0", None)]
        table = SymbolTable()
        table.symbol_table = {1: ["0", "int", "constant"]}

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(data, ["", "\texit(0);", ""])
Beispiel #13
0
    def test_compile_switch_case_default(self):
        # Test swtich, case, and default statements

        opcodes = [
            OpCode("var_assign", "a---1", "int"),
            OpCode("switch", "a", ""),
            OpCode("scope_begin", "", ""),
            OpCode("case", "1", ""),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"Hello"', None),
            OpCode("scope_over", "", ""),
            OpCode("default", "", ""),
            OpCode("scope_begin", "", ""),
            OpCode("print", '"Bye"', None),
            OpCode("scope_over", "", ""),
            OpCode("scope_over", "", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "int", "variable"],
            2: ["1", "int", "constant"],
            3: ["1", "int", "constant"],
            4: ['"Hello"', "string", "constant"],
            5: ['"Bye"', "string", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "#include <stdio.h>",
                "\tint a = 1;",
                "\tswitch(a) {",
                "\tcase 1:",
                "{",
                '\tprintf("Hello");',
                "}",
                "\tdefault:",
                "{",
                '\tprintf("Bye");',
                "}",
                "}",
                "",
            ],
        )
Beispiel #14
0
    def test_var_statement_var_no_assign(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("newline", "", 1)
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["b", "var", "variable", "", ""],
            2: ["1", "int", "constant", "", ""],
            3: ["a", "var", "variable", "", ""],
        }

        opcodes = parse(tokens_list, table)

        self.assertEqual(opcodes[0], OpCode("var_no_assign", "b", None))
Beispiel #15
0
    def test_unary_statement_post_decrement(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("assignment", "", 1),
            Token("number", 2, 1),
            Token("newline", "", 1),
            Token("id", 1, 2),
            Token("decrement", "", 2),
            Token("newline", "", 2),
        ]
        table = SymbolTable()
        table.symbol_table = {1: ["a", "var", "variable"], 2: ["1", "int", "constant"]}

        opcodes = parse(tokens=tokens_list, table=table)

        self.assertEqual(opcodes[1], OpCode("unary", "a -- ", None))
Beispiel #16
0
    def test_assign_statement_assign_no_error(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("newline", "", 1),
            Token("id", 1, 2),
            Token("assignment", "", 2),
            Token("number", 2, 2),
            Token("newline", "", 2),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["b", "var", "variable", "", ""],
            2: ["1", "int", "constant", "", ""],
        }

        opcodes = parse(tokens_list, table)

        self.assertEqual(opcodes[1], OpCode("assign", "b---=---1", ""))
Beispiel #17
0
    def test_compile_assign(self):
        opcodes = [
            OpCode("var_no_assign", "a", None),
            OpCode("assign", "a---=---3.14159", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "float", "variable"],
            2: ["3.14159", "float", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data, ['#include <stdio.h>', '\tfloat a;', '\ta = 3.14159;', ''])
Beispiel #18
0
    def test_expression_cannot_find_type_error(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("newline", "", 2),
            Token("print", "", 2),
            Token("left_paren", "", 2),
            Token("id", 1, 2),
            Token("right_paren", "", 2),
            Token("newline", "", 2),
        ]
        table = SymbolTable()
        table.symbol_table = {1: ["a", "var", "variable"]}

        self.__suppress_print()

        with self.assertRaises(SystemExit):
            _ = parse(tokens=tokens_list, table=table)

        self.__release_print()
Beispiel #19
0
    def test_assign_statement_expected_integer_or_expression_as_index(self):
        tokens_list = [
            Token("MAIN", "", 1),
            Token("newline", "", 1),
            Token("var", "", 2),
            Token("id", 1, 2),
            Token("left_bracket", "", 2),
            Token("number", 2, 2),
            Token("right_bracket", "", 2),
            Token("assignment", "", 2),
            Token("left_brace", "", 2),
            Token("number", 3, 2),
            Token("comma", "", 2),
            Token("number", 4, 2),
            Token("right_brace", "", 2),
            Token("newline", "", 2),
            Token("id", 1, 3),
            Token("left_bracket", "", 3),
            Token("number", 5, 3),
            Token("right_bracket", "", 3),
            Token("assignment", "", 3),
            Token("number", 6, 3),
            Token("newline", "", 3),
            Token("END_MAIN", "", 4),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "var", "variable", "", ""],
            2: ["2", "int", "constant", "", ""],
            3: ["1", "int", "constant", "", ""],
            4: ["2", "int", "constant", "", ""],
            5: ["10", "float", "constant", "", ""],
            6: ["0", "int", "constant", "", ""],
        }

        self.__suppress_print()

        with self.assertRaises(SystemExit):
            _ = parse(tokens_list, table)

        self.__release_print()
Beispiel #20
0
    def test_compile_ptr_assign(self):
        opcodes = [
            OpCode("var_assign", "a---1", "int"),
            OpCode("ptr_assign", "n---&a---1", "int"),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["a", "int", "variable"],
            2: ["1", "int", "constant"],
            3: ["n", "int", "variable"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data, ['#include <stdio.h>', '\tint a = 1;', '\tint *n = &a;', ''])
Beispiel #21
0
    def test_compile_return(self):
        opcodes = [
            OpCode("func_decl", "hello---", ""),
            OpCode("scope_begin", "", ""),
            OpCode("return", "1", ""),
            OpCode("scope_over", "", ""),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["hello", "int", "function"],
            2: ["1", "int", "constant"],
        }

        compile(opcodes, "testing.c", table)

        with open("testing.c", "r") as file:
            data = file.read().split("\n")

        os.remove("testing.c")

        self.assertEqual(
            data, ["", "", "int hello(void) {", "", "\treturn 1;", "}", ""])
Beispiel #22
0
    def test_var_statement_ptr_assign(self):
        tokens_list = [
            Token("var", "", 1),
            Token("id", 1, 1),
            Token("assignment", "", 1),
            Token("number", 2, 1),
            Token("newline", "", 1),
            Token("var", "", 2),
            Token("multiply", "", 2),
            Token("id", 3, 2),
            Token("assignment", "", 2),
            Token("id", 1, 2),
            Token("newline", "", 2),
        ]
        table = SymbolTable()
        table.symbol_table = {
            1: ["b", "var", "variable", "", ""],
            2: ["1", "int", "constant", "", ""],
            3: ["a", "var", "variable", "", ""],
        }

        opcodes = parse(tokens_list, table)

        self.assertEqual(opcodes[1], OpCode("ptr_assign", "a---b---1", "int"))