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()
{
QByteArray text = ui->textTextEdit->document()->toPlainText().toLatin1();
ui->programTextEdit->setPlainText("");
ui->debugTextEdit->setHtml("");
// First pass : find decades/steps
QList<char> decades; // Store the decades for initial loop
@ -24,6 +26,9 @@ void QBrainfuck::on_encodeButton_clicked()
array.append(0);
QList<char> 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 += "&nbsp;&nbsp;&nbsp;" + QString(*textIterator);
}
qSort(decades);
// Second pass : write 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) {
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 += "&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;
QList<char>::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, "&nbsp;" + QString(*textIterator));
++textIterator;
}
ui->programTextEdit->setPlainText(program);
ui->debugTextEdit->insertHtml("<br /><br /># The program<br /><br />" + 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:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + QString(array.at(ptr)));
break;
case ',':
array[ptr] = *inputIterator;
inputIterator++;
++inputIterator;
printMemory(array, maxPtr, ptr, "INPUT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + QString(array.at(ptr)));
break;
case '[':
last_textIterator = textIterator;
loopCounter = 0;
printMemory(array, maxPtr, ptr, "LOOP BEG.&nbsp;&nbsp;&nbsp;&nbsp;");
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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
} else {
printMemory(array, maxPtr, ptr, "LOOP END&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
}
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 "&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:
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

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1187</width>
<height>585</height>
<width>1194</width>
<height>634</height>
</rect>
</property>
<property name="windowTitle">
@ -16,244 +16,290 @@
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QGroupBox" name="resultGroupBox">
<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>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</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="1">
<widget class="QSpinBox" name="loopSpinBox">
<property name="maximum">
<number>5000</number>
</property>
<property name="value">
<number>500</number>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="loopLabel">
<property name="text">
<string>Max loop length</string>
</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="2" column="1">
<widget class="QLineEdit" name="inputLineEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="inputLabel">
<property name="text">
<string>Input</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="arraySpinBox">
<property name="maximum">
<number>5000</number>
</property>
<property name="value">
<number>100</number>
</property>
</widget>
</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 class="QWidget" name="tab">
<attribute name="title">
<string>Main</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="2">
<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>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</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="3" 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>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</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="3" column="2">
<widget class="QGroupBox" name="resultGroupBox">
<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>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</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="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>
</item>
</layout>