Clean up codes
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
from typing import Dict, Optional, Tuple, List, Any
|
||||
|
||||
class MaMa:
|
||||
def __init__(self, prog: Dict[int, str] | List[str],
|
||||
stack: Dict[int, int] | List[int] | None = None) -> None:
|
||||
self.steps = 0
|
||||
self.prog = {i: instr for i, instr in enumerate(prog)} if isinstance(prog, list) else prog
|
||||
self.p_prog = 0
|
||||
self.stack: Dict[int, int] = {}
|
||||
self.p_stack = -1
|
||||
def __init__(self, prog: Dict[int, str] | List[str], stack: Dict[int, int] | List[int] | None = None) -> None:
|
||||
self.step_counter = 0
|
||||
self.halted = False
|
||||
|
||||
# Init prog from list or dict
|
||||
self.p_prog = 0
|
||||
if isinstance(prog, list):
|
||||
self.prog = {i: micro for i, micro in enumerate(prog)}
|
||||
else:
|
||||
self.prog = prog
|
||||
|
||||
# Default stack
|
||||
self.p_stack = -1
|
||||
self.stack: Dict[int, int] = {}
|
||||
|
||||
# Init custom stack from list or dict
|
||||
if stack is not None:
|
||||
if isinstance(stack, list):
|
||||
self.stack = {i: v for i, v in enumerate(stack)}
|
||||
@@ -17,8 +25,7 @@ class MaMa:
|
||||
self.p_stack = max(self.stack.keys(), default=-1)
|
||||
self.initial_stack = dict(self.stack)
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
||||
# Runs the machine and returns execution journal
|
||||
def run(self, max_steps: int = 1000) -> List[Dict[str, int | str | dict | list]]:
|
||||
steps = 0
|
||||
# always insert init configuration as step 0
|
||||
@@ -28,48 +35,48 @@ class MaMa:
|
||||
steps += 1
|
||||
return journal
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
||||
# Returns the full program
|
||||
def structure(self) -> Dict[int, Dict[str, Any]]:
|
||||
return {i: {"call": call, "macros": []} for i, call in sorted(self.prog.items())}
|
||||
return {i: {"micro": micro, "macros": []} for i, micro in sorted(self.prog.items())}
|
||||
|
||||
# Decodes string MaMa instructions to function callables
|
||||
@staticmethod
|
||||
def decode(call: str) -> Tuple[str, Optional[List[int]]]:
|
||||
if "(" in call:
|
||||
name, rest = call.split("(", 1)
|
||||
def decode(micro: str) -> Tuple[str, Optional[List[int]]]:
|
||||
if "(" in micro:
|
||||
name, rest = micro.split("(", 1)
|
||||
args_str = rest[:-1]
|
||||
if args_str.strip() == "":
|
||||
return name, []
|
||||
args = [int(a.strip()) for a in args_str.split(",")]
|
||||
return name, args
|
||||
return call, None
|
||||
return micro, None
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
||||
def config(self, call: str) -> Dict[str, int | str | dict | list]:
|
||||
# Generates journal entry for a step
|
||||
def config(self, micro: str) -> Dict[str, int | str | dict | list]:
|
||||
return {
|
||||
"step": self.steps,
|
||||
"call": call,
|
||||
"step": self.step_counter,
|
||||
"micro": micro,
|
||||
"p_prog": self.p_prog,
|
||||
"p_stack": self.p_stack,
|
||||
"stack": dict(self.stack),
|
||||
}
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
||||
# Executes one step of the machine
|
||||
def __step(self) -> Dict[str, int | str | dict | list]:
|
||||
if self.halted or self.p_prog not in self.prog:
|
||||
self.halted = True
|
||||
return self.config("halted")
|
||||
|
||||
call = self.prog[self.p_prog]
|
||||
name, args = MaMa.decode(call)
|
||||
micro = self.prog[self.p_prog]
|
||||
name, args = MaMa.decode(micro)
|
||||
method = getattr(self, f"_{name}", None)
|
||||
if method is None:
|
||||
raise ValueError(f"Unknown instruction: {call}")
|
||||
raise ValueError(f"Unknown instruction: {micro}")
|
||||
method(args)
|
||||
self.steps += 1
|
||||
return self.config(call)
|
||||
self.step_counter += 1
|
||||
return self.config(micro)
|
||||
|
||||
# DEFINE MaMa Micros
|
||||
|
||||
# Stop execution
|
||||
def _stop(self, _: Optional[int]) -> None:
|
||||
|
||||
Reference in New Issue
Block a user