示例#1
0
    def test_palinString(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp23))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table, str_table)

        self.assertEqual(machine.memory[20], 0)
        self.assertEqual(machine.memory[21], 1)
示例#2
0
    def test_incrementVariable(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp18))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        self.assertEqual(machine.memory[machine.registers[SP] - 1], 2)
        self.assertEqual(machine.registers[SP], 0xF000)
示例#3
0
    def test_memFactorial(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp17))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        self.assertEqual(machine.memory[100], 40320)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#4
0
    def tests_memoryAccessWithVariable(self):
        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp14))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        for i in range(5, 25):
            self.assertEqual(machine.memory[i], 0, i)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#5
0
    def test_moreRecursiveFunctionsWithFiller(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp9))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        self.assertEqual(machine.memory[100], 1)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#6
0
    def test_assignmentSingleFunctionCallNestedMem(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp4))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        self.assertEqual(machine.memory[50], 50)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#7
0
    def tests_memorySorted(self):
        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp15))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        for i in range(20, 99):
            self.assertTrue(machine.memory[i] <= machine.memory[i + 1],
                            'Mem loc: {}'.format(i))

        self.assertEqual(machine.registers[SP], 0xF000)
示例#8
0
    def test_bullshitFunctionCallsIsPrime(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp5))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        self.assertEqual(machine.memory[50], 1)
        self.assertEqual(machine.memory[51], 0)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#9
0
    def test_noAssignmentInFunctionsRecursive(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp3))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()

        machine.run(pure, table)

        self.assertEqual(machine.memory[100], 210)
        self.assertEqual(machine.memory[101], 40320)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#10
0
    def test_makeAscending(self):
        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp20))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        ptr_a = machine.memory[100]

        self.assertEqual(machine.memory[ptr_a - 1], 10)

        for i in range(10):
            self.assertEqual(machine.memory[ptr_a + i], i)
示例#11
0
    def test_memString2D(self):
        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp28))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table, str_table)

        for i in range(5):
            if i % 2 == 0:
                for j in range(5):
                    self.assertEqual(machine.memory[5 + i * 5 + j], ord('a'))
            else:
                for j in range(5):
                    self.assertEqual(machine.memory[5 + i * 5 + j], ord('t'))
示例#12
0
    def test_memArrAssign(self):
        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp26))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        arr = machine.memory[100]

        # machine.print_memory(0, 120)
        self.assertEqual(machine.memory[arr - 1], 20)

        for i in range(0, 20):
            self.assertTrue(machine.memory[arr + i] == i, i)
示例#13
0
    def test_basicMath2(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp2))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()

        machine.run(pure, table)

        self.assertEqual(machine.memory[50], -90)
        self.assertEqual(machine.memory[51], 1700)
        self.assertEqual(machine.memory[52], 80)
        self.assertEqual(machine.memory[53], 1700)
        self.assertEqual(machine.memory[54], 1)

        self.assertEqual(machine.registers[SP], 0xF000)
示例#14
0
    def test_mergeSort(self):

        instructions, labels, inv_lbl, func_help = parse(tokenize(sp.samp22))
        lc3_conv = LC3Converter(instructions, labels, inv_lbl, func_help)

        tree, table, str_table = lc3_conv.convert()
        pure = flatten(tree)

        machine = Machine()
        machine.run(pure, table)

        arr = machine.memory[150]

        # machine.print_memory(0, 120)
        self.assertEqual(machine.memory[arr - 1], 100)

        for i in range(0, 99):
            self.assertTrue(
                machine.memory[arr + i] <= machine.memory[arr + i + 1],
                'Mem loc: {}, seed: {}'.format(arr + i, machine.seed))

        self.assertEqual(machine.registers[SP], 0xF000)
示例#15
0
from core.lexer import tokenize
from core.parser import parse
from core.lc3_converter import LC3Converter
from core.lc3_printer import gen_asm

import os.path

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage:\npython3 transmogrifier.py filename')
        exit(0)

    content = ''

    with open(sys.argv[-1], 'r') as filename:
        content = filename.read()

    basename, ext = os.path.splitext(sys.argv[-1])

    tokens = tokenize(content)
    ir_form, labels, ln_to_label, func_help = parse(tokens)

    lc3_converter = LC3Converter(ir_form, labels, ln_to_label, func_help)
    tree, table, str_table = lc3_converter.convert()

    output = gen_asm(tree, table, str_table)

    asm_file = basename + '.asm'

    with open(asm_file, 'w+') as af:
        af.write(output)