89 | 89 |
};
|
90 | 90 |
|
91 | 91 |
/*
|
|
92 |
* Return an error message string if this Instruction is not well-formed.
|
|
93 |
*/
|
|
94 |
this.check = function() {
|
|
95 |
var srcType = this.srcType;
|
|
96 |
var destType = this.destType;
|
|
97 |
if (this.opcode === 'MOV') {
|
|
98 |
if (srcType !== 'I' && srcType !== 'R' && srcType !== 'M') {
|
|
99 |
return "Illegal source reference";
|
|
100 |
}
|
|
101 |
if (destType !== 'R' && destType !== 'M') {
|
|
102 |
return "Illegal destination reference";
|
|
103 |
}
|
|
104 |
return true;
|
|
105 |
} else if (this.opcode === 'INC') {
|
|
106 |
if (srcType !== 'R' && srcType !== 'M') {
|
|
107 |
return "Illegal source reference";
|
|
108 |
}
|
|
109 |
return true;
|
|
110 |
} else if (this.opcode === 'WAIT') {
|
|
111 |
if (srcType !== 'M') {
|
|
112 |
return "Illegal source reference";
|
|
113 |
}
|
|
114 |
if (destType !== 'I') {
|
|
115 |
return "Illegal destination reference";
|
|
116 |
}
|
|
117 |
return true;
|
|
118 |
} else {
|
|
119 |
return "Illegal opcode";
|
|
120 |
}
|
|
121 |
};
|
|
122 |
|
|
123 |
/*
|
92 | 124 |
* Given a yoob.Tape that represents shared memory, and an array of
|
93 | 125 |
* yoob.Tapes that represent private register contexts, execute this
|
94 | 126 |
* Instruction. May return:
|
|
157 | 189 |
};
|
158 | 190 |
};
|
159 | 191 |
|
|
192 |
/*
|
|
193 |
* Objects of this class can actually represent 2 interleaved programs,
|
|
194 |
* as well as a single program.
|
|
195 |
*/
|
160 | 196 |
var Program = function() {
|
161 | 197 |
this.init = function(cfg) {
|
162 | 198 |
cfg = cfg || {};
|
|
206 | 242 |
}
|
207 | 243 |
|
208 | 244 |
return this;
|
|
245 |
};
|
|
246 |
|
|
247 |
/*
|
|
248 |
* May return:
|
|
249 |
*
|
|
250 |
* true, to indicate that the program checked successfully; or
|
|
251 |
* an error string, to indicate that an instruction was malformed.
|
|
252 |
*/
|
|
253 |
this.check = function() {
|
|
254 |
var code = this.code;
|
|
255 |
|
|
256 |
for (var i = 0; i < code.length; i++) {
|
|
257 |
var result = code[i].check();
|
|
258 |
if (typeof result === "string") {
|
|
259 |
return result;
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
return true;
|
209 | 264 |
};
|
210 | 265 |
|
211 | 266 |
/*
|
|
292 | 347 |
this.code = [];
|
293 | 348 |
for (var i = 0; i < lines.length; i++) {
|
294 | 349 |
var str = lines[i];
|
|
350 |
if (progNum === 0 && str.trim().length === 0) {
|
|
351 |
continue;
|
|
352 |
}
|
295 | 353 |
s.reset(str);
|
296 | 354 |
if (s.onType('opcode') && s.token === 'PROG') {
|
297 | 355 |
s.scan();
|
|
405 | 463 |
];
|
406 | 464 |
regs[0].style = this.progStyles[0];
|
407 | 465 |
var prog1 = this.parse(prog1text).setRegistersIndex(0);
|
|
466 |
var result = prog1.check();
|
|
467 |
if (typeof result === 'string') {
|
|
468 |
this.updateStatus('<span style="color: yellow; background: red;">ERROR</span> ' + result);
|
|
469 |
return;
|
|
470 |
}
|
408 | 471 |
|
409 | 472 |
regs[1].style = this.progStyles[1];
|
410 | 473 |
var prog2 = this.parse(prog2text).setRegistersIndex(1);
|
|
474 |
var result = prog2.check();
|
|
475 |
if (typeof result === 'string') {
|
|
476 |
this.updateStatus('<span style="color: yellow; background: red;">ERROR</span> ' + result);
|
|
477 |
return;
|
|
478 |
}
|
411 | 479 |
|
412 | 480 |
var $this = this;
|
413 | 481 |
this.worker = new Worker(this.workerURL);
|