Permalink
Browse files

Added support for comma using an input box.

  • Loading branch information...
1 parent eb76b11 commit 5f94c839ce823a1766d0fc6033c8aee001abe4cb @dwillie dwillie committed Aug 10, 2013
Showing with 46 additions and 4 deletions.
  1. +18 −0 css/screen.css
  2. +5 −1 index.html
  3. +4 −2 js/interpreter.js
  4. +3 −0 js/models.js
  5. +16 −1 js/views.js
View
@@ -94,6 +94,24 @@ section.machine {
word-wrap: normal;
}
+ section.editor #input-box {
+ float: right;
+ margin-right: 20px;
+ color: gray;
+ display: none;
+ }
+
+ section.editor #input {
+ margin-left:5px;
+ padding: 10px;
+ background-color: #eeeeee;
+ border: none;
+ width: 20px;
+ font-size: 16px;
+ text-align: center;
+ outline:none;
+ }
+
section.editor #source span.caret {
border-radius: 4px;
background-color: #fff;
View
@@ -28,6 +28,10 @@
<a class="button hidden" href="#" id="stop">Stop</a>
<a class="button hidden" href="#" id="pause">Pause</a>
<a class="button hidden" href="#" id="continue">Continue</a>
+ <span id="input-box">
+ <label>Waiting for input</label>
+ <input type="text" id="input"/>
+ </span>
</div>
<div contenteditable="true" id="source">+++++ +++++ initialize counter (cell #0) to 10
[ use loop to set 70/100/30/10
@@ -118,4 +122,4 @@
src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"
alt="Fork me on GitHub"></a>
</body>
-</html>
+</html>
View
@@ -1,10 +1,12 @@
-var Interpreter = function (source, tape, pointer, out, instruction) {
+var Interpreter = function (source, tape, pointer,
+ out, awaitInput, instruction) {
/*
* Brainfuck Interpreter Class
* @source: Brainfuck script
* @tape: Tape model
* @pointer: Pointer model
* @out: Output callback
+ * @awaitInput: Input callback
*
* Usage:
*
@@ -47,7 +49,7 @@ var Interpreter = function (source, tape, pointer, out, instruction) {
break;
case ",":
- // not implemented
+ awaitInput(cell);
break;
case ".":
View
@@ -12,6 +12,9 @@ var Cell = Backbone.Model.extend({
dec: function () {
this.increase("value", -1);
},
+ put: function (c) {
+ this.set("value", c.charCodeAt(0));
+ },
char: function () {
return String.fromCharCode(this.get("value"))
}
View
@@ -57,9 +57,11 @@ var InterpreterView = Backbone.View.extend({
"click #pause": "pause",
"click #continue": "continue",
"click #stop": "stop",
- "keyup #source": "sourceChange"
+ "keyup #source": "sourceChange",
+ "keyup #input": "recieveInput"
},
render: function () {
+ this.input = this.$el.find("#input");
this.output = this.$el.find("#output");
this.buttons = new ButtonSwitchView({
el: this.el
@@ -72,17 +74,30 @@ var InterpreterView = Backbone.View.extend({
run: function () {
this.reset();
this.output.empty();
+ this.input.val("");
this.interpreter = new Interpreter(
this.editor.text(),
this.tape,
this.pointer,
this.out.bind(this),
+ this.awaitInput.bind(this),
this.instruction.bind(this));
this.continue();
},
out: function (cell) {
this.output.append(cell.char());
},
+ awaitInput: function (cell) {
+ this.input.parent().show();
+ this.pause();
+ this.inputTarget = cell;
+ },
+ recieveInput: function () {
+ this.inputTarget.put(this.input.val());
+ this.input.parent().hide();
+ this.input.val("");
+ this.continue();
+ },
instruction: function(index) {
this.editor.find("span.caret").contents().unwrap();
var src = this.editor.text();

0 comments on commit 5f94c83

Please sign in to comment.