我正在玩弄nim(在撰写本文时仍称为 nimrod),用该语言编写 Brainfuck 解释器。没有实现循环,我有:
import os, unsigned
const RamSize = 200
type
TRam = array[0..RamSize, int]
var
ram : TRam
ip : int = 0
dp : int = 0
proc readCode(path: string) =
var
f : TFile = open(path)
i : int = 0
while i < RamSize and not EndOfFile(f):
ram[i] = ord(readChar(f))
inc(i)
proc main(path: string) =
readCode(path)
while ip < RamSize:
case chr(ram[ip])
of '>' : inc dp
of '<' : dec dp
of '+' : inc ram[dp]
of '-' : dec ram[dp]
of '.' : write stdout, chr(ram[dp])
else : nil
inc(ip)
echo()
if paramcount() == 1: main(paramstr(1))
else: echo("usage: bfrun PATH")
它编译成功,但是当我向它输入一个输入时:
>
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ .
哪个应该打印字符'A'它返回'N'。有任何想法吗?