diff options
| author | 2023-02-09 16:32:32 +0000 | |
|---|---|---|
| committer | 2023-02-09 16:32:32 +0000 | |
| commit | ef84135a2e243ee8edf6f915405e2f5b65ab7500 (patch) | |
| tree | ce9a682df7f0da4f68cd07d4c5f60176c852251e /asm/creole.py | |
| parent | add compile t est (diff) | |
change instructions to be an enum
Diffstat (limited to 'asm/creole.py')
| -rw-r--r-- | asm/creole.py | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/asm/creole.py b/asm/creole.py index 1ed25e8..1d105a1 100644 --- a/asm/creole.py +++ b/asm/creole.py @@ -50,13 +50,23 @@ class TypecheckException(Exception): self.opcode = opcode def __str__(self): return f'opcode {self.opcode} has invalid value {self.sarg} (expected {self.argtype} in position {self.i}' -class Instruction: - def __init__(self, opcode, argtypes): +class Instruction(Enum): + NOP = 0 + PUSH = 1, ArgType.VAL + POP = 2, ArgType.REG + ADD = 3, ArgType.VAL, ArgType.VAL, ArgType.VAL + MUL = 4, ArgType.VAL, ArgType.VAL, ArgType.VAL + DIV = 5, ArgType.VAL, ArgType.VAL, ArgType.VAL + JL = 6, ArgType.LAB, ArgType.VAL, ArgType.VAL + CLB = 7, ArgType.LAB + SYS = 8, ArgType.VAL + + def __init__(self, opcode, *args): if opcode > 0x7F or opcode < 0: raise OpcodeException(opcode) self.opcode = opcode - self.argtypes = argtypes + self.argtypes = args def typecheck(self, sargs): rargs = [] if len(sargs) != len(self.argtypes): @@ -71,18 +81,6 @@ class Instruction: rargs.append(t) return rargs -instructions = { -"nop" : Instruction(0, []), -"push" : Instruction(1, [ArgType.VAL]), -"pop" : Instruction(2, [ArgType.REG]), -"add" : Instruction(3, [ArgType.VAL, ArgType.VAL, ArgType.VAL]), -"mul" : Instruction(4, [ArgType.VAL, ArgType.VAL, ArgType.VAL]), -"div" : Instruction(5, [ArgType.VAL, ArgType.VAL, ArgType.VAL]), -"jl" : Instruction(6, [ArgType.LAB, ArgType.VAL, ArgType.VAL]), -"clb" : Instruction(7, [ArgType.LAB]), -"sys" : Instruction(8, [ArgType.VAL]) -} - encoding_types = { # start mask A B 2: (0x7F, 0xC0, 7), @@ -165,12 +163,14 @@ class Program: def parse_asm_line(self, line): line = line.split() line[0] = line[0].casefold() - if line[0] not in instructions: + try: + # TODO: is there no better way to do this in Python? + ins = getattr(Instruction, line[0].upper()) + except Exception as e: raise InstructionNotFoundException(line[0]) - else: - ins = instructions[line[0]] - args_w_type = ins.typecheck(line[1:]) - self.asm_push_line(ins.opcode, args_w_type) + + args_w_type = ins.typecheck(line[1:]) + self.asm_push_line(ins.opcode, args_w_type) def __call__(self): b = bytes() |
