Add debug

This commit is contained in:
Nathanaël Restori 2014-08-13 17:58:49 +02:00
parent ec66620ad8
commit e8ff5e0203
3 changed files with 379 additions and 250 deletions

View File

@ -16,6 +16,8 @@ QBrainfuck::~QBrainfuck()
void QBrainfuck::on_encodeButton_clicked() void QBrainfuck::on_encodeButton_clicked()
{ {
QByteArray text = ui->textTextEdit->document()->toPlainText().toLatin1(); QByteArray text = ui->textTextEdit->document()->toPlainText().toLatin1();
ui->programTextEdit->setPlainText("");
ui->debugTextEdit->setHtml("");
// First pass : find decades/steps // First pass : find decades/steps
QList<char> decades; // Store the decades for initial loop QList<char> decades; // Store the decades for initial loop
@ -24,6 +26,9 @@ void QBrainfuck::on_encodeButton_clicked()
array.append(0); array.append(0);
QList<char> steps; // Store the steps to construct the program QList<char> steps; // Store the steps to construct the program
QString debugNumbers;
QString debugChars;
QByteArray::const_iterator textIterator; QByteArray::const_iterator textIterator;
for (textIterator = text.constBegin(); textIterator != text.constEnd(); ++textIterator) { for (textIterator = text.constBegin(); textIterator != text.constEnd(); ++textIterator) {
int max_diff = ui->maxSignsSpinBox->value()+1; 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()); decades.append(qRound(*textIterator/(float)ui->decadesSpinBox->value())*ui->decadesSpinBox->value());
array.append(*textIterator); array.append(*textIterator);
} }
// Add current char to debug (with corresponding ASCII value)
debugNumbers += formatNumber(*textIterator);
debugChars += "&nbsp;&nbsp;&nbsp;" + QString(*textIterator);
} }
qSort(decades); qSort(decades);
// Second pass : write program // Second pass : write program
QString program = ""; QString program = "";
ui->debugTextEdit->insertHtml("# The message<br /><br />" + QString(text) + "<br /><br /># In ASCII<br /><br />" + debugNumbers + "<br />" + debugChars + "<br /><br /># The memory<br /><br />");
for(int i = 0; i < ui->decadesSpinBox->value(); ++i) { for(int i = 0; i < ui->decadesSpinBox->value(); ++i) {
program += "+"; program += "+";
} }
@ -71,6 +83,20 @@ void QBrainfuck::on_encodeButton_clicked()
} }
program += "-]"; program += "-]";
// Print initial loop to debug
QString debugText;
for(int multiplier = 0; multiplier <= ui->decadesSpinBox->value(); ++multiplier) {
debugText += "&nbsp;&nbsp;<b>" + formatNumber(ui->decadesSpinBox->value()-multiplier) + "</b>";
for(int i = 1; i < decades.length(); ++i) {
debugText += formatNumber(decades[i]/ui->decadesSpinBox->value()*multiplier);
}
debugText += "<br />";
}
debugText += "<br />";
ui->debugTextEdit->insertHtml(debugText);
int prev_ptr = 0; int prev_ptr = 0;
QList<char>::const_iterator stepsIterator; QList<char>::const_iterator stepsIterator;
@ -78,7 +104,6 @@ void QBrainfuck::on_encodeButton_clicked()
for (stepsIterator = steps.constBegin(); stepsIterator != steps.constEnd(); ++stepsIterator) { for (stepsIterator = steps.constBegin(); stepsIterator != steps.constEnd(); ++stepsIterator) {
int next_ptr = decades.indexOf(*stepsIterator); int next_ptr = decades.indexOf(*stepsIterator);
if(next_ptr > prev_ptr) { if(next_ptr > prev_ptr) {
for(int i = 0; i < next_ptr-prev_ptr; ++i) { for(int i = 0; i < next_ptr-prev_ptr; ++i) {
program += ">"; program += ">";
@ -102,17 +127,25 @@ void QBrainfuck::on_encodeButton_clicked()
program += "."; program += ".";
prev_ptr = next_ptr; prev_ptr = next_ptr;
decades[next_ptr] = *textIterator; decades[next_ptr] = *textIterator;
// Print current memory and current letter
printMemory(decades, decades.length(), next_ptr, "&nbsp;" + QString(*textIterator));
++textIterator; ++textIterator;
} }
ui->programTextEdit->setPlainText(program); ui->programTextEdit->setPlainText(program);
ui->debugTextEdit->insertHtml("<br /><br /># The program<br /><br />" + program);
} }
void QBrainfuck::on_decodeButton_clicked() void QBrainfuck::on_decodeButton_clicked()
{ {
ui->textTextEdit->setPlainText(""); ui->textTextEdit->setPlainText("");
ui->debugTextEdit->setHtml("");
bool exit = false;
int ptr = 0; int ptr = 0;
int maxPtr = 0;
QByteArray text = ui->programTextEdit->document()->toPlainText().toLatin1(); QByteArray text = ui->programTextEdit->document()->toPlainText().toLatin1();
QByteArray array = QByteArray(ui->arraySpinBox->value(), (char)0); QByteArray array = QByteArray(ui->arraySpinBox->value(), (char)0);
QByteArray input = ui->inputLineEdit->text().toLatin1(); QByteArray input = ui->inputLineEdit->text().toLatin1();
@ -121,21 +154,23 @@ void QBrainfuck::on_decodeButton_clicked()
QByteArray::const_iterator textIterator; QByteArray::const_iterator textIterator;
int loopCounter = 0; int loopCounter = 0;
for (textIterator = text.constBegin(); textIterator != text.constEnd(); ++textIterator) { for (textIterator = text.constBegin(); textIterator != text.constEnd() && exit == false; ++textIterator) {
switch (*textIterator) { switch (*textIterator) {
case '>': case '>':
ptr++; ++ptr;
if (ptr >= ui->arraySpinBox->value()) { if (ptr >= ui->arraySpinBox->value()) {
QMessageBox::critical(this, "QBrainfuck", "Error: Pointer went upper than array's length"); ptr = 0;
return; }
if(maxPtr < ptr) {
maxPtr = ptr;
} }
break; break;
case '<': case '<':
ptr--; --ptr;
if (ptr < 0) { if (ptr < 0) {
QMessageBox::critical(this, "QBrainfuck", "Error: Pointer went under 0"); ptr = ui->arraySpinBox->value()-1;
return; maxPtr = ptr;
} }
break; break;
case '+': case '+':
@ -146,24 +181,30 @@ void QBrainfuck::on_decodeButton_clicked()
break; break;
case '.': case '.':
ui->textTextEdit->insertPlainText(QString(array.at(ptr))); ui->textTextEdit->insertPlainText(QString(array.at(ptr)));
printMemory(array, maxPtr, ptr, "OUTPUT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + QString(array.at(ptr)));
break; break;
case ',': case ',':
array[ptr] = *inputIterator; array[ptr] = *inputIterator;
inputIterator++; ++inputIterator;
printMemory(array, maxPtr, ptr, "INPUT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + QString(array.at(ptr)));
break; break;
case '[': case '[':
last_textIterator = textIterator; last_textIterator = textIterator;
loopCounter = 0; loopCounter = 0;
printMemory(array, maxPtr, ptr, "LOOP BEG.&nbsp;&nbsp;&nbsp;&nbsp;");
break; break;
case ']': case ']':
if(array.at(ptr)!=0) { if(array.at(ptr)!=0) {
textIterator = last_textIterator; textIterator = last_textIterator;
loopCounter++; ++loopCounter;
if (loopCounter >= ui->loopSpinBox->value()) { if (loopCounter >= ui->loopSpinBox->value()) {
QMessageBox::critical(this, "QBrainfuck", "Error: Loop too long"); QMessageBox::critical(this, "QBrainfuck", "Error: Loop too long");
return; exit = true;
} }
printMemory(array, maxPtr, ptr, "LOOP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
} else {
printMemory(array, maxPtr, ptr, "LOOP END&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
} }
break; break;
} }
@ -255,6 +296,7 @@ void QBrainfuck::on_saveButton_clicked()
saveFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8() + "\n\n"); saveFile.write(ui->motifTextEdit->document()->toPlainText().toUtf8() + "\n\n");
saveFile.write(ui->resultTextEdit->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()); ui->resultTextEdit->setPlainText(jsonObject["result"].toString());
} }
} }
QString QBrainfuck::formatNumber(int number)
{
if(number < 10) {
return "&nbsp;&nbsp;&nbsp;" + QString().setNum(number);
} else if (number < 100) {
return "&nbsp;&nbsp;" + QString().setNum(number);
} else {
return "&nbsp;" + 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 += "<b>" + formatNumber(array[i]) + "</b>";
} else {
str += formatNumber(array[i]);
}
}
ui->debugTextEdit->insertHtml(text + str + "<br />");
}
void QBrainfuck::printMemory(QList<char> array, int maxPtr, int ptr, QString text)
{
QString str;
for(int i = 0; i < maxPtr; ++i) {
if(i == ptr) {
str += "<b>" + formatNumber(array[i]) + "</b>";
} else {
str += formatNumber(array[i]);
}
}
ui->debugTextEdit->insertHtml(text + str + "<br />");
}

View File

@ -40,6 +40,10 @@ private slots:
private: private:
Ui::QBrainfuck *ui; Ui::QBrainfuck *ui;
QString formatNumber(int number);
void printMemory(QByteArray array, int maxPtr, int ptr, QString text);
void printMemory(QList<char> array, int maxPtr, int ptr, QString text);
}; };
#endif // QBRAINFUCK_H #endif // QBRAINFUCK_H

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1187</width> <width>1194</width>
<height>585</height> <height>634</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -16,244 +16,290 @@
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0"> <item row="0" column="0">
<layout class="QGridLayout" name="gridLayout"> <widget class="QTabWidget" name="tabWidget">
<item row="1" column="1"> <property name="currentIndex">
<widget class="QGroupBox" name="resultGroupBox"> <number>0</number>
<property name="title">
<string>Result</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QPlainTextEdit" name="resultTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveButton">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadButton">
<property name="text">
<string>Load</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="programGroupBox">
<property name="title">
<string>Brainfuck program</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPlainTextEdit" name="programTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="programLabel">
<property name="text">
<string>Program length: 0</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="decodeButton">
<property name="text">
<string>Decode</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="motifGoupBox">
<property name="title">
<string>Motif</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QPlainTextEdit" name="motifTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="motifLabel">
<property name="text">
<string>Motif length: 0</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="motifButton">
<property name="text">
<string>Motif</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0">
<widget class="QGroupBox" name="textGroupBox">
<property name="title">
<string>Plain text</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="textTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="textLabel">
<property name="text">
<string>Text length: 0</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="encodeButton">
<property name="text">
<string>Encode</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="optionsGroupBox">
<property name="title">
<string>Options</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <widget class="QWidget" name="tab">
<item row="0" column="2"> <attribute name="title">
<layout class="QFormLayout" name="formLayout_3"> <string>Main</string>
<property name="fieldGrowthPolicy"> </attribute>
<enum>QFormLayout::ExpandingFieldsGrow</enum> <layout class="QGridLayout" name="gridLayout_4">
</property> <item row="0" column="2">
<item row="0" column="1"> <widget class="QGroupBox" name="programGroupBox">
<widget class="QSpinBox" name="loopSpinBox"> <property name="title">
<property name="maximum"> <string>Brainfuck program</string>
<number>5000</number> </property>
</property> <layout class="QVBoxLayout" name="verticalLayout_2">
<property name="value"> <item>
<number>500</number> <widget class="QPlainTextEdit" name="programTextEdit">
</property> <property name="font">
</widget> <font>
</item> <family>Monospace</family>
<item row="0" column="0"> </font>
<widget class="QLabel" name="loopLabel"> </property>
<property name="text"> <property name="lineWrapMode">
<string>Max loop length</string> <enum>QPlainTextEdit::NoWrap</enum>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item>
<widget class="QLabel" name="arrayLabel"> <widget class="QLabel" name="programLabel">
<property name="text"> <property name="text">
<string>Array size</string> <string>Program length: 0</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item>
<widget class="QLineEdit" name="inputLineEdit"/> <widget class="QPushButton" name="decodeButton">
</item> <property name="text">
<item row="2" column="0"> <string>Decode</string>
<widget class="QLabel" name="inputLabel"> </property>
<property name="text"> </widget>
<string>Input</string> </item>
</property> </layout>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="3" column="0">
<widget class="QSpinBox" name="arraySpinBox"> <widget class="QGroupBox" name="motifGoupBox">
<property name="maximum"> <property name="title">
<number>5000</number> <string>Motif</string>
</property> </property>
<property name="value"> <layout class="QVBoxLayout" name="verticalLayout_3">
<number>100</number> <item>
</property> <widget class="QPlainTextEdit" name="motifTextEdit">
</widget> <property name="font">
</item> <font>
</layout> <family>Monospace</family>
</item> </font>
<item row="0" column="1"> </property>
<layout class="QFormLayout" name="formLayout_2"> <property name="lineWrapMode">
<property name="fieldGrowthPolicy"> <enum>QPlainTextEdit::NoWrap</enum>
<enum>QFormLayout::ExpandingFieldsGrow</enum> </property>
</property> </widget>
<item row="0" column="0"> </item>
<widget class="QLabel" name="maxSignsLabel"> <item>
<property name="text"> <widget class="QLabel" name="motifLabel">
<string>Max signs (+/-)</string> <property name="text">
</property> <string>Motif length: 0</string>
</widget> </property>
</item> </widget>
<item row="0" column="1"> </item>
<widget class="QSpinBox" name="maxSignsSpinBox"> <item>
<property name="value"> <widget class="QPushButton" name="motifButton">
<number>10</number> <property name="text">
</property> <string>Motif</string>
</widget> </property>
</item> </widget>
<item row="1" column="0"> </item>
<widget class="QLabel" name="decadesLabel"> </layout>
<property name="text"> </widget>
<string>Decades size</string> </item>
</property> <item row="3" column="2">
</widget> <widget class="QGroupBox" name="resultGroupBox">
</item> <property name="title">
<item row="2" column="0"> <string>Result</string>
<widget class="QLabel" name="regexLabel"> </property>
<property name="text"> <layout class="QVBoxLayout" name="verticalLayout_4">
<string>Motif regex</string> <item>
</property> <widget class="QPlainTextEdit" name="resultTextEdit">
</widget> <property name="font">
</item> <font>
<item row="2" column="1"> <family>Monospace</family>
<widget class="QLineEdit" name="regexLineEdit"> </font>
<property name="text"> </property>
<string>#</string> <property name="lineWrapMode">
</property> <enum>QPlainTextEdit::NoWrap</enum>
</widget> </property>
</item> </widget>
<item row="1" column="1"> </item>
<widget class="QSpinBox" name="decadesSpinBox"> <item>
<property name="value"> <widget class="QPushButton" name="saveButton">
<number>10</number> <property name="text">
</property> <string>Save</string>
</widget> </property>
</item> </widget>
</layout> </item>
</item> <item>
</layout> <widget class="QPushButton" name="loadButton">
<property name="text">
<string>Load</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0" rowspan="3" colspan="2">
<widget class="QGroupBox" name="textGroupBox">
<property name="title">
<string>Plain text</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="textTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="textLabel">
<property name="text">
<string>Text length: 0</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="encodeButton">
<property name="text">
<string>Encode</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="4" column="0" colspan="3">
<widget class="QGroupBox" name="optionsGroupBox">
<property name="title">
<string>Options</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="2">
<layout class="QFormLayout" name="formLayout_3">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="loopLabel">
<property name="text">
<string>Max loop length</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="loopSpinBox">
<property name="maximum">
<number>100000</number>
</property>
<property name="value">
<number>500</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="arrayLabel">
<property name="text">
<string>Array size</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="arraySpinBox">
<property name="maximum">
<number>100000</number>
</property>
<property name="value">
<number>100</number>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="inputLabel">
<property name="text">
<string>Input</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="inputLineEdit"/>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="maxSignsLabel">
<property name="text">
<string>Max signs (+/-)</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="maxSignsSpinBox">
<property name="value">
<number>10</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="decadesLabel">
<property name="text">
<string>Decades size</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="regexLabel">
<property name="text">
<string>Motif regex</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="regexLineEdit">
<property name="text">
<string>#</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="decadesSpinBox">
<property name="value">
<number>10</number>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
<zorder>textGroupBox</zorder>
<zorder>programGroupBox</zorder>
<zorder>motifGoupBox</zorder>
<zorder>resultGroupBox</zorder>
<zorder>optionsGroupBox</zorder>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Debug</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QTextEdit" name="debugTextEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
<property name="lineWrapMode">
<enum>QTextEdit::NoWrap</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget> </widget>
</item> </item>
</layout> </layout>