aboutsummaryrefslogtreecommitdiffstats
path: root/asm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2023-02-11 14:28:43 +0000
committerGravatar Peter McGoron 2023-02-11 14:28:43 +0000
commit582a0a2c9a476abe9dec3410ff4f555433ed9e0f (patch)
tree422878d94f091f440143348d7f52fcfa42b294dc /asm
parenttest pop (diff)
enable compiling and add compile test
Diffstat (limited to 'asm')
-rw-r--r--asm/ffi.py25
-rw-r--r--asm/test.py43
2 files changed, 34 insertions, 34 deletions
diff --git a/asm/ffi.py b/asm/ffi.py
index 39d9800..a1907c6 100644
--- a/asm/ffi.py
+++ b/asm/ffi.py
@@ -13,6 +13,20 @@ class CompileRet(Enum):
LABEL_OVERFLOW = 7
TYPE_ERROR = 8
CLEARED_INSTRUCTION = 9
+ PROGRAM_OVERFLOW = 10
+
+class RunRet(Enum):
+ CONTINUE = 0
+ SYSCALL = 1
+ STOP = 2
+ STACK_OVERFLOW = 3
+ STACK_UNDERFLOW = 4
+ RUN_LABEL_OVERFLOW = 5
+ REGISTER_OVERFLOW = 6
+ UNKNOWN_OPCODE = 7
+
+ def is_halt(self):
+ return not (self == RunRet.CONTINUE or self == RunRet.SYSCALL)
class CIns(Structure):
_fields_ = [("opcode", c_int),
@@ -76,6 +90,17 @@ class Environment:
ret = dll.creole_compile(byref(self.cenv), byref(rd))
return CompileRet(ret)
+ def syscall(self, sc):
+ pass
+ def __call__(self):
+ sc = c_size_t()
+ ret = RunRet.CONTINUE
+ while not ret.is_halt():
+ ret = RunRet(dll.creole_step(byref(self.cenv), byref(sc)))
+ if ret == RunRet.SYSCALL:
+ self.syscall(sc)
+ return ret
+
class CParseLineException(Exception):
pass
def parse_line(line):
diff --git a/asm/test.py b/asm/test.py
index ba6d125..ca54f59 100644
--- a/asm/test.py
+++ b/asm/test.py
@@ -98,7 +98,6 @@ class PopTest(unittest.TestCase):
def test_compile_throw_pop_literal(self):
p = Program()
- ex = ffi.Environment()
with self.assertRaises(TypecheckException) as cm:
p.parse_asm_line("pop 6")
self.assertEqual(cm.exception.argtype, ArgType.REG)
@@ -108,7 +107,6 @@ class PopTest(unittest.TestCase):
def test_compile_throw_pop_label(self):
p = Program()
- ex = ffi.Environment()
with self.assertRaises(TypecheckException) as cm:
p.parse_asm_line("pop l9")
self.assertEqual(cm.exception.argtype, ArgType.REG)
@@ -128,45 +126,22 @@ class PopTest(unittest.TestCase):
p = Program()
with self.assertRaises(TypecheckLenException) as cm:
p.parse_asm_line("push")
- self.assertEqual(cm.exception.opcode, 2)
+ self.assertEqual(cm.exception.opcode, 1)
self.assertEqual(cm.exception.insargs, [])
self.assertEqual(cm.exception.argtypelen, 1)
class ProgramTest(unittest.TestCase):
- def test_two(self):
- p = Program()
- p.parse_asm_line("PUSH r1")
- p.parse_asm_line("ADD r1 5 6")
- b = p()
- self.assertEqual(b, b'\x01\xC2\x81\x00\x03\xC2\x81\xC0\x85\xC0\x86\x00')
- def test_label(self):
+ def test_exec_simple_reg(self):
p = Program()
- b_ex = bytes()
- p.parse_asm_line("CLB l0")
- b_ex = b_ex + b'\x07\xC0\x80\x00'
+ p.parse_asm_line("push 5")
+ p.parse_asm_line("push 6")
p.parse_asm_line("pop r0")
- b_ex = b_ex + b'\x02\xC2\x80\x00'
p.parse_asm_line("pop r1")
- b_ex = b_ex + b'\x02\xC2\x81\x00'
- p.parse_asm_line("mul r2 r0 r1")
- b_ex = b_ex + b'\x04\xC2\x82\xC2\x80\xC2\x81\x00'
- p.parse_asm_line("push r2")
- b_ex = b_ex + b'\x01\xC2\x82\x00'
- p.parse_asm_line("push r2")
- b_ex = b_ex + b'\x01\xC2\x82\x00'
- p.parse_asm_line("jl l0 r2 10")
- b_ex = b_ex + b'\x06\xC0\x80\xC2\x82\xC0\x8A\x00'
- b = p()
- self.assertEqual(b, b_ex)
-
- def test_parse_imm(self):
- p = Program()
- p.parse_asm_line("add r1 23 3648")
- ins = ffi.parse_line(p())
- self.assertEqual(ins[0], Instruction.ADD.opcode)
- self.assertEqual(ins[1][0], (1,1))
- self.assertEqual(ins[1][1], (0,23))
- self.assertEqual(ins[1][2], (0,3648))
+ ex = ffi.Environment()
+ self.assertEqual(ex.load(p()), ffi.CompileRet.OK)
+ self.assertEqual(ex(), ffi.RunRet.STOP)
+ self.assertEqual(ex.cenv.reg[0], 6)
+ self.assertEqual(ex.cenv.reg[1], 5)
if __name__ == "__main__":
unittest.main()