asm.py: write
This commit is contained in:
parent
a7308280cb
commit
7cc44cf730
35
asm.py
35
asm.py
|
@ -11,15 +11,15 @@ class ArgType(Enum):
|
||||||
def gettype(s):
|
def gettype(s):
|
||||||
if s.isnumeric():
|
if s.isnumeric():
|
||||||
return (TYPE_IMM, int(s))
|
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:]))
|
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:]))
|
return (TYPE_LAB, int(s[1:]))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def typecheck(self, s):
|
def typecheck(self, s):
|
||||||
t = gettype(s)
|
t = ArgType.gettype(s)
|
||||||
if t is None:
|
if t is None:
|
||||||
return None
|
return None
|
||||||
if self == TYPE_VAL:
|
if self == TYPE_VAL:
|
||||||
|
@ -86,24 +86,39 @@ def encode_pseudo_utf8(n, high_bits, to):
|
||||||
all_bytes.append(start_byte | n)
|
all_bytes.append(start_byte | n)
|
||||||
return bytes(reversed(all_bytes))
|
return bytes(reversed(all_bytes))
|
||||||
|
|
||||||
"""
|
|
||||||
class Line:
|
class Line:
|
||||||
def __init__(self, opcode, args):
|
def __init__(self, opcode, args):
|
||||||
self.opcode = opcode
|
self.opcode = opcode
|
||||||
self.args = args
|
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:
|
class Program:
|
||||||
def asm_push_line(self, ins, args):
|
def asm_push_line(self, ins, args):
|
||||||
self.asm.append((ins, args))
|
self.asm.append(Line(ins, args))
|
||||||
|
|
||||||
def parse_asm_line(self, line):
|
def parse_asm_line(self, line):
|
||||||
line = line.split(' ')
|
line = line.split()
|
||||||
if line[0] not in instructions:
|
if line[0] not in instructions:
|
||||||
raise Exception
|
raise Exception
|
||||||
else:
|
else:
|
||||||
r = instructions[line[0]].typecheck(line[1:])
|
ins = instructions[line[0]]
|
||||||
|
args_w_type = ins.typecheck(line[1:])
|
||||||
if r is None:
|
if r is None:
|
||||||
raise Exception
|
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 = []
|
||||||
|
|
Loading…
Reference in New Issue