diff options
| author | 2023-02-20 18:45:21 +0000 | |
|---|---|---|
| committer | 2023-02-20 18:45:21 +0000 | |
| commit | 3dd29b100483cb9eb34d96f6ed21827e86cd77a2 (patch) | |
| tree | b07bf066bad51f43c0225da682631ddde9f4e5fb /asm/test.py | |
| parent | adjust python assembler to new API (diff) | |
add label parser that can deal with forward jumps; all tests pass
Diffstat (limited to '')
| -rw-r--r-- | asm/test.py | 98 |
1 files changed, 36 insertions, 62 deletions
diff --git a/asm/test.py b/asm/test.py index f2c2277..cae3d36 100644 --- a/asm/test.py +++ b/asm/test.py @@ -30,17 +30,17 @@ class PushTest(unittest.TestCase): def test_parse_push_catch_typecheck_push_lab(self): p = Program() with self.assertRaises(TypecheckException) as cm: - p.parse_asm_line("push l0") + p.parse_asm_line("push .l0") self.assertEqual(cm.exception.argtype, ArgType.VAL) - self.assertEqual(cm.exception.sarg, 'l0') + self.assertEqual(cm.exception.sarg, '.l0') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 1) + self.assertEqual(cm.exception.opcode, Instruction.PUSH) def test_parse_push_catch_typecheck_argument_overflow(self): p = Program() with self.assertRaises(TypecheckLenException) as cm: p.parse_asm_line("push r1 r2") - self.assertEqual(cm.exception.opcode, 1) + self.assertEqual(cm.exception.opcode, Instruction.PUSH) self.assertEqual(cm.exception.insargs, ["r1", "r2"]) self.assertEqual(cm.exception.argtypelen, 1) @@ -48,7 +48,7 @@ class PushTest(unittest.TestCase): p = Program() with self.assertRaises(TypecheckLenException) as cm: p.parse_asm_line("push") - self.assertEqual(cm.exception.opcode, 1) + self.assertEqual(cm.exception.opcode, Instruction.PUSH) self.assertEqual(cm.exception.insargs, []) self.assertEqual(cm.exception.argtypelen, 1) @@ -64,23 +64,6 @@ class PushTest(unittest.TestCase): p.parse_asm_line("PUSH 6") ex = ffi.Environment(p()) - self.assertEqual(ex.cenv.prgend, 2) - - self.assertEqual(ex.cenv.prg[0].opcode, 1) - self.assertEqual(ex.cenv.prg[0].w_flags[0], 1) - self.assertEqual(ex.cenv.prg[0].w_flags[1], 0) - self.assertEqual(ex.cenv.prg[0].w_flags[2], 0) - self.assertEqual(ex.cenv.prg[0].w[0], 0) - self.assertEqual(ex.cenv.prg[0].w[1], 0) - self.assertEqual(ex.cenv.prg[0].w[2], 0) - - self.assertEqual(ex.cenv.prg[1].opcode, 1) - self.assertEqual(ex.cenv.prg[1].w_flags[0], 0) - self.assertEqual(ex.cenv.prg[1].w_flags[1], 0) - self.assertEqual(ex.cenv.prg[1].w_flags[2], 0) - self.assertEqual(ex.cenv.prg[1].w[0], 6) - self.assertEqual(ex.cenv.prg[1].w[1], 0) - self.assertEqual(ex.cenv.prg[1].w[2], 0) def test_push_many(self): p = Program() @@ -107,15 +90,6 @@ class PopTest(unittest.TestCase): b = p() self.assertEqual(b, b'\x02\xC2\x89\x00') ex = ffi.Environment(b) - self.assertEqual(ex.cenv.prgend, 1) - - self.assertEqual(ex.cenv.prg[0].opcode, 2) - self.assertEqual(ex.cenv.prg[0].w_flags[0], 1) - self.assertEqual(ex.cenv.prg[0].w_flags[1], 0) - self.assertEqual(ex.cenv.prg[0].w_flags[2], 0) - self.assertEqual(ex.cenv.prg[0].w[0], 9) - self.assertEqual(ex.cenv.prg[0].w[1], 0) - self.assertEqual(ex.cenv.prg[0].w[2], 0) def test_compile_throw_pop_literal(self): p = Program() @@ -124,22 +98,22 @@ class PopTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.REG) self.assertEqual(cm.exception.sarg, '6') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 2) + self.assertEqual(cm.exception.opcode, Instruction.POP) def test_compile_throw_pop_label(self): p = Program() with self.assertRaises(TypecheckException) as cm: - p.parse_asm_line("pop l9") + p.parse_asm_line("pop .l9") self.assertEqual(cm.exception.argtype, ArgType.REG) - self.assertEqual(cm.exception.sarg, 'l9') + self.assertEqual(cm.exception.sarg, '.l9') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 2) + self.assertEqual(cm.exception.opcode, Instruction.POP) def test_compile_throw_argument_overflow(self): p = Program() with self.assertRaises(TypecheckLenException) as cm: p.parse_asm_line("pop r1 r2") - self.assertEqual(cm.exception.opcode, 2) + self.assertEqual(cm.exception.opcode, Instruction.POP) self.assertEqual(cm.exception.insargs, ["r1", "r2"]) self.assertEqual(cm.exception.argtypelen, 1) @@ -147,7 +121,7 @@ class PopTest(unittest.TestCase): p = Program() with self.assertRaises(TypecheckLenException) as cm: p.parse_asm_line("pop") - self.assertEqual(cm.exception.opcode, 2) + self.assertEqual(cm.exception.opcode, Instruction.POP) self.assertEqual(cm.exception.insargs, []) def test_pop_underflow(self): @@ -189,7 +163,7 @@ class AddTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.REG) self.assertEqual(cm.exception.sarg, '5') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 3) + self.assertEqual(cm.exception.opcode, Instruction.ADD) def test_exec_add_throw_lab_1(self): p = Program() @@ -198,7 +172,7 @@ class AddTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.VAL) self.assertEqual(cm.exception.sarg, '.label') self.assertEqual(cm.exception.i, 1) - self.assertEqual(cm.exception.opcode, 3) + self.assertEqual(cm.exception.opcode, Instruction.ADD) def test_exec_add_throw_lab_2(self): p = Program() @@ -207,7 +181,7 @@ class AddTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.VAL) self.assertEqual(cm.exception.sarg, '.ab') self.assertEqual(cm.exception.i, 2) - self.assertEqual(cm.exception.opcode, 3) + self.assertEqual(cm.exception.opcode, Instruction.ADD) class MulTest(unittest.TestCase): def test_exec_mul_imm_imm(self): @@ -233,25 +207,25 @@ class MulTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.REG) self.assertEqual(cm.exception.sarg, '942') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 4) + self.assertEqual(cm.exception.opcode, Instruction.MUL) def test_exec_mul_throw_lab_1(self): p = Program() with self.assertRaises(TypecheckException) as cm: - p.parse_asm_line("mul r9 l2 1991") + p.parse_asm_line("mul r9 .l2 1991") self.assertEqual(cm.exception.argtype, ArgType.VAL) - self.assertEqual(cm.exception.sarg, 'l2') + self.assertEqual(cm.exception.sarg, '.l2') self.assertEqual(cm.exception.i, 1) - self.assertEqual(cm.exception.opcode, 4) + self.assertEqual(cm.exception.opcode, Instruction.MUL) def test_exec_mul_throw_lab_2(self): p = Program() with self.assertRaises(TypecheckException) as cm: - p.parse_asm_line("mul r0 -11 l48") + p.parse_asm_line("mul r0 -11 .l48") self.assertEqual(cm.exception.argtype, ArgType.VAL) - self.assertEqual(cm.exception.sarg, 'l48') + self.assertEqual(cm.exception.sarg, '.l48') self.assertEqual(cm.exception.i, 2) - self.assertEqual(cm.exception.opcode, 4) + self.assertEqual(cm.exception.opcode, Instruction.MUL) class DivTest(unittest.TestCase): def test_div(self): @@ -295,7 +269,7 @@ class DivTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.REG) self.assertEqual(cm.exception.sarg, '5') self.assertEqual(cm.exception.i, 0) - self.assertEqual(cm.exception.opcode, 5) + self.assertEqual(cm.exception.opcode, Instruction.DIV) def test_exec_div_throw_lab_1(self): p = Program() @@ -304,7 +278,7 @@ class DivTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.VAL) self.assertEqual(cm.exception.sarg, '.qqweq') self.assertEqual(cm.exception.i, 1) - self.assertEqual(cm.exception.opcode, 5) + self.assertEqual(cm.exception.opcode, Instruction.DIV) def test_exec_div_throw_lab_2(self): p = Program() @@ -313,7 +287,7 @@ class DivTest(unittest.TestCase): self.assertEqual(cm.exception.argtype, ArgType.VAL) self.assertEqual(cm.exception.sarg, '.24') self.assertEqual(cm.exception.i, 2) - self.assertEqual(cm.exception.opcode, 5) + self.assertEqual(cm.exception.opcode, Instruction.DIV) class LabelTest(unittest.TestCase): def test_unconditional_jump(self): @@ -321,9 +295,9 @@ class LabelTest(unittest.TestCase): p.parse_lines([ "mov r0 5", "mov r0 6", - "j l0", + "j .l0", "mov r0 7", - "CLB l0", + ".l0", ]) ex = ffi.Environment(p()) self.assertEqual(ex(), ffi.RunRet.STOP) @@ -334,10 +308,10 @@ class LabelTest(unittest.TestCase): p.parse_lines([ "add r0 10 0", "add r1 20 0", - "CLB l0", + ".loop_head", "add r0 r0 -1", "add r1 r1 1", - "jl l0 0 r0" + "jl .loop_head 0 r0" ]) ex = ffi.Environment(p()) self.assertEqual(ex(), ffi.RunRet.STOP) @@ -349,10 +323,10 @@ class LabelTest(unittest.TestCase): p.parse_lines([ "mov r0 30", "mov r1 0", - "CLB l0", + ".l0", "add r0 r0 -1", "add r1 r1 1", - "jls l0 -30 r0" + "jls .l0 -30 r0" ]) ex = ffi.Environment(p()) self.assertEqual(ex(), ffi.RunRet.STOP) @@ -364,11 +338,11 @@ class LabelTest(unittest.TestCase): p.parse_lines([ "mov r0 50", "mov r1 0", - "CLB l0", + ".l0", "add r1 r1 1", "mul r2 r0 -1", "add r2 r2 r1", - "jne l0 r2 0" + "jne .l0 r2 0" ]) ex = ffi.Environment(p()) self.assertEqual(ex(), ffi.RunRet.STOP) @@ -381,14 +355,14 @@ class LabelTest(unittest.TestCase): p.parse_lines([ "mov r0 0", # outer loop counter "mov r2 0", # total iteration counter - "CLB l0", + ".outer_loop", "mov r1 0", # inner loop counter - "CLB l1", + ".inner_loop", "add r1 r1 1", "add r2 r2 1", - "jl l1 r1 50", + "jl .inner_loop r1 50", "add r0 r0 1", - "jl l0 r0 50" + "jl .outer_loop r0 50" ]) ex = ffi.Environment(p()) self.assertEqual(ex(), ffi.RunRet.STOP) |
