We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.
扌宀 machine
扌宀 machine is designed by PSTF.
Definition
扌宀 machine is a variant of S^A machine.
A 扌宀 machine consists of an infinite tape and the following 8 operations.
- Increase the value of current cell(hereinafter referred as N) by a where a is a positive integer
- Decrease the value of N by a where a is a positive integer if N>0 else do nothing
- Swap the values of cell #x and cell #y where both x and y are natural numbers
- Move to cell #x where x is a natural number
- If N is 0, jump to line x, where x is a positive integer. Note: The program will stop if x exceeds the actual total number of lines.
- Transfer the value of N to the screen
- Transfer the input value to N
- Jump to line x, where x is a positive integer. Note: The program will stop if x exceeds the actual total number of lines.
As the definition shown, any S^A machine can be represented by these operations:
1 + a 2 - a 3 % x y 4 # x 5 ? x 6 . 7 , 8 ! x
Computational Class Proof
Since 扌宀 machine is a variant of S^A machine and might be a superset of S^A machine, so it is also Turing-complete.
It can be used to simulate Brainfuck, and is also a programming machine.
Example
Cat Program
, ? 6 // This program consists only 5 lines . , ! 2
Hello, World!
Note, the input and output mode used here is character input and output.
+ 11 ? 18 > 1 + 6 > 2 + 9 > 3 + 8 > 4 + 4 > 5 + 3 > 6 + 1 > 0 - 1 ! 2 > 1 + 6 . > 2 + 2 . + 7 . . + 3 . > 3 . > 4 - 1 . > 2 - 1 . > 1 . + 3 . - 6 . - 8 . > 4 + 1 . > 5 + 1 .
Implementation
This was implemented in Python, written by another AI assistant called Kimi.
class CellMachine:
"""
单元格计算模型
- 无限长磁带(用字典稀疏存储,默认值为 0)
- 支持 8 条指令
"""
def __init__(self):
self.tape = {} # 无限磁带:{单元格编号: 值}
self.pointer = 0 # 当前单元格指针
self.pc = 0 # 程序计数器(0-based 索引)
self.program = [] # 指令列表
self.input_queue = [] # 输入队列
self.output = [] # 输出记录
self.running = False
# ---------- 辅助方法 ----------
def get(self, cell_idx=None):
"""读取单元格值,默认为当前单元格"""
idx = self.pointer if cell_idx is None else cell_idx
return self.tape.get(idx, 0)
def set(self, value, cell_idx=None):
"""写入单元格,默认为当前单元格"""
idx = self.pointer if cell_idx is None else cell_idx
self.tape[idx] = value
# ---------- 核心指令 ----------
def exec_add(self, a):
"""将当前单元格的值增加 a"""
self.set(self.get() + a)
self.pc += 1
def exec_sub(self, a):
"""将当前单元格的值减少 a(仅当 N > 0 时执行)"""
if self.get() > 0:
self.set(self.get() - a)
self.pc += 1
def exec_swap(self, x, y):
"""交换单元格 #x 和单元格 #y 的值"""
val_x = self.get(x)
val_y = self.get(y)
self.set(val_y, x)
self.set(val_x, y)
self.pc += 1
def exec_mov(self, x):
"""移动到单元格 #x"""
self.pointer = x
self.pc += 1
def exec_jz(self, x):
"""如果 N 为 0,则跳转到第 x 行(1-based)"""
if self.get() == 0:
self.pc = x - 1 # 转换为 0-based 索引
else:
self.pc += 1
def exec_out(self):
"""将 N 的值传输到屏幕(输出)"""
self.output.append(self.get())
print(self.get(), end=' ')
self.pc += 1
def exec_in(self):
"""将输入值传输到 N"""
if self.input_queue:
self.set(self.input_queue.pop(0))
else:
# 无输入时默认置 0(或可改为阻塞等待)
self.set(0)
self.pc += 1
def exec_jmp(self, x):
"""无条件跳转到第 x 行(1-based)"""
self.pc = x - 1
# ---------- 程序加载与执行 ----------
def load(self, program_lines):
"""
加载程序
program_lines: 字符串列表,每条格式为 "指令 参数1 参数2"
"""
self.program = []
for line in program_lines:
line = line.strip()
if not line or line.startswith('#'):
continue
self.program.append(line)
self.pc = 0
def step(self):
"""执行单条指令,返回是否继续运行"""
if not (0 <= self.pc < len(self.program)):
return False
parts = self.program[self.pc].split()
cmd = parts[0].upper()
args = [int(p) for p in parts[1:]]
if cmd == '+':
self.exec_add(*args)
elif cmd == '-':
self.exec_sub(*args)
elif cmd == '%':
self.exec_swap(*args)
elif cmd == '>':
self.exec_mov(*args)
elif cmd == '?':
self.exec_jz(*args)
elif cmd == '.':
self.exec_out()
elif cmd == ',':
self.exec_in()
elif cmd == '!':
self.exec_jmp(*args)
else:
raise ValueError(f"未知指令: {cmd} (第 {self.pc + 1} 行)")
return True
def run(self, inputs=None):
"""
运行程序
inputs: 可选的输入值列表
返回: 输出值列表
"""
if inputs:
self.input_queue = list(inputs)
self.output = []
self.running = True
while self.step():
pass
self.running = False
return self.output
def reset(self):
"""重置机器状态"""
self.tape.clear()
self.pointer = 0
self.pc = 0
self.input_queue = []
self.output = []
self.running = False
# ==================== 示例演示 ====================
if __name__ == "__main__":
machine = CellMachine()
# 示例 1: 计算 3 + 5 并输出
# 单元格 0 放 3,单元格 1 放 5,相加后输出
program1 = [
"> 0", # 第 1 行
"+ 3", # 第 2 行
"> 1", # 第 3 行
"+ 5", # 第 4 行
"% 0 1", # 第 5 行
"> 0", # 第 6 行
"+ 1", # 第 7 行(将单元格 1 的值加到单元格 0)
".", # 第 8 行
]
print("示例 1: 3 + 5 =")
machine.load(program1)
machine.run()
print(f"\n输出记录: {machine.output}\n")
# 示例 2: 循环递减输出(从 5 倒数到 1)
# 单元格 0 作为计数器,初始值 5
program2 = [
"> 0", # 第 1 行
"+ 5", # 第 2 行(初始化计数器)
".", # 第 3 行(输出当前值)
"- 1", # 第 4 行(减 1)
"? 7", # 第 5 行(如果为 0,跳到结束)
"! 3", # 第 6 行(跳回输出)
".", # 第 7 行(输出最后的 0)
]
print("示例 2: 从 5 倒数到 0:")
machine.reset()
machine.load(program2)
machine.run()
print(f"\n输出记录: {machine.output}\n")
# 示例 3: 输入处理(输入一个数,加 20 后输出)
program3 = [
",", # 第 1 行
"+ 20", # 第 2 行
".", # 第 3 行
]
print("示例 3: 输入一个数,加 20:")
machine.reset()
machine.load(program3)
machine.run(inputs=[10])
print(f"\n输出记录: {machine.output}")