From a1b341c64b1eb0284bbd8f50a4401ba7efaf7cde Mon Sep 17 00:00:00 2001 From: Max Reynolds Date: Tue, 30 Aug 2016 18:56:25 +0100 Subject: [PATCH 1/4] Create Interpreter.ino Needs debugging, especially loops. Needs to remove Serial reliance and introduce LCD compatibility. --- Interpreter.ino | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Interpreter.ino diff --git a/Interpreter.ino b/Interpreter.ino new file mode 100644 index 0000000..c86cb8b --- /dev/null +++ b/Interpreter.ino @@ -0,0 +1,89 @@ +/* + * + tape++, codePointer++ + * - tape--, codePointer++ + * > tapePointer++, codePointer++ + * < tapePointer--, codePointer++ + * [ if tape[tapePointer] = 0 { while code[codePointer] != ']' { codePointer++ }} else { codePointer++ } + * ] if tape[tapePointer] = 0 { codePointer++ } else { j = -1; + * . Serial.print(String(tape[tapePointer])), codePointer++ + * , Serial.flush(); while(!Serial.available()); tape[tapePointer] = Serial.read();, codePointer++ + */ + +char code[] = {'+', '>', '+', '<', '+', '+', '.', '>', '>', ',', '.'}; + +byte tape[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +unsigned int codePointer = 0; +unsigned int tapePointer = 0; +int i, j = 0; +boolean complete = false; +char readBuffer[3]; + +void setup() { + Serial.begin(9600); + delay(750); + Serial.println("Started!"); + Serial.print("code length: "); + Serial.print('\t'); + Serial.println(sizeof(code)); + + delay(100); +} + +void loop() { + while(!complete){ + + Serial.print("found "); + Serial.println(String(code[codePointer])); + + switch(code[codePointer]){ + case '+': + tape[tapePointer]++; + codePointer++; + break; + + case '-': + tape[tapePointer]--; + codePointer++; + break; + + case '>': + tapePointer++; + codePointer++; + break; + + case '<': + tapePointer--; + codePointer++; + break; + + case '.': + Serial.print(String(tape[tapePointer])); + codePointer++; + break; + + case ',': + Serial.flush(); + while(!Serial.available()); + tape[tapePointer] = char(Serial.parseInt()); + codePointer++; + break; + + default: + codePointer++; + break; + } + delay(100); + Serial.print("tapePointer, tapeValue: "); + Serial.print(" "); + Serial.print(tapePointer); + Serial.print(" "); + Serial.println(tape[tapePointer]); + + if(codePointer == sizeof(code)){ + complete = true; + Serial.println("Complete!"); + } + } +} From 764824d590b5665d8c5a50d38d7552a1835c9722 Mon Sep 17 00:00:00 2001 From: Max Reynolds Date: Wed, 31 Aug 2016 10:07:03 +0100 Subject: [PATCH 2/4] Update input/output functions Changed the Serial so it will read ASCII to char value (eg 'A' turns into 65) and then print A from 65. --- Interpreter.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Interpreter.ino b/Interpreter.ino index c86cb8b..df05d53 100644 --- a/Interpreter.ino +++ b/Interpreter.ino @@ -59,14 +59,14 @@ void loop() { break; case '.': - Serial.print(String(tape[tapePointer])); + Serial.print(char(tape[tapePointer])); codePointer++; break; case ',': Serial.flush(); while(!Serial.available()); - tape[tapePointer] = char(Serial.parseInt()); + tape[tapePointer] = char(Serial.read()); codePointer++; break; From cb1e14fbb645f297e8c62be6aa0d8fd809e42362 Mon Sep 17 00:00:00 2001 From: Max Reynolds Date: Wed, 31 Aug 2016 13:22:05 +0100 Subject: [PATCH 3/4] Complete Trigger Changed the 'complete' trigger to avoid going through the rest of empty code. Made tape and code longer. --- Interpreter.ino | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Interpreter.ino b/Interpreter.ino index df05d53..f847aea 100644 --- a/Interpreter.ino +++ b/Interpreter.ino @@ -9,10 +9,9 @@ * , Serial.flush(); while(!Serial.available()); tape[tapePointer] = Serial.read();, codePointer++ */ -char code[] = {'+', '>', '+', '<', '+', '+', '.', '>', '>', ',', '.'}; +char code[512] = {'+', '>', '+', '<', '+', '+', '.', '>', '>', ',', '.'}; //This is not a profound and pithy brainfuck statement. -byte tape[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +byte tape[1024] = {}; unsigned int codePointer = 0; unsigned int tapePointer = 0; @@ -71,7 +70,7 @@ void loop() { break; default: - codePointer++; + complete = true; break; } delay(100); @@ -80,10 +79,5 @@ void loop() { Serial.print(tapePointer); Serial.print(" "); Serial.println(tape[tapePointer]); - - if(codePointer == sizeof(code)){ - complete = true; - Serial.println("Complete!"); - } } } From d3722944d987219ee54e767f589829b6fd7753ca Mon Sep 17 00:00:00 2001 From: Max Reynolds Date: Wed, 31 Aug 2016 13:25:47 +0100 Subject: [PATCH 4/4] Editing Comments --- Interpreter.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Interpreter.ino b/Interpreter.ino index f847aea..a3fca96 100644 --- a/Interpreter.ino +++ b/Interpreter.ino @@ -3,10 +3,10 @@ * - tape--, codePointer++ * > tapePointer++, codePointer++ * < tapePointer--, codePointer++ - * [ if tape[tapePointer] = 0 { while code[codePointer] != ']' { codePointer++ }} else { codePointer++ } - * ] if tape[tapePointer] = 0 { codePointer++ } else { j = -1; - * . Serial.print(String(tape[tapePointer])), codePointer++ - * , Serial.flush(); while(!Serial.available()); tape[tapePointer] = Serial.read();, codePointer++ + * [ if tape[tapePointer] = 0, find matching ']' and go past it. else carry on. + * ] if tape[tapePointer] = 0, carry on, else loop back to matching '[' + * . print current number as char + * , input number/char */ char code[512] = {'+', '>', '+', '<', '+', '+', '.', '>', '>', ',', '.'}; //This is not a profound and pithy brainfuck statement.