From e8ff5e0203678ea4efd76acbe90749b33a60591d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathana=C3=ABl=20Restori?= Date: Wed, 13 Aug 2014 17:58:49 +0200 Subject: [PATCH] Add debug --- qbrainfuck.cpp | 101 ++++++++-- qbrainfuck.h | 4 + qbrainfuck.ui | 524 +++++++++++++++++++++++++++---------------------- 3 files changed, 379 insertions(+), 250 deletions(-) diff --git a/qbrainfuck.cpp b/qbrainfuck.cpp index ec66220..517d051 100644 --- a/qbrainfuck.cpp +++ b/qbrainfuck.cpp @@ -16,6 +16,8 @@ QBrainfuck::~QBrainfuck() void QBrainfuck::on_encodeButton_clicked() { QByteArray text = ui->textTextEdit->document()->toPlainText().toLatin1(); + ui->programTextEdit->setPlainText(""); + ui->debugTextEdit->setHtml(""); // First pass : find decades/steps QList decades; // Store the decades for initial loop @@ -24,6 +26,9 @@ void QBrainfuck::on_encodeButton_clicked() array.append(0); QList steps; // Store the steps to construct the program + QString debugNumbers; + QString debugChars; + QByteArray::const_iterator textIterator; for (textIterator = text.constBegin(); textIterator != text.constEnd(); ++textIterator) { int max_diff = ui->maxSignsSpinBox->value()+1; @@ -47,12 +52,19 @@ void QBrainfuck::on_encodeButton_clicked() decades.append(qRound(*textIterator/(float)ui->decadesSpinBox->value())*ui->decadesSpinBox->value()); array.append(*textIterator); } + + // Add current char to debug (with corresponding ASCII value) + debugNumbers += formatNumber(*textIterator); + debugChars += "   " + QString(*textIterator); } + qSort(decades); // Second pass : write program QString program = ""; + ui->debugTextEdit->insertHtml("# The message

" + QString(text) + "

# In ASCII

" + debugNumbers + "
" + debugChars + "

# The memory

"); + for(int i = 0; i < ui->decadesSpinBox->value(); ++i) { program += "+"; } @@ -71,6 +83,20 @@ void QBrainfuck::on_encodeButton_clicked() } program += "-]"; + // Print initial loop to debug + QString debugText; + for(int multiplier = 0; multiplier <= ui->decadesSpinBox->value(); ++multiplier) { + debugText += "  " + formatNumber(ui->decadesSpinBox->value()-multiplier) + ""; + + for(int i = 1; i < decades.length(); ++i) { + debugText += formatNumber(decades[i]/ui->decadesSpinBox->value()*multiplier); + } + + debugText += "
"; + } + debugText += "
"; + ui->debugTextEdit->insertHtml(debugText); + int prev_ptr = 0; QList::const_iterator stepsIterator; @@ -78,7 +104,6 @@ void QBrainfuck::on_encodeButton_clicked() for (stepsIterator = steps.constBegin(); stepsIterator != steps.constEnd(); ++stepsIterator) { int next_ptr = decades.indexOf(*stepsIterator); - if(next_ptr > prev_ptr) { for(int i = 0; i < next_ptr-prev_ptr; ++i) { program += ">"; @@ -102,17 +127,25 @@ void QBrainfuck::on_encodeButton_clicked() program += "."; prev_ptr = next_ptr; decades[next_ptr] = *textIterator; + + // Print current memory and current letter + printMemory(decades, decades.length(), next_ptr, " " + QString(*textIterator)); + ++textIterator; } ui->programTextEdit->setPlainText(program); + ui->debugTextEdit->insertHtml("

# The program

" + program); } void QBrainfuck::on_decodeButton_clicked() { ui->textTextEdit->setPlainText(""); + ui->debugTextEdit->setHtml(""); + bool exit = false; int ptr = 0; + int maxPtr = 0; QByteArray text = ui->programTextEdit->document()->toPlainText().toLatin1(); QByteArray array = QByteArray(ui->arraySpinBox->value(), (char)0); QByteArray input = ui->inputLineEdit->text().toLatin1(); @@ -121,21 +154,23 @@ void QBrainfuck::on_decodeButton_clicked() QByteArray::const_iterator textIterator; int loopCounter = 0; - for (textIterator = text.constBegin(); textIterator != text.constEnd(); ++textIterator) { + for (textIterator = text.constBegin(); textIterator != text.constEnd() && exit == false; ++textIterator) { switch (*textIterator) { case '>': - ptr++; + ++ptr; if (ptr >= ui->arraySpinBox->value()) { - QMessageBox::critical(this, "QBrainfuck", "Error: Pointer went upper than array's length"); - return; + ptr = 0; + } + if(maxPtr < ptr) { + maxPtr = ptr; } break; case '<': - ptr--; + --ptr; if (ptr < 0) { - QMessageBox::critical(this, "QBrainfuck", "Error: Pointer went under 0"); - return; + ptr = ui->arraySpinBox->value()-1; + maxPtr = ptr; } break; case '+': @@ -146,24 +181,30 @@ void QBrainfuck::on_decodeButton_clicked() break; case '.': ui->textTextEdit->insertPlainText(QString(array.at(ptr))); + printMemory(array, maxPtr, ptr, "OUTPUT:     " + QString(array.at(ptr))); break; case ',': array[ptr] = *inputIterator; - inputIterator++; + ++inputIterator; + printMemory(array, maxPtr, ptr, "INPUT:      " + QString(array.at(ptr))); break; case '[': last_textIterator = textIterator; loopCounter = 0; + printMemory(array, maxPtr, ptr, "LOOP BEG.    "); break; case ']': if(array.at(ptr)!=0) { textIterator = last_textIterator; - loopCounter++; + ++loopCounter; if (loopCounter >= ui->loopSpinBox->value()) { QMessageBox::critical(this, "QBrainfuck", "Error: Loop too long"); - return; + exit = true; } + printMemory(array, maxPtr, ptr, "LOOP         "); + } else { + printMemory(array, maxPtr, ptr, "LOOP END     "); } break; } @@ -255,6 +296,7 @@ void QBrainfuck::on_saveButton_clicked() saveFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8() + "\n\n"); saveFile.write(ui->resultTextEdit->document()->toPlainText().toUtf8() + "\n\n"); } + } } @@ -284,3 +326,40 @@ void QBrainfuck::on_loadButton_clicked() ui->resultTextEdit->setPlainText(jsonObject["result"].toString()); } } + +QString QBrainfuck::formatNumber(int number) +{ + if(number < 10) { + return "   " + QString().setNum(number); + } else if (number < 100) { + return "  " + QString().setNum(number); + } else { + return " " + QString().setNum(number); + } +} + +void QBrainfuck::printMemory(QByteArray array, int maxPtr, int ptr, QString text) +{ + QString str; + for(int i = 0; i <= maxPtr; ++i) { + if(i == ptr) { + str += "" + formatNumber(array[i]) + ""; + } else { + str += formatNumber(array[i]); + } + } + ui->debugTextEdit->insertHtml(text + str + "
"); +} + +void QBrainfuck::printMemory(QList array, int maxPtr, int ptr, QString text) +{ + QString str; + for(int i = 0; i < maxPtr; ++i) { + if(i == ptr) { + str += "" + formatNumber(array[i]) + ""; + } else { + str += formatNumber(array[i]); + } + } + ui->debugTextEdit->insertHtml(text + str + "
"); +} diff --git a/qbrainfuck.h b/qbrainfuck.h index 9fab705..61ab2f9 100644 --- a/qbrainfuck.h +++ b/qbrainfuck.h @@ -40,6 +40,10 @@ private slots: private: Ui::QBrainfuck *ui; + + QString formatNumber(int number); + void printMemory(QByteArray array, int maxPtr, int ptr, QString text); + void printMemory(QList array, int maxPtr, int ptr, QString text); }; #endif // QBRAINFUCK_H diff --git a/qbrainfuck.ui b/qbrainfuck.ui index 5e806ff..43384aa 100644 --- a/qbrainfuck.ui +++ b/qbrainfuck.ui @@ -6,8 +6,8 @@ 0 0 - 1187 - 585 + 1194 + 634 @@ -16,244 +16,290 @@ - - - - - Result - - - - - - - Monospace - - - - - - - - Save - - - - - - - Load - - - - - - - - - - Brainfuck program - - - - - - - Monospace - - - - - - - - Program length: 0 - - - - - - - Decode - - - - - - - - - - Motif - - - - - - - Monospace - - - - - - - - Motif length: 0 - - - - - - - Motif - - - - - - - - - - Plain text - - - - - - - Monospace - - - - - - - - Text length: 0 - - - - - - - Encode - - - - - - - - - - - - Options + + + 0 - - - - - QFormLayout::ExpandingFieldsGrow - - - - - 5000 - - - 500 - - - - - - - Max loop length - - - - - - - Array size - - - - - - - - - - Input - - - - - - - 5000 - - - 100 - - - - - - - - - QFormLayout::ExpandingFieldsGrow - - - - - Max signs (+/-) - - - - - - - 10 - - - - - - - Decades size - - - - - - - Motif regex - - - - - - - # - - - - - - - 10 - - - - - - + + + Main + + + + + + Brainfuck program + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + Program length: 0 + + + + + + + Decode + + + + + + + + + + Motif + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + Motif length: 0 + + + + + + + Motif + + + + + + + + + + Result + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + Save + + + + + + + Load + + + + + + + + + + Plain text + + + + + + + Monospace + + + + QPlainTextEdit::NoWrap + + + + + + + Text length: 0 + + + + + + + Encode + + + + + + + + + + Options + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Max loop length + + + + + + + 100000 + + + 500 + + + + + + + Array size + + + + + + + 100000 + + + 100 + + + + + + + Input + + + + + + + + + + + + QFormLayout::ExpandingFieldsGrow + + + + + Max signs (+/-) + + + + + + + 10 + + + + + + + Decades size + + + + + + + Motif regex + + + + + + + # + + + + + + + 10 + + + + + + + + + + textGroupBox + programGroupBox + motifGoupBox + resultGroupBox + optionsGroupBox + + + + Debug + + + + + + + Monospace + + + + QTextEdit::NoWrap + + + + +