add data parsing

This commit is contained in:
Peter McGoron 2023-02-21 17:42:51 +00:00
parent 5a09f481b3
commit 6e0d124efd
2 changed files with 22 additions and 3 deletions

View File

@ -72,10 +72,10 @@ class Argument:
class StringArgument(Argument): class StringArgument(Argument):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def __bytes__(self): def __call__(self):
b = bytes() b = bytes()
for v in self.val: for v in self.val:
b = b + Argument(ArgType.IMM, v)() b = b + Argument(ArgType.IMM, int(v, base=16))()
return b return b
class LabelArgument(Argument): class LabelArgument(Argument):
@ -350,6 +350,19 @@ class Line:
class InstructionNotFoundException(Exception): class InstructionNotFoundException(Exception):
pass pass
def _term_sep(s):
""" Split up the arguments of an instruction.
OP arg1 arg2 [data,data,data,...]
"""
s = s.strip()
s_data = s.split('[')
if len(s_data) == 2:
return s_data[0].split() + [s_data[1].rstrip('] \t\n\r\v').split(',')]
else:
return s.split()
class Program: class Program:
def _asm_push_line(self, ins, args): def _asm_push_line(self, ins, args):
l = Line(ins, args) l = Line(ins, args)
@ -361,7 +374,7 @@ class Program:
:param line: String containing the line. :param line: String containing the line.
:raises InstructionNotFoundException: :raises InstructionNotFoundException:
""" """
line = line.strip().split() line = _term_sep(line)
line[0] = line[0].casefold() line[0] = line[0].casefold()
if line[0][0] == '.': if line[0][0] == '.':
self.asm.append(line[0]) self.asm.append(line[0])

View File

@ -433,6 +433,12 @@ class ProgramTest(unittest.TestCase):
self.range_test(0x7FFFF00, 0x8000000, sgn=True) self.range_test(0x7FFFF00, 0x8000000, sgn=True)
self.range_test(-100, 0, sgn=True) self.range_test(-100, 0, sgn=True)
class DataTest(unittest.TestCase):
def test_parse_db(self):
p = Program()
p.parse_asm_line("db d0 [4d2,1234,0,5]")
self.assertEqual(p(), b'\x0b\xc0\x80\xe0\x93\x92\xf0\x81\x88\xb4\xc0\x80\xc0\x85\x00')
class SCEnv(ffi.Environment): class SCEnv(ffi.Environment):
def syscall(self, s): def syscall(self, s):
self.s = s.value self.s = s.value