from typing import Any
import json as pyjson
import duck
+import TapeMachine
+
import builtins
+p = builtins.print
+
# ----------------------------------------------------------------------
def to_JSON_TM(
"""
tape = tm.tape()
- tape_data = [tm.tape()[n] for n in range(0 ,len(tape.length))]
+ tape_data = [tape[n] for n in range(len(tape))]
obj = {
"tape" : tape_data
- ,"head" : tm.address()
- ,"extent": tm.extent
+ ,"head" : tm.where()
+ ,"extent": tm.extent()
}
- return pyjson.dumps(tm ,ensure_ascii=False)
+ return pyjson.dumps(obj ,ensure_ascii=False)
# ----------------------------------------------------------------------
out = pyjson.dumps(parsed ,indent=2 ,ensure_ascii=False)
prefix = " " * indent
for line in out.split("\n"):
- print(prefix + line)
+ p(prefix + line)
# ----------------------------------------------------------------------
while True:
v = tm1.read()
- if tm1.address() == tm0.address():
+ if tm1.where() == tm0.where():
mark = "*"
elif v is None:
mark = "␀"
mark = " "
s = "" if v is None else str(v)
- print(f"{prefix}{mark}{s}")
+ p(f"{prefix}{mark}{s}")
if not tm1.can_step(): break
tm1.step()
read
can_step
step
- address
+ where
These are exactly what pretty_TM() uses.
"""
,"read"
,"can_step"
,"step"
- ,"address"
+ ,"where"
)
# ----------------------------------------------------------------------
print_TM(value ,indent)
else:
prefix = " " * indent
- print(prefix + str(value))
+ p(prefix + str(value))
# ----------------------------------------------------------------------
# JSON primitive: str, int, float, bool, None
if isinstance(value ,(str ,int ,float ,bool)) or value is None:
- print(prefix + str(value))
+ p(prefix + str(value))
return
# Try to JSON-dump; if it fails, wrap in {"value": repr}
# Pretty-print JSON
out = pyjson.dumps(parsed ,indent=2 ,ensure_ascii=False)
for line in out.split("\n"):
- print(prefix + line)
+ p(prefix + line)
- print
- pretty
"""
- p = builtins.print
- tm = TM_ND_A([1 ,None ,"three"])
+ tm = TapeMachine.TM_ND_A([1 ,None ,"three"])
p("json.is_a_TM examples:")
p(" is_a_TM(tm) ->" ,is_a_TM(tm))
def write(self ,value: Any ,n: int = 0) -> None: self._tape[self._head_position + n] = value
# because this is an ND machine it is safe to entangle heads
- def entangle(self) -> "TapeMachine":
- """Create an entangled copy: shared tape, independent head_position."""
- new_tm = object.__new__(TapeMachine)
- new_tm._tape = self._tape
- new_tm._length = self._length
- new_tm._head_position = self._head_position
- return new_tm
+ def entangle(self) -> "TM_ND_A":
+ tm = object.__new__(TM_ND_A)
+ tm._tape = self._tape
+ tm._extent = self._extent
+ tm._head_position = self._head_position
+ return tm