Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix brainfuck interpreter quirks

  • Loading branch information...
commit 418612d180feadf15fec4800f1e713a54dbe0fa4 1 parent 50bf1e2
@andrewrk authored
Showing with 8 additions and 3 deletions.
  1. +5 −2 src/Interpreter.cpp
  2. +3 −1 src/instructions.h
View
7 src/Interpreter.cpp
@@ -70,7 +70,7 @@ void Interpreter::start()
instruction->execute();
pc++;
m_cycle_count++;
- if (m_max_cycles > 0 && m_cycle_count > m_max_cycles) {
+ if (m_max_cycles > 0 && m_cycle_count == m_max_cycles) {
timed_out_flag = true;
break;
}
@@ -95,7 +95,10 @@ void Interpreter::setInput(QByteArray input)
{
m_input = input;
delete stdin_;
- stdin_ = new QTextStream(&m_input);
+ if (input.isNull())
+ stdin_ = NULL;
+ else
+ stdin_ = new QTextStream(&m_input);
}
void Interpreter::setMaxCycles(qint64 cycles)
View
4 src/instructions.h
@@ -60,6 +60,8 @@ class InputHeadInstruction : public Instruction
public:
InputHeadInstruction(Interpreter * interpreter) : Instruction(interpreter) {}
void execute() {
+ if (m_interpreter->stdin_ == NULL)
+ return;
char byte;
*(m_interpreter->stdin_) >> byte;
m_interpreter->tape->writeToHead(byte);
@@ -71,7 +73,7 @@ class BeginLoopInstruction : public Instruction
BeginLoopInstruction(Interpreter * interpreter) : Instruction(interpreter) {}
void execute() {
if (m_interpreter->tape->readFromHead() == 0) {
- m_interpreter->pc = m_interpreter->matching_bracket.value(m_interpreter->pc);
+ m_interpreter->pc = m_interpreter->matching_bracket.value(m_interpreter->pc, m_interpreter->pc);
}
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.