RosettaCodeData/Task/Universal-Turing-machine/Amazing-Hopper/universal-turing-machine.ho...

90 lines
1.9 KiB
Plaintext

#include <hopper.h>
#proto UniversalTuringMachine(_X_)
main:
.ctrlc
stbegin=0,stEnd=0,state=0,ptr=0
tape=0,states=0,rules=0,long=0,tapeSize=0
file="turing/prg03.tm"
// load program, rules & states:
jsub(load Archive)
// RUN Universal Turing Machine program:
i=1
__TURING_RUN__:
_Universal Turing Machine ([i,1:end]get(rules))
++i,{long,i}gt? do{ i=1 }
jt(__TURING_RUN__)
println
exit(0)
.locals
printTape:
#hl{
print(tape[1:(ptr-1)],"\R",tape[ptr],"\OFF",tape[(ptr+1):end],"\n")
//sleep(0.1)
}
up(1)
clear mark
back
Universal Turing Machine(rules)
cont=1
clear mark
#hl{
if( rules[1] == state )
if( tape[ptr] == rules[2] )
tape[ptr] = rules[3]
ptr += rules[4]
if(ptr==0)
}
++tapeSize
{0,1,tape}, array(INSERT), ++ptr
#hl{
else if(ptr>tapeSize)
}
++tapeSize
{tapeSize,tape},array(RESIZE),
[tapeSize]{0},put(tape),clear mark
#hl{
endif
state = rules[5]
if(state == stEnd)
cont=0
endif
}
jsub(print Tape)
#hl{
endif
endif
}, {cont}
back
load Archive:
{","}tok sep
{file} stats file
[1,1:end],{file},!(5),load, mov(tape)
[2,1:3], !(5),load, mov(states)
[3:end,1:5], load, mov(rules)
clear mark
[1:end,4]get(rules),colMoving=0, mov(colMoving)
{"1","RIGHT",colMoving} transform, mov(colMoving)
{"-1","LEFT",colMoving} transform, mov(colMoving)
{"0","STAY",colMoving} transform, xtonum, put(rules)
clear mark
{0}reshape(tape)
size(tape),lengthTape=0,mov(lengthTape),[2]get(lengthTape),mov(tapeSize)
#hl{
stbegin=states[1,1]
stEnd=states[1,2]
ptr=states[1,3]
state=stbegin
}
data rules=0, size(rules), mov(datarules), [2]get(data rules), mov(long)
{""}tok sep
back