diff options
| author | 2023-02-07 16:48:22 +0000 | |
|---|---|---|
| committer | 2023-02-07 16:48:22 +0000 | |
| commit | 7cc44cf730a2c7ce780090d17cd68330f95fa5ab (patch) | |
| tree | 97405082c3bcdd4cd45820dae5e68e9f1f4a464e | |
| parent | start assembler (diff) | |
asm.py: write
| -rw-r--r-- | asm.py | 35 |
1 files changed, 25 insertions, 10 deletions
@@ -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 = [] |
