diff --git a/TODO b/TODO index 0c8e71b..6312dc4 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,4 @@ +* timing mechanism; end execution after it is stuck in a loop for a while +* fudge the randomness so as to not introduce as many loops? * ability to run genetic algorithm on auto generated programs * more instructions diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index f338213..b32cfe4 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -9,6 +9,7 @@ Interpreter::Interpreter(QByteArray program) : tape(NULL), stdout_(new QTextStream(stdout)), stderr_(new QTextStream(stderr)), + stdin_(new QTextStream(stdin)), m_program(program) { m_instructions.insert('>', new IncrementHeadInstruction(this)); @@ -35,6 +36,7 @@ Interpreter::~Interpreter() delete stdout_; delete stderr_; + delete stdin_; } void Interpreter::start() @@ -78,3 +80,10 @@ void Interpreter::setCaptureOutput(bool on) this->stdout_ = new QTextStream(stdout); } } + +void Interpreter::setInput(QByteArray input) +{ + m_input = input; + delete stdin_; + stdin_ = new QTextStream(&m_input); +} diff --git a/src/Interpreter.h b/src/Interpreter.h index b1019da..e77ab20 100644 --- a/src/Interpreter.h +++ b/src/Interpreter.h @@ -17,6 +17,7 @@ class Interpreter ~Interpreter(); void setCaptureOutput(bool on); + void setInput(QByteArray input); void start(); @@ -30,12 +31,14 @@ class Interpreter QTextStream * stdout_; QTextStream * stderr_; + QTextStream * stdin_; private: QByteArray m_program; QString m_stdout_str; QString m_stderr_str; + QByteArray m_input; Instruction * m_noop; diff --git a/src/instructions.h b/src/instructions.h index b17194a..7599e0c 100644 --- a/src/instructions.h +++ b/src/instructions.h @@ -4,8 +4,6 @@ #include "Instruction.h" #include "Interpreter.h" -#include - class NoopInstruction : public Instruction { public: @@ -63,7 +61,7 @@ class InputHeadInstruction : public Instruction InputHeadInstruction(Interpreter * interpreter) : Instruction(interpreter) {} void execute() { char byte; - std::cin >> byte; + *(m_interpreter->stdin_) >> byte; m_interpreter->tape->writeToHead(byte); } }; diff --git a/src/main.cpp b/src/main.cpp index 22a08da..0b4ab75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) qDebug() << "evaluating program" << i; Interpreter * interp = new Interpreter(program_set.at(i)); interp->setCaptureOutput(true); - + interp->setInput(QByteArray()); interp->start(); QString output = interp->stdout_->readAll();