705b7e1338853d75b360c2c121e97220db4257e2
[brainmess.git] / js / main.js
1 var Brainmess = function() {
2     var context = undefined;
3
4     var execute = function() {
5         while(!context.endOfProgram()) {
6             var i = context.fetch();
7             if (i === ",") {
8                 context.enableInput();
9                 break;// break out of while
10             }
11             switch(i) {
12                 case ">": 
13                     context.forward();
14                     break;
15                 case "<":
16                     context.backward();
17                     break;
18                 case "+":
19                     context.inc();
20                     break;
21                 case "-":
22                     context.dec();
23                     break;
24                 case ".":
25                     context.output();
26                     break;
27                 case "[":
28                     context.testAndJumpForward();
29                     break;
30                 case "]":
31                     context.testAndJumpBackward();
32                     break;
33         
34
35             }
36         }
37     };
38
39     return {
40         // creates a new context based on paramters
41         // and starts execution of the program
42         run: function(programText, inputNode, outputNode) {
43             var p = new Program(programText);
44             var outputText = document.createTextNode("");
45             if (outputNode.firstChild) {
46                 outputNode.removeChild(outputNode.firstChild);
47             }
48             outputNode.appendChild(outputText);
49             var output = function(char)  {
50                 outputText.appendData(String.fromCharCode(char));
51             };
52             context = new Context(p, inputNode, output);
53             execute();
54         },
55         resume: function(event) {
56             context.disableInput();
57             context.input(event.charCode);
58             execute();
59         },
60     };
61
62 };
63 var brainmess = new Brainmess();
64 function main() {
65     brainmess.run(document.getElementById("prog").value, 
66         document.getElementById("input"),
67         document.getElementById("output"));
68 }