From 6c09f661b83dd13a8403243c1441efa5c4900538 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Thu, 22 Aug 2019 13:47:41 +0200 Subject: In include, use content from editor if file is already open. This allows to assemble from modified include files without need to save their content the first. Signed-off-by: Pavel Pisa --- qtmips_asm/simpleasm.cpp | 34 +++++++++++++++++++++++++--------- qtmips_asm/simpleasm.h | 10 ++++++---- qtmips_cli/main.cpp | 12 +----------- qtmips_gui/mainwindow.cpp | 15 ++++++++++++++- qtmips_gui/mainwindow.h | 14 ++++++++++++++ 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/qtmips_asm/simpleasm.cpp b/qtmips_asm/simpleasm.cpp index ad720b9..223b34d 100644 --- a/qtmips_asm/simpleasm.cpp +++ b/qtmips_asm/simpleasm.cpp @@ -256,16 +256,9 @@ bool SimpleAsm::process_line(QString line, QString filename, error = QString("recursive include of file: \"%1\"").arg(incname); res = false; } else { - QFile incfile(incname); - if (!incfile.open(QFile::ReadOnly | QFile::Text)) { - error = QString("cannot open file: \"%1\"").arg(incname); + if (!process_file(incname, error_ptr)) { res = false; - } else for (int ln = 1; !incfile.atEnd(); ln++) { - QString line = incfile.readLine(); - if (!process_line(line, incname, ln, error_ptr)) { - res = false; - break; - } + error = QString("error in included file: \"%1\"").arg(incname); } } if (!res) { @@ -567,6 +560,29 @@ bool SimpleAsm::process_line(QString line, QString filename, return true; } +bool SimpleAsm::process_file(QString filename, QString *error_ptr) { + QString error; + bool res = true; + QFile srcfile(filename); + if (!srcfile.open(QFile::ReadOnly | QFile::Text)) { + error = QString("cannot open file: \"%1\"").arg(filename); + emit report_message(messagetype::MSG_ERROR, "", 0, 0, error, ""); + error_occured = true; + if (error_ptr != nullptr) + *error_ptr = error; + return false; + } + for (int ln = 1; !srcfile.atEnd(); ln++) { + QString line = srcfile.readLine(); + if ((line.count() > 0) && (line.at(line.count() - 1) == '\n')) + line.truncate(line.count() - 1); + if (!process_line(line, filename, ln, error_ptr)) + res = false; + } + srcfile.close(); + return res; +} + bool SimpleAsm::finish(QString *error_ptr) { bool error_reported = false; foreach(machine::RelocExpression *r, reloc) { diff --git a/qtmips_asm/simpleasm.h b/qtmips_asm/simpleasm.h index f97904d..ac06ca1 100644 --- a/qtmips_asm/simpleasm.h +++ b/qtmips_asm/simpleasm.h @@ -36,8 +36,8 @@ #ifndef SIMPLEASM_H #define SIMPLEASM_H -#include -#include +#include +#include #include "fixmatheval.h" #include "qtmipsmachine.h" #include "messagetype.h" @@ -71,11 +71,13 @@ public: void setup(machine::MemoryAccess *mem, SymbolTableDb *symtab, std::uint32_t address); bool process_line(QString line, QString filename = "", int line_number = 0, QString *error_ptr = nullptr); + virtual bool process_file(QString filename, QString *error_ptr = nullptr); bool finish(QString *error_ptr = nullptr); -private: - QStringList include_stack; +protected: bool error_occured; bool fatal_occured; +private: + QStringList include_stack; SymbolTableDb *symtab; machine::MemoryAccess *mem; machine::RelocExpressionList reloc; diff --git a/qtmips_cli/main.cpp b/qtmips_cli/main.cpp index 08696b4..b318f13 100644 --- a/qtmips_cli/main.cpp +++ b/qtmips_cli/main.cpp @@ -334,18 +334,8 @@ bool assemble(QtMipsMachine &machine, MsgReport &msgrep, QString filename) { sasm.setup(mem, &symtab, 0x80020000); - QFile input_file(filename); - if (!input_file.open(QIODevice::ReadOnly)) { - cout << "cannot open filename " << filename.toLocal8Bit().data() << endl; + if (!sasm.process_file(filename)) return false; - } - - QTextStream in(&input_file); - for (int ln = 1; !in.atEnd(); ln++) { - QString line = in.readLine(); - sasm.process_line(line, filename, ln); - } - input_file.close(); return sasm.finish(); } diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index cbee3e5..09fe660 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -858,6 +858,19 @@ void MainWindow::message_selected(messagetype::Type type, QString file, int line central_window->setCurrentWidget(editor); } +bool SimpleAsmWithEditorCheck::process_file(QString filename, QString *error_ptr) { + SrcEditor *editor = mainwindow->source_editor_for_file(filename, false); + if (editor == nullptr) + return Super::process_file(filename, error_ptr); + QTextDocument *doc = editor->document(); + int ln = 1; + for ( QTextBlock block = doc->begin(); block.isValid(); block = block.next(), ln++) { + QString line = block.text(); + process_line(line, filename, ln); + } + return !error_occured; +} + void MainWindow::compile_source() { bool error_occured = false; if (current_srceditor == nullptr) @@ -883,7 +896,7 @@ void MainWindow::compile_source() { QTextDocument *doc = editor->document(); emit clear_messages(); - SimpleAsm sasm; + SimpleAsmWithEditorCheck sasm(this); connect(&sasm, SIGNAL(report_message(messagetype::Type,QString,int,int,QString,QString)), this, SIGNAL(report_message(messagetype::Type,QString,int,int,QString,QString))); diff --git a/qtmips_gui/mainwindow.h b/qtmips_gui/mainwindow.h index 13c8295..976c21e 100644 --- a/qtmips_gui/mainwindow.h +++ b/qtmips_gui/mainwindow.h @@ -58,10 +58,12 @@ #include "qtmipsmachine.h" #include "machineconfig.h" #include "srceditor.h" +#include "simpleasm.h" class MainWindow : public QMainWindow { Q_OBJECT + friend class SimpleAsmWithEditorCheck; public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); @@ -165,4 +167,16 @@ private: bool ignore_unsaved; }; +class SimpleAsmWithEditorCheck : public SimpleAsm { + Q_OBJECT + using Super = SimpleAsm; + +public: + SimpleAsmWithEditorCheck(MainWindow *a_mainwindow, QObject *parent = nullptr) : + Super(parent), mainwindow(a_mainwindow) {} + bool process_file(QString filename, QString *error_ptr = nullptr) override; +private: + MainWindow *mainwindow; +}; + #endif // MAINWINDOW_H -- cgit v1.2.3