Skip to content

Commit

Permalink
Interpreter can take stdin correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 22, 2011
1 parent 70a0048 commit f8796b4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions src/Interpreter.cpp
Expand Up @@ -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));
Expand All @@ -35,6 +36,7 @@ Interpreter::~Interpreter()

delete stdout_;
delete stderr_;
delete stdin_;
}

void Interpreter::start()
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions src/Interpreter.h
Expand Up @@ -17,6 +17,7 @@ class Interpreter
~Interpreter();

void setCaptureOutput(bool on);
void setInput(QByteArray input);

void start();

Expand All @@ -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;

Expand Down
4 changes: 1 addition & 3 deletions src/instructions.h
Expand Up @@ -4,8 +4,6 @@
#include "Instruction.h"
#include "Interpreter.h"

#include <iostream>

class NoopInstruction : public Instruction
{
public:
Expand Down Expand Up @@ -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);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -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();
Expand Down

0 comments on commit f8796b4

Please sign in to comment.