📝 update scripts

This commit is contained in:
ValKmjolnir
2023-11-16 23:19:03 +08:00
parent a03739ebb2
commit 28a42346b7
72 changed files with 1285 additions and 1237 deletions
+20 -20
View File
@@ -8,8 +8,8 @@ var table=[
];
var operand={
new:func(symbol,changed_symbol,move,next_state){
if(move!='L' and move!='R' and move!='S')
new:func(symbol,changed_symbol,move,next_state) {
if (move!='L' and move!='R' and move!='S')
die("invalid move type:"+move);
return {
symbol:symbol,
@@ -22,27 +22,27 @@ var operand={
var machine={
states:{},
add:func(state,operand){
if(!contains(me.states,state))
add:func(state,operand) {
if (!contains(me.states,state))
me.states[state]=[operand];
else{
else {
foreach(var i;me.states[state])
if(i.symbol==operand.symbol or i.symbol==nil){
if (i.symbol==operand.symbol or i.symbol==nil) {
println(i);
die("conflict operand");
}
append(me.states[state],operand);
}
},
load:func(data){
foreach(var opr;data){
load:func(data) {
foreach(var opr;data) {
var (nstat,sym,csym,move,nextstat)=opr;
me.add(nstat,operand.new(sym,csym,move,nextstat));
}
}
};
var prt=func(state,pointer,paper,act=nil){
var prt = func(state,pointer,paper,act=nil) {
print(act!=nil?act:'','\n\t');
var s='';
foreach(var i;paper)
@@ -53,43 +53,43 @@ var prt=func(state,pointer,paper,act=nil){
s~=' ';
print(s,'^\n',state," ");
}
var run=func(table,start,stop){
var run = func(table,start,stop) {
var paper=['0','1','1','1','0','1','0','a'];
var pointer=0;
machine.load(table);
print("states: ",keys(machine.states),'\n');
if(!contains(machine.states,start))
if (!contains(machine.states,start))
die(start~" is not a valid node");
if(!contains(machine.states,stop))
if (!contains(machine.states,stop))
die(stop~" is not a valid node");
var (state,pointer)=(start,0);
prt(state,pointer,paper);
while(state!=stop){
if(!contains(machine.states,state))
while(state!=stop) {
if (!contains(machine.states,state))
die("no matching function for state:"~state);
var found=0;
foreach(var action;machine.states[state]){
foreach(var action;machine.states[state]) {
var (sym,csym,move,next)=(
action.symbol,
action.changed_symbol,
action.move,
action.next_state
);
if(sym==paper[pointer] or sym==nil){
if(sym!=nil)
if (sym==paper[pointer] or sym==nil) {
if (sym!=nil)
paper[pointer]=csym;
if(move=='L') pointer-=1;
elsif(move=='R') pointer+=1;
if (move=='L') pointer-=1;
elsif (move=='R') pointer+=1;
(state,found)=(next,1);
break;
}
}
if(!found)
if (!found)
die("no matching function for state:"~state);
prt(state,pointer,paper,[sym,csym,move,next]);