Initial very rough draft of javascript implementation of brainmess.
authorMichael Welch <michaelgwelch@gmail.com>
Sun, 18 Mar 2012 15:52:30 +0000 (10:52 -0500)
committerMichael Welch <michaelgwelch@gmail.com>
Sun, 18 Mar 2012 15:52:30 +0000 (10:52 -0500)
This version has no support for input

js/context.js
js/form.html [new file with mode: 0644]
js/main.js [new file with mode: 0644]
js/program.js [new file with mode: 0644]

index ea186b4..0a8c5b1 100644 (file)
@@ -1,19 +1,20 @@
-var Context = function() {
+var Context = function(prog, input, output) {
+    var tape = new Tape();
     return {
-        forward: function(prog, tape, input, output) { tape.forward(); },
-        backward: function(prog, tape, input, output) { tape.backward(); },
-        inc: function(prog, tape, input, output) { tape.inc(); },
-        dec: function(prog, tape, input, output) { tape.dec(); },
-        testAndJumpForward: function(prog, tape, input, output) {
+        forward: function() { tape.forward(); },
+        backward: function() { tape.backward(); },
+        inc: function() { tape.inc(); },
+        dec: function() { tape.dec(); },
+        testAndJumpForward: function() {
             if (tape.get() === 0) prog.jumpForward();
         },
-        testAndJumpBackward: function(prog, tape, input, output) {
+        testAndJumpBackward: function() {
             if (tape.get() !== 0) prog.jumpBackward();
         },
-        input: function(prog, tape, input, output) { 
+        input: function() { 
             tape.set(input());
         },
-        output: function(prog, tape, input, output) {
+        output: function() {
             output(tape.get());
         }
     };
diff --git a/js/form.html b/js/form.html
new file mode 100644 (file)
index 0000000..7714149
--- /dev/null
@@ -0,0 +1,57 @@
+<html>
+<head>
+ <title>JavaScript Example 03</title>
+ <script language="JavaScript">
+
+ function poller() {
+   var result = "";
+
+   return {
+     poll: function() {
+       var input = document.ioForm.inputField.value;
+       if (!input) setTimeout("getInput()",100)
+       else result = input;
+     }
+     
+     
+   }
+ }
+
+ function getInput()
+ {
+    var input = document.ioForm.inputField.value;
+    if (!input) setTimeout("getInput()", 100)
+
+    return input;
+ }
+
+ function writeOutput(char)
+{
+  var preTag = document.getElementById("output");
+  if (preTag.firstChild) preTag.appendChild(String.fromCharCode(char));
+  else preTag.firstChild = String.fromCharCode(char);
+}
+
+ </script>
+ <script src="main.js"></script>
+ <script src="program.js"></script>
+ <script src="tape.js"></script>
+ <script src="context.js"></script>
+</head>
+<body>
+ <form name="ioForm">
+ <label>Program</label>
+ <textarea id="prog" name="prog" cols=40 rows=10 wrap="hard"></textarea>
+ <input type="button" name="runButton" value="Run" onClick="main();"/>
+ <br>
+ <label>Input</label>
+ <input type="text" name="inputField" />
+ <input type="button" name="actionButton" value="OK"
+              onClick="getInput();" />
+ <br>
+ <label>Output</label>
+ </form>
+
+ <pre name="stuf" id="output"></pre>
+</body>
+</html>
diff --git a/js/main.js b/js/main.js
new file mode 100644 (file)
index 0000000..cd1d5fe
--- /dev/null
@@ -0,0 +1,47 @@
+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("");
+    preTag.appendChild(outputText);
+    var output = function(char)  {
+       outputText.appendData(String.fromCharCode(char));
+    }
+
+
+    var context = new Context(p, input, output);
+
+    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;
+       
+
+        }
+    }
+
+}
diff --git a/js/program.js b/js/program.js
new file mode 100644 (file)
index 0000000..e29494d
--- /dev/null
@@ -0,0 +1,34 @@
+var Program = function(prog) {
+    var pc = 0;
+
+    return {
+        endOfProgram: function() {
+            return pc >= prog.length;
+        },
+        fetch: function() {
+            return prog[pc++]
+        },
+        jumpForward: function() {
+            pc = prog.findMatch(pc-1) + 1;
+        },
+        jumpBackward: function() {
+            pc = prog.findMatch(pc-1);
+        }
+    }
+};
+
+String.prototype.findMatch = function(index) {
+    var increment;
+    if (this[index] === "[") increment = 1;
+    else if (this[index]  === "]") increment = -1;
+    index += increment;
+    var nestLevel = 1;
+    while(nestLevel > 0) {
+        var instruction = this[index];
+        if (instruction === "[") nestLevel += increment;
+        else if (instruction === "]") nestLevel -= increment;
+        index += increment;
+    }
+    return index - increment;
+}
+