Esempio n. 1
0
class CodeTest(unittest.TestCase):
    def setUp(self):
        self.coder = Code()

    def testDest(self):
        self.assertEqual(0b000, self.coder.dest('null'))
        self.assertEqual(0b001, self.coder.dest('M'))

    def testComp(self):
        self.assertEqual(0b0101010, self.coder.comp('0'))
        self.assertEqual(0b0001111, self.coder.comp('-D'))

    def testJump(self):
        self.assertEqual(0b000, self.coder.jump('null'))
        self.assertEqual(0b101, self.coder.jump('JNE'))
Esempio n. 2
0
def second_pass(parser, symbols):
    is_variable = re.compile(u'^[^\d]')
    variable_address = 15
    coder = Code()
    translated = []

    parser.reset()
    while parser.hasMoreCommands:
        parser.advance()
        command_type = parser.commandType

        if command_type == 'A_COMMAND':
            address = parser.symbol
            if is_variable.match(address):
                if symbols.contains(address):
                    address = symbols.GetAddress(address)
                else:
                    variable_address = variable_address + 1
                    symbols.addEntry(address, variable_address)
                    address = variable_address
            else:
                address = int(address)
            translated.append(A_COMM_TMPL.format(address)[0:16])

        if command_type == 'C_COMMAND':
            comp = coder.comp(parser.comp)
            dest = coder.dest(parser.dest)
            jump = coder.jump(parser.jump)
            translated.append(C_COMM_TMPL.format(comp, dest, jump)[0:16])

    return '\n'.join(translated)
Esempio n. 3
0
class CodeTest(unittest.TestCase):

    def setUp(self):
        self.coder = Code()

    def testDest(self):
        self.assertEqual(0b000, self.coder.dest('null'))
        self.assertEqual(0b001, self.coder.dest('M'))

    def testComp(self):
        self.assertEqual(0b0101010, self.coder.comp('0'))
        self.assertEqual(0b0001111, self.coder.comp('-D'))

    def testJump(self):
        self.assertEqual(0b000, self.coder.jump('null'))
        self.assertEqual(0b101, self.coder.jump('JNE'))
Esempio n. 4
0
def second_pass(parser, symbols):
    is_variable = re.compile(u'^[^\d]')
    variable_address = 15
    coder = Code()
    translated = []

    parser.reset()
    while parser.hasMoreCommands:
        parser.advance()
        command_type = parser.commandType

        if command_type == 'A_COMMAND':
            address = parser.symbol
            if is_variable.match(address):
                if symbols.contains(address):
                    address = symbols.GetAddress(address)
                else:
                    variable_address = variable_address + 1
                    symbols.addEntry(address, variable_address)
                    address = variable_address
            else:
                address = int(address)
            translated.append(A_COMM_TMPL.format(address)[0:16])

        if command_type == 'C_COMMAND':
            comp = coder.comp(parser.comp)
            dest = coder.dest(parser.dest)
            jump = coder.jump(parser.jump)
            translated.append(C_COMM_TMPL.format(comp, dest, jump)[0:16])

    return '\n'.join(translated)
Esempio n. 5
0
 def test_jump(self):
     self.assertEqual(Code.jump('A=0'), '000')
     self.assertEqual(Code.jump('A=0;JGT'), '001')
     self.assertEqual(Code.jump('A=0;JEQ'), '010')
     self.assertEqual(Code.jump('A=0;JGE'), '011')
     self.assertEqual(Code.jump('A=0;JLT'), '100')
     self.assertEqual(Code.jump('A=0;JNE'), '101')
     self.assertEqual(Code.jump('A=0;JLE'), '110')
     self.assertEqual(Code.jump(';JMP'), '111')