-
Notifications
You must be signed in to change notification settings - Fork 0
/
op.py
43 lines (31 loc) · 1007 Bytes
/
op.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
from abc import ABC, abstractmethod
from typing import NamedTuple, Optional, List
from belt import Belt
from pretty import Pretty
from vm import VM
class Instruction(ABC):
@abstractmethod
def run(self, vm: VM) -> Optional['Break']:
pass
class Opcode(ABC):
@abstractmethod
def name(self) -> str:
pass
@abstractmethod
def prefix(self) -> int:
pass
@abstractmethod
def instruction(self, payload) -> Instruction:
pass
class Break(NamedTuple):
depth: int
is_continue: bool
class Block(Pretty):
def __init__(self, instructions: List[Instruction]) -> None:
self._instructions = instructions
def run(self, vm: VM) -> Optional['Break']:
for ins in self._instructions:
br = ins.run(vm)
print(ins, [vm.belt().get_num(i).value.expect_int() for i in range(Belt.SIZE)])
if br is not None and br.depth > 0:
return Break(br.depth - 1, is_continue=br.is_continue)