asm.py: write

This commit is contained in:
Peter McGoron 2023-02-07 16:48:22 +00:00
parent a7308280cb
commit 7cc44cf730
1 changed files with 25 additions and 10 deletions

35
asm.py
View File

@ -11,15 +11,15 @@ class ArgType(Enum):
def gettype(s):
if s.isnumeric():
return (TYPE_IMM, int(s))
elif s[0] == 'r' and is_num(s[1:]):
elif s[0] == 'r' and s[1:].isnumeric():
return (TYPE_REG, int(s[1:]))
elif s[0] == 'l' and is_num(s[1:]):
elif s[0] == 'l' and s[1:].isnumeric():
return (TYPE_LAB, int(s[1:]))
else:
return None
def typecheck(self, s):
t = gettype(s)
t = ArgType.gettype(s)
if t is None:
return None
if self == TYPE_VAL:
@ -86,24 +86,39 @@ def encode_pseudo_utf8(n, high_bits, to):
all_bytes.append(start_byte | n)
return bytes(reversed(all_bytes))
"""
class Line:
def __init__(self, opcode, args):
self.opcode = opcode
self.args = args
def render(self):
def __call__(self):
b = bytes([self.opcode])
for a in args:
if a[0] == TYPE_REG:
b = b + encode_pseudo_utf8(a[1],1,None)
else:
b = b + encode_pseudo_utf8(a[1],0,None)
return b + bytes([0])
class Program:
def asm_push_line(self, ins, args):
self.asm.append((ins, args))
self.asm.append(Line(ins, args))
def parse_asm_line(self, line):
line = line.split(' ')
line = line.split()
if line[0] not in instructions:
raise Exception
else:
r = instructions[line[0]].typecheck(line[1:])
ins = instructions[line[0]]
args_w_type = ins.typecheck(line[1:])
if r is None:
raise Exception
asm_push_instruction(instructions[line[0]].opcode, r)
"""
self.asm_push_line(ins.opcode, args_w_type)
def __call__(self):
b = bytes()
for line in self.asm:
b = b + line()
return b
def __init__(self):
self.asm = []