Exemplo n.º 1
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);',
                "}",
                "",
            ],
        )
Exemplo n.º 2
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();",
                "",
            ],
        )
Exemplo n.º 3
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);',
                "}",
                "",
            ],
        )
Exemplo n.º 4
0
    def test_compile_single_multi_line_comments(self):
        # Test single and multi line comments

        opcodes = [
            OpCode("single_line_comment", " single line", ""),
            OpCode(
                "multi_line_comment",
                """
                    Multi line
                    """,
                "",
            ),
        ]
        table = SymbolTable()

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

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

        os.remove("testing.c")

        self.assertEqual(
            data,
            [
                "",
                "\t//  single line ",
                "/* ",
                "                    Multi line",
                "                    */",
                "",
            ],
        )
Exemplo n.º 5
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 = =;", ""],
        )
Exemplo n.º 6
0
class TestOpCodeClass(unittest.TestCase):
    def setUp(self):
        self.opcode = OpCode("var_assign", "a---1 + 2", "int")

    def test__str__(self):
        self.assertEqual(str(self.opcode), "OpCode('var_assign', 'a---1 + 2', 'int')")

    def test_opcode2dig(self):
        self.assertEqual(self.opcode.opcode2dig("var_assign"), 2)
        self.assertEqual(self.opcode.opcode2dig("unary"), 12)
        self.assertEqual(self.opcode.opcode2dig("hello"), 0)
Exemplo n.º 7
0
    def test_compile_main_end_main(self):
        opcodes = [OpCode("MAIN", "", ""), OpCode("END_MAIN", "", "")]
        table = SymbolTable()

        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 main() {", "", "\treturn 0;", "}", ""])
Exemplo n.º 8
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++;", ""])
Exemplo n.º 9
0
    def test_compile_continue_break(self):
        # Test continue and break statements

        opcodes = [OpCode("continue", "", ""), OpCode("break", "", "")]
        table = SymbolTable()

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

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

        os.remove("testing.c")

        self.assertEqual(data, ["", "\tcontinue;", "\tbreak;", ""])
Exemplo n.º 10
0
    def test_check_include_math_constant(self):
        opcodes = [OpCode('print', '"%lf", M_PI', 'None')]

        includes = check_include(opcodes)
        includes = set(includes.split("\n"))
        self.assertEqual(includes,
                         set(["#include <stdio.h>", "#include <math.h>"]))
Exemplo n.º 11
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;', ''])
Exemplo n.º 12
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;', ''])
Exemplo n.º 13
0
    def test_compile_print(self):
        opcodes = [OpCode("print", '"%d", 1')]
        table = SymbolTable()

        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>", '\tprintf("%d", 1);', ""])
Exemplo n.º 14
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;", "}", ""])
Exemplo n.º 15
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);", ""])
Exemplo n.º 16
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;", ""])
Exemplo n.º 17
0
    def test_case_statement_no_error(self):
        tokens_list = [
            Token("case", "", 1),
            Token("number", 1, 1),
            Token("colon", "", 1),
            Token("newline", "", 1),
            Token("left_brace", "", 1),
            Token("print", "", 1),
        ]
        table = SymbolTable()
        table.entry("1", "bool", "variable")

        opcode, _, _ = case_statement(
            tokens=tokens_list, i=1, table=table, func_ret_type={}
        )

        self.assertEqual(opcode, OpCode("case", "1", ""))
Exemplo n.º 18
0
    def test_if_statement_no_error(self):
        tokens_list = [
            Token("if", "", 1),
            Token("left_paren", "", 1),
            Token("number", 1, 1),
            Token("right_paren", "", 1),
            Token("call_end", "", 1),
            Token("newline", "", 1),
            Token("print", "", 1),
        ]
        table = SymbolTable()
        table.entry("true", "bool", "variable")

        opcode, _, _ = if_statement(
            tokens=tokens_list, i=1, table=table, func_ret_type={}
        )

        self.assertEqual(opcode, OpCode("if", "true"))
Exemplo n.º 19
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");',
                "}",
                "}",
                "",
            ],
        )
Exemplo n.º 20
0
    def test___str__match_case(self):
        opcode = OpCode(opcode="print", val='"Hello World"')

        self.assertEqual(str(opcode), str(OpCode("print", '"Hello World"', None)))
Exemplo n.º 21
0
    def test___str__no_match_case(self):
        opcode = OpCode(opcode="var_assign", val="a--1+2")

        self.assertNotEqual(str(opcode), str(OpCode("print", '"Hello World"', None)))
Exemplo n.º 22
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");',
                "}",
                "",
            ],
        )
Exemplo n.º 23
0
 def setUp(self):
     self.opcode = OpCode("var_assign", "a---1 + 2", "int")
Exemplo n.º 24
0
    def test_check_include_no_includes(self):
        opcodes = [OpCode("var_assign", "b---2", "int")]

        includes = check_include(opcodes)
        self.assertEqual(includes, "")
Exemplo n.º 25
0
    def test_check_include_input(self):
        opcodes = [OpCode('var_assign', 'a---Enter a number: ---i', 'int')]

        includes = check_include(opcodes)
        self.assertEqual(includes, "#include <stdio.h>")
Exemplo n.º 26
0
    def test_check_include_no_includes(self):
        opcodes = [OpCode('var_assign', 'b---2', 'int')]

        includes = check_include(opcodes)
        self.assertEqual(includes, "")
Exemplo n.º 27
0
    def test_check_include_print(self):
        opcodes = [OpCode("print", '"%d", a', "None")]

        includes = check_include(opcodes)
        self.assertEqual(includes, "#include <stdio.h>")
Exemplo n.º 28
0
    def test_check_include_input(self):
        opcodes = [OpCode("var_assign", "a---Enter a number: ---i", "int")]

        includes = check_include(opcodes)
        self.assertEqual(includes, "#include <stdio.h>")