在我之前的代码高尔夫条目的基础上,这里有一个(稍微概括为 IO)OISC仿真器
Fortran77
模糊且没有装载脚手架(655 个字符):
subroutine o(n,m)
integer m(n)
l=1;
do while (l.ne.0)
i=m(l)
j=m(l+1)
k=m(l+2)
mi=mg(n,m,i)
mj=mg(n,m,j)
if(j.eq.(n+2)) then
write(6,*)mj-mi
else
m(l+1)=mj-mi
endif
if (m(l+1).lt.0) then
l=mg(n,m,k)
else
l=l+3
endif
end do
return
end
function mg(n,m,i)
integer m(n)
if (i.eq.n+2) then
read(5,*)mg
elseif (i.eq.n+1) then
mg=0
else
mg=m(i)
endif
return
end
带有注释、加载脚手架等(2435 个字符):
program c
parameter (n=1024) ! The size to use for memeory
integer m(n) ! represent the memory
c Load a program into memory
i=1
1 read(5,*,end=2)l
c write (6,*) "Load ",l," into location ",i
m(i)=l
i=i+1
goto 1
c Run the computer
2 call o(n,m)
stop
end
subroutine o(n,m)
c Simulate a simple computer that supports only a single
c instruction. Memory is a fixed size array of integers.
c
c The supported instruction is subtract-branch-negative which we
c give the assembly syntax
c
c sbn i j k
c
c and acts by subtracting the value in memeory location i from that
c in location j and storing the result in j. If the result is
c negative, the PC is set to k. Because there is only one opcode, it
c is not represented, so a program consists simply of a series of
c triplet (i,j,k), and the PC is generally incremented by 3. The
c program counter starts at 1
c
c Povisions for IO and halting are by way of special memory
c location:
c
c * PC=0 means halt
c * writes (opcode argument j) to memory location n+1 send
c output, reads from n+1 evaluate as 0
c * reads (opcode argument i, j, k) from memory location n+2 fetch
c input, writes to n+2 are forbidden
c * All others reads and writes outside of memeory are forbidden
c n ! the size of the computers memory
integer m(n) ! an array representing the computers memory
l=1; ! program counter
do while (l.ne.0)
i=m(l)
j=m(l+1)
k=m(l+2)
c write (6,*) "Executing from PC=",l,": (",i,", ",j,", ", k,")"
c handle the write special case for output
mi=mg(n,m,i)
mj=mg(n,m,j)
if(j.eq.(n+1)) then
write(6,*)mj-mi
else
c write (6,*) "Setting location ",j," to ",mj-mi
m(l+1)=mj-mi
endif
if (m(l+1).lt.0) then
l=mg(n,m,k)
else
l=l+3
endif
end do
return
end
c Handle the various read special cases
function mg(n,m,i)
integer m(n)
if (i.eq.n+2) then
read(5,*)mg
elseif (i.eq.n+1) then
mg=0
else
mg=m(i)
endif
c write (6,*) "Read ",mg," from location ",i
return
end
示例程序:
13
1025
0
14
1025
0
14
15
0
0
0
0
-1
1
0
导致输出:
$ cat trivial.oisc | ./a.out
1
-1
这是预期的。
它确实是非常简单的代码。这里的重点不是我可以编写多紧密的代码,而是图灵完整性所需的语言有多简单。