-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode.py
64 lines (50 loc) · 1.02 KB
/
opcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations
from enum import StrEnum, unique
@unique
class Opcode(StrEnum):
NOP = "nop"
HALT = "halt"
# Binary operators
ADD = "add"
SUB = "sub"
MUL = "mul"
DIV = "div"
MOD = "mod"
CMP = "cmp"
AND = "and"
OR = "or"
XOR = "xor"
# Unary operators
INC = "inc"
DEC = "dec"
NEG = "neg"
NOT = "not"
JMP = "jmp"
JZ = "jz"
JNZ = "jnz"
JS = "js"
JNS = "jns"
CALL = "call"
RET = "ret"
INPUT = "input"
OUTPUT = "output"
PUSH = "push"
POP = "pop"
SWAP = "swap"
DUP = "dup"
OVER = "over"
OVER3 = "over3"
LOAD = "load"
STORE = "store"
DEBUG = "debug"
def __init__(self, mnemonic: str):
self.mnemonic = mnemonic
def __str__(self):
return str(self.value)
@classmethod
def from_string(cls, value: str):
value = value.lower()
for opcode in cls:
if opcode.mnemonic == value:
return opcode
return None