-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.js
179 lines (164 loc) · 4.58 KB
/
exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const ast=require("./ast.js");
var programs;
var listBreakPoint;
var memoryLine;
var oldCurrent;
var stopClicked=false;
createMain("read X, L % for (cons nil (cons nil nil)) do X:= (hd X); L := (cons X L) od; if X=?L then nop else X:=L fi; while X do X:=(tl X) od ; F:=nil % write X, L ");
function createMain(prog){
$('ul:first').children().remove();
programs=ast.stringToPrograms(prog)
listBreakPoint=[];
memoryLine=programs[1].getMemoryLine();
oldCurrent="l"+memoryLine[1];
for(var i=0;i<programs[0].length;i++){
$('ul:first').append('<li id="l'+i+'" state="notCurrent" clicked="false"><p>'+programs[0][i]+'</p></li>');
}
for(var i=0;i<programs[0].length;i++){
document.getElementById("l"+i).onclick = clickReact("l"+i);
}
update();
}
function update(){
document.getElementById(oldCurrent).setAttribute("state","notCurrent");
if(memoryLine[1]<programs[0].length){
document.getElementById("l"+memoryLine[1]).setAttribute("state","current");
oldCurrent="l"+memoryLine[1];
}
$('ul:last').children().remove();
for(var i=0;i<memoryLine[0].length;i++){
$('ul:last').append(memoryLine[0][i]);
}
}
document.getElementById("back").onclick = function(){
if(inJump){
return stop()
}
else{
return back()
}
}
document.getElementById("step").onclick = function(){
if(inJump){
return stop()
}
else{
return step()
}
};
document.getElementById("stop").onclick = stop;
document.getElementById("jump").onclick = function(){
if(inJump){
return stop()
}
else{
return jump()
};
}
document.getElementById("jump back").onclick = function(){
if(inJump){
return stop()
}
else{
return jumpBack()
}
};
document.getElementById("edit").onclick = function(){
if(inJump){
return stop()
}
else{
return edit()
}
};
document.getElementById("commit").onclick = commit;
function back(){
programs[1].back();
memoryLine=programs[1].getMemoryLine();
update();
}
function step(){
programs[1].step();
memoryLine=programs[1].getMemoryLine();
update();
}
function stop(){
stopClicked=true;
}
var inJump=false
function jump(){
inJump=true
stopClicked=false;
numberStep=0;
document.getElementById("jump").setAttribute("pressed","true");
step();
setTimeout(execJump,500)
}
function jumpBack(){
inJump=true
stopClicked=false;
numberStep=0;
document.getElementById("jump back").setAttribute("pressed","true");
back();
setTimeout(execJumpBack,500)
}
function execJump(){
console.log("l"+memoryLine[1],listBreakPoint);
if(!stopClicked && numberStep<10000 && memoryLine[1]<programs[0].length && listBreakPoint.indexOf("l"+memoryLine[1])==-1){
step();
numberStep++;
setTimeout(execJump,500);
}
else{
update();
document.getElementById("jump").setAttribute("pressed","false");
inJump=false
}
}
function execJumpBack(){
console.log("l"+memoryLine[1],listBreakPoint);
if(!stopClicked && numberStep<10000 && memoryLine[1]>0 && listBreakPoint.indexOf("l"+memoryLine[1])==-1){
back();
numberStep++;
setTimeout(execJumpBack,500)
}
else{
update();
document.getElementById("jump back").setAttribute("pressed","false");
inJump=false
}
}
function edit(){
document.getElementById("splitLeftMain").setAttribute("class","notVisible");
document.getElementById("splitLeftText").setAttribute("class","visible");
var prog="";
for(var i=0;i<programs[0].length-1;i++){
prog=prog+" "+programs[0][i]+"\n";
}
prog=prog+" "+programs[0][programs[0].length-1];
document.getElementById("text").value=prog;
}
function commit(){
stop()
createMain(document.getElementById("text").value);
update();
document.getElementById("splitLeftMain").setAttribute("class","visible");
document.getElementById("splitLeftText").setAttribute("class","notVisible");
}
function clickReact(id){
return function(){
var value=document.getElementById(id).getAttribute("clicked");
if(value=="true"){
document.getElementById(id).setAttribute("clicked","false");
var index = listBreakPoint.indexOf(id);
if (index > -1) {
listBreakPoint.splice(index, 1);
}
}
else{
document.getElementById(id).setAttribute("clicked","true");
listBreakPoint=listBreakPoint.concat([id]);
}
}
}
module.exports.stop=stop;