Move assembler from CodeEditor to EditTab
MODIFY
src/codeeditor.cpp
src/codeeditor.cpp
@@ -40,42 +40,18 @@ CodeEditor::CodeEditor(QWidget* parent) : QPlainTextEdit(parent) {
setMouseTracking(true);
setWordWrapMode(QTextOption::NoWrap);
+ setupChangedTimer();
}
-void CodeEditor::setupAssembler() {
+void CodeEditor::setupChangedTimer() {
// configures the change-timer and assembler connectivity with Parser
m_changeTimer.setInterval(500);
m_changeTimer.setSingleShot(true);
// A change in the document will start the timer - when the timer elapses, the contents will be assembled if there
// is no syntax error. By doing this, the timer is restartet each time a change occurs (ie. a user is continuously
// typing)
- connect(this, &CodeEditor::textChanged, [=] {
- if (m_timerEnabled)
- m_changeTimer.start();
- });
- connect(&m_changeTimer, &QTimer::timeout, this, &CodeEditor::assembleCode);
- m_assembler = new Assembler();
-}
-
-void CodeEditor::assembleCode() {
- if (m_tooltipForLine.isEmpty()) {
- // No tooltips available => syntax is accepted
-
- const QByteArray& ret = m_assembler->assembleBinaryFile(*document());
- if (!m_assembler->hasError()) {
- emit assembledSuccessfully(ret, true, 0x0);
- if (m_assembler->hasData()) {
- emit assembledSuccessfully(m_assembler->getDataSegment(), false, DATASTART);
- }
- } else {
- QMessageBox err;
- err.setText("Error in assembling file.");
- err.exec();
- }
- }
- // Restart the simulator to trigger the data memory to be loaded into the main memory. Bad code that this is done
- // from here, but it works
- Pipeline::getPipeline()->restart();
+ connect(this, &QPlainTextEdit::textChanged, [=] { m_changeTimer.start(); });
+ connect(&m_changeTimer, &QTimer::timeout, this, &CodeEditor::textChanged);
}
int CodeEditor::lineNumberAreaWidth() {

MODIFY
src/codeeditor.h
src/codeeditor.h
@@ -29,18 +29,18 @@ public:
void updateBreakpoints();
int lineNumberAreaWidth();
void setupSyntaxHighlighter();
- void setupAssembler();
+ void setupChangedTimer();
void enableBreakpointArea();
- void reset() { m_highlighter->reset(); }
- void setTimerEnabled(bool state) { m_timerEnabled = state; }
- const QByteArray& getCurrentOutputArray() { return m_assembler->getTextSegment(); }
- void clearOutputArray() {
- m_assembler->clear();
+ void reset() {
+ m_highlighter->reset();
m_tooltipForLine.clear();
+ clear();
}
+ bool syntaxAccepted() const { return m_tooltipForLine.isEmpty(); }
+
signals:
void readyToTranslate(); // Emitted when syntax has been accepted, and the input text can be translated to
- void assembledSuccessfully(const QByteArray& bytearray, bool clearSimulatorMemory, uint32_t baseAddress);
+ void textChanged();
protected:
void resizeEvent(QResizeEvent* event) override;
@@ -51,10 +51,8 @@ private slots:
void highlightCurrentLine();
void updateSidebar(const QRect&, int);
void updateTooltip(int line, QString tip);
- void assembleCode();
private:
- Assembler* m_assembler;
SyntaxHighlighter* m_highlighter;
LineNumberArea* m_lineNumberArea;
BreakpointArea* m_breakpointArea;
@@ -71,7 +69,6 @@ private:
// occur on a regular mouse scroll
QTimer m_fontTimer;
QTimer m_changeTimer;
- bool m_timerEnabled = true;
bool eventFilter(QObject* observed, QEvent* event) override;
};

MODIFY
src/edittab.cpp
src/edittab.cpp
#include "edittab.h"
#include "ui_edittab.h"
+#include <QMessageBox>
+
#include "parser.h"
#include "pipeline.h"
@@ -10,7 +12,7 @@ EditTab::EditTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, parent)
// Only add syntax highlighter for code edit view - not for translated code. This is assumed to be correct after a
// translation is complete
m_ui->assemblyedit->setupSyntaxHighlighter();
- m_ui->assemblyedit->setupAssembler();
+ m_ui->assemblyedit->setupChangedTimer();
m_ui->binaryedit->setReadOnly(true);
// enable breakpoint area for the translated code only
m_ui->binaryedit->enableBreakpointArea();
@@ -21,8 +23,9 @@ EditTab::EditTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, parent)
connect(m_ui->binaryedit->verticalScrollBar(), &QScrollBar::valueChanged, m_ui->assemblyedit->verticalScrollBar(),
&QScrollBar::setValue);
- // Connect data parsing signals from the assembler to this
- connect(m_ui->assemblyedit, &CodeEditor::assembledSuccessfully, this, &EditTab::assemblingComplete);
+ m_assembler = new Assembler();
+
+ connect(m_ui->assemblyedit, &CodeEditor::textChanged, this, &EditTab::assemble);
}
QString EditTab::getAssemblyText() {
@@ -30,19 +33,37 @@ QString EditTab::getAssemblyText() {
}
const QByteArray& EditTab::getBinaryData() {
- return m_ui->assemblyedit->getCurrentOutputArray();
+ return m_assembler->getTextSegment();
}
-void EditTab::clearOutputArray() {
- m_ui->assemblyedit->clearOutputArray();
+void EditTab::clear() {
+ m_ui->assemblyedit->reset();
+ m_assembler->clear();
}
-EditTab::~EditTab() {
- delete m_ui;
+void EditTab::assemble() {
+ if (m_ui->assemblyedit->syntaxAccepted()) {
+ // No tooltips available => syntax is accepted
+
+ const QByteArray& ret = m_assembler->assembleBinaryFile(*m_ui->assemblyedit->document());
+ if (!m_assembler->hasError()) {
+ assemblingComplete(ret, true, 0x0);
+ if (m_assembler->hasData()) {
+ assemblingComplete(m_assembler->getDataSegment(), false, DATASTART);
+ }
+ } else {
+ QMessageBox err;
+ err.setText("Error in assembling file.");
+ err.exec();
+ }
+ }
+ // Restart the simulator to trigger the data memory to be loaded into the main memory. Bad code that this is done
+ // from here, but it works
+ Pipeline::getPipeline()->restart();
}
-void EditTab::setTimerEnabled(bool state) {
- m_ui->assemblyedit->setTimerEnabled(state);
+EditTab::~EditTab() {
+ delete m_ui;
}
void EditTab::newProgram() {
@@ -79,13 +100,8 @@ void EditTab::on_assemblyfile_toggled(bool checked) {
// Since we are removing the input text/binary info, we need to reset the pipeline
Pipeline::getPipeline()->reset();
- // handles toggling between assembly input and binary input
- if (checked) {
- m_ui->assemblyedit->setEnabled(true);
- m_ui->assemblyedit->setTimerEnabled(true);
- } else {
- // Disable when loading binary files
- m_ui->assemblyedit->setTimerEnabled(false);
+ if (!checked) {
+ // Disable assembly edit when loading binary files
m_ui->assemblyedit->setEnabled(false);
}
// clear both editors when switching input mode and reset the highlighter for the assembly editor
@@ -109,6 +125,6 @@ void EditTab::on_disassembledViewButton_toggled(bool checked) {
// if (m_ui->binaryfile->isChecked()) {
// assemblingComplete(Parser::getParser()->getFileByteArray());
//} else {
- assemblingComplete(m_ui->assemblyedit->getCurrentOutputArray());
+ assemblingComplete(getBinaryData());
// }
}

MODIFY
src/edittab.h
src/edittab.h
#include <QFile>
#include <QWidget>
+#include "assembler.h"
#include "ripestab.h"
namespace Ui {
class EditTab;
}
+class Assembler;
+
class EditTab : public RipesTab {
Q_OBJECT
@@ -18,11 +21,11 @@ public:
void setAssemblyText(const QString& text);
void setDisassemblerText();
void setInputMode(bool isAssembly);
- void setTimerEnabled(bool state);
- void clearOutputArray();
QString getAssemblyText();
- const QByteArray& getBinaryData();
void newProgram();
+ void clear();
+
+ const QByteArray& getBinaryData();
signals:
void loadBinaryFile();
@@ -31,11 +34,13 @@ signals:
// sent to the processor
private slots:
+ void assemble();
void on_assemblyfile_toggled(bool checked);
- void assemblingComplete(const QByteArray& binaryCode, bool clear = true, uint32_t baseAddress = 0x0);
-
void on_disassembledViewButton_toggled(bool checked);
private:
+ void assemblingComplete(const QByteArray& binaryCode, bool clear = true, uint32_t baseAddress = 0x0);
+
Ui::EditTab* m_ui;
+ Assembler* m_assembler = nullptr;
};

MODIFY
src/mainwindow.cpp
src/mainwindow.cpp
@@ -190,7 +190,6 @@ void MainWindow::loadAssemblyFileTriggered() {
}
void MainWindow::loadBinaryFile(QString filename) {
- m_editTab->setTimerEnabled(false);
m_editTab->setInputMode(false);
m_processorTab->restart();
Parser::getParser()->loadBinaryFile(filename);
@@ -202,9 +201,8 @@ void MainWindow::loadAssemblyFile(QString fileName) {
// ... load file
QFile file(fileName);
m_editTab->setInputMode(true);
- m_editTab->setTimerEnabled(true);
Parser::getParser()->clear();
- m_editTab->clearOutputArray();
+ m_editTab->clear();
m_processorTab->restart();
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_editTab->setAssemblyText(file.readAll());

Move assembler from CodeEditor to EditTab