Add support for input.
authorMichael Welch <michaelgwelch@gmail.com>
Sun, 18 Mar 2012 17:19:13 +0000 (12:19 -0500)
committerMichael Welch <michaelgwelch@gmail.com>
Sun, 18 Mar 2012 17:19:13 +0000 (12:19 -0500)
Added a input of type text which can accept one character at a time. It
is disabled by default. Only when an input instruction is being executed
is it enabled. Immediately after the keyUp event the input is disabled.

When an input instruction is being executed the fetch/execute cycle is
terminated and the program does nothing until the user types a character.
The fetch/execute cycle is then resumed where it left off.

js/context.js
js/form.html
js/main.js

index 0a8c5b1..b325e5d 100644 (file)
@@ -1,6 +1,15 @@
-var Context = function(prog, input, output) {
+var Context = function(prog, inputNode, output) {
     var tape = new Tape();
     return {
+        enableInput: function() { 
+            inputNode.disabled=false; 
+            inputNode.value="";
+        },
+        disableInput: function() {
+            inputNode.disabled = true;
+        },
+        endOfProgram: function() { return prog.endOfProgram(); },
+        fetch: function() { return prog.fetch(); },
         forward: function() { tape.forward(); },
         backward: function() { tape.backward(); },
         inc: function() { tape.inc(); },
@@ -11,8 +20,8 @@ var Context = function(prog, input, output) {
         testAndJumpBackward: function() {
             if (tape.get() !== 0) prog.jumpBackward();
         },
-        input: function() { 
-            tape.set(input());
+        input: function(charCode) { 
+            tape.set(charCode);
         },
         output: function() {
             output(tape.get());
index 30c9475..d16e7c4 100644 (file)
  <textarea id="prog" name="prog" cols=80 rows=20 wrap="hard"></textarea>
  <input type="button" name="runButton" value="Run" onClick="main();"/>
  <br>
+ <label>Input</label>
+ <input type="text" id="input" disabled="true"
+    onKeyPress="brainmess.resume(event);"></input>
+ <br>
  <label>Output</label>
  </form>
 
+
  <pre name="stuf" id="output"></pre>
 </body>
 </html>
index 385c36d..705b7e1 100644 (file)
@@ -1,47 +1,68 @@
-function main() {
-    var programText = document.getElementById("prog").value;
-    var p = new Program(programText);
-    var input = undefined;
-    var preTag = document.getElementById("output");
-    var outputText = document.createTextNode("");
-    if (preTag.firstChild) preTag.removeChild(preTag.firstChild);
-    preTag.appendChild(outputText);
-    var output = function(char)  {
-       outputText.appendData(String.fromCharCode(char));
-    }
-
-    var context = new Context(p, input, output);
+var Brainmess = function() {
+    var context = undefined;
 
-    while(!p.endOfProgram()) {
-        var i = p.fetch();
-        switch(i) {
-            case ">": 
-                context.forward();
-                break;
-            case "<":
-                context.backward();
-                break;
-            case "+":
-                context.inc();
-                break;
-            case "-":
-                context.dec();
-                break;
-            case ".":
-                context.output();
-                break;
-            case ",":
-                context.input();
-                break;
-            case "[":
-                context.testAndJumpForward();
-                break;
-            case "]":
-                context.testAndJumpBackward();
-                break;
-       
+    var execute = function() {
+        while(!context.endOfProgram()) {
+            var i = context.fetch();
+            if (i === ",") {
+                context.enableInput();
+                break;// break out of while
+            }
+            switch(i) {
+                case ">": 
+                    context.forward();
+                    break;
+                case "<":
+                    context.backward();
+                    break;
+                case "+":
+                    context.inc();
+                    break;
+                case "-":
+                    context.dec();
+                    break;
+                case ".":
+                    context.output();
+                    break;
+                case "[":
+                    context.testAndJumpForward();
+                    break;
+                case "]":
+                    context.testAndJumpBackward();
+                    break;
+        
 
+            }
         }
-    }
+    };
 
+    return {
+        // creates a new context based on paramters
+        // and starts execution of the program
+        run: function(programText, inputNode, outputNode) {
+            var p = new Program(programText);
+            var outputText = document.createTextNode("");
+            if (outputNode.firstChild) {
+                outputNode.removeChild(outputNode.firstChild);
+            }
+            outputNode.appendChild(outputText);
+            var output = function(char)  {
+                outputText.appendData(String.fromCharCode(char));
+            };
+            context = new Context(p, inputNode, output);
+            execute();
+        },
+        resume: function(event) {
+            context.disableInput();
+            context.input(event.charCode);
+            execute();
+        },
+    };
+
+};
+var brainmess = new Brainmess();
+function main() {
+    brainmess.run(document.getElementById("prog").value, 
+        document.getElementById("input"),
+        document.getElementById("output"));
 }