| 1 | // File: sourcecodepane.cpp |
| 2 | /* |
| 3 | Pep8-1 is a virtual machine for writing machine language and assembly |
| 4 | language programs. |
| 5 | |
| 6 | Copyright (C) 2009 J. Stanley Warford, Pepperdine University |
| 7 | |
| 8 | This program is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <QList> |
| 23 | #include <QStringList> |
| 24 | #include <QTextCursor> |
| 25 | #include <QPalette> |
| 26 | #include <QSyntaxHighlighter> |
| 27 | #include <QFontDialog> |
| 28 | #include <QKeyEvent> |
| 29 | #include "sourcecodepane.h" |
| 30 | #include "ui_sourcecodepane.h" |
| 31 | #include "code.h" |
| 32 | #include "sim.h" |
| 33 | #include "pep.h" |
| 34 | |
| 35 | // #include <QDebug> |
| 36 | |
| 37 | SourceCodePane::SourceCodePane(QWidget *parent) : |
| 38 | QWidget(parent), |
| 39 | ui(new Ui::SourceCodePane) |
| 40 | { |
| 41 | ui->setupUi(this); |
| 42 | |
| 43 | connect(ui->textEdit->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setLabelToModified(bool))); |
| 44 | |
| 45 | pepHighlighter = new PepHighlighter(ui->textEdit->document()); |
| 46 | |
| 47 | connect(ui->textEdit, SIGNAL(undoAvailable(bool)), this, SIGNAL(undoAvailable(bool))); |
| 48 | connect(ui->textEdit, SIGNAL(redoAvailable(bool)), this, SIGNAL(redoAvailable(bool))); |
| 49 | |
| 50 | ui->label->setFont(QFont(Pep::labelFont, Pep::labelFontSize)); |
| 51 | ui->textEdit->setFont(QFont(Pep::codeFont, Pep::codeFontSize)); |
| 52 | } |
| 53 | |
| 54 | SourceCodePane::~SourceCodePane() |
| 55 | { |
| 56 | delete ui; |
| 57 | } |
| 58 | |
| 59 | bool SourceCodePane::assemble() |
| 60 | { |
| 61 | QString sourceLine; |
| 62 | QString errorString; |
| 63 | QStringList sourceCodeList; |
| 64 | Code *code; |
| 65 | int lineNum = 0; |
| 66 | bool dotEndDetected = false; |
| 67 | |
| 68 | removeErrorMessages(); |
| 69 | Asm::listOfReferencedSymbols.clear(); |
| 70 | Asm::listOfReferencedSymbolLineNums.clear(); |
| 71 | Pep::memAddrssToAssemblerListing->clear(); |
| 72 | Pep::symbolTable.clear(); |
| 73 | Pep::adjustSymbolValueForBurn.clear(); |
| 74 | Pep::symbolFormat.clear(); |
| 75 | Pep::symbolFormatMultiplier.clear();; |
| 76 | Pep::symbolTraceList.clear(); // Does this clear the lists within the map? |
| 77 | Pep::globalStructSymbols.clear(); |
| 78 | Pep::blockSymbols.clear(); |
| 79 | Pep::equateSymbols.clear(); |
| 80 | while (!codeList.isEmpty()) { |
| 81 | delete codeList.takeFirst(); |
| 82 | } |
| 83 | QString sourceCode = ui->textEdit->toPlainText(); |
| 84 | sourceCodeList = sourceCode.split('\n'); |
| 85 | Pep::byteCount = 0; |
| 86 | Pep::burnCount = 0; |
| 87 | while (lineNum < sourceCodeList.size() && !dotEndDetected) { |
| 88 | sourceLine = sourceCodeList[lineNum]; |
| 89 | if (!Asm::processSourceLine(sourceLine, lineNum, code, errorString, dotEndDetected)) { |
| 90 | appendMessageInSourceCodePaneAt(lineNum, errorString); |
| 91 | return false; |
| 92 | } |
| 93 | codeList.append(code); |
| 94 | lineNum++; |
| 95 | } |
| 96 | if (!dotEndDetected) { |
| 97 | errorString = ";ERROR: Missing .END sentinel."; |
| 98 | appendMessageInSourceCodePaneAt(0, errorString); |
| 99 | return false; |
| 100 | } |
| 101 | if (Pep::byteCount > 65535) { |
| 102 | errorString = ";ERROR: Object code size too large to fit into memory."; |
| 103 | appendMessageInSourceCodePaneAt(0, errorString); |
| 104 | return false; |
| 105 | } |
| 106 | for (int i = 0; i < Asm::listOfReferencedSymbols.length(); i++) { |
| 107 | if (!Pep::symbolTable.contains(Asm::listOfReferencedSymbols[i])) { |
| 108 | errorString = ";ERROR: Symbol " + Asm::listOfReferencedSymbols[i] + " is used but not defined."; |
| 109 | appendMessageInSourceCodePaneAt(Asm::listOfReferencedSymbolLineNums[i], errorString); |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | Pep::traceTagWarning = false; |
| 114 | for (int i = 0; i < codeList.size(); i++) { |
| 115 | if (!codeList[i]->processFormatTraceTags(lineNum, errorString)) { |
| 116 | appendMessageInSourceCodePaneAt(lineNum, errorString); |
| 117 | Pep::traceTagWarning = true; |
| 118 | } |
| 119 | } |
| 120 | if (!Pep::traceTagWarning && !(Pep::blockSymbols.isEmpty() && Pep::equateSymbols.isEmpty())) { |
| 121 | for (int i = 0; i < codeList.size(); i++) { |
| 122 | if (!codeList[i]->processSymbolTraceTags(lineNum, errorString)) { |
| 123 | appendMessageInSourceCodePaneAt(lineNum, errorString); |
| 124 | Pep::traceTagWarning = true; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | QList<int> SourceCodePane::getObjectCode() |
| 132 | { |
| 133 | objectCode.clear(); |
| 134 | for (int i = 0; i < codeList.size(); ++i) { |
| 135 | codeList.at(i)->appendObjectCode(objectCode); |
| 136 | } |
| 137 | return objectCode; |
| 138 | } |
| 139 | |
| 140 | QStringList SourceCodePane::getAssemblerListingList() |
| 141 | { |
| 142 | assemblerListingList.clear(); |
| 143 | listingTraceList.clear(); |
| 144 | hasCheckBox.clear(); |
| 145 | for (int i = 0; i < codeList.length(); i++) { |
| 146 | codeList[i]->appendSourceLine(assemblerListingList, listingTraceList, hasCheckBox); |
| 147 | } |
| 148 | return assemblerListingList; |
| 149 | } |
| 150 | |
| 151 | QStringList SourceCodePane::getListingTraceList() |
| 152 | { |
| 153 | return listingTraceList; |
| 154 | } |
| 155 | |
| 156 | QList<bool> SourceCodePane::getHasCheckBox() |
| 157 | { |
| 158 | return hasCheckBox; |
| 159 | } |
| 160 | |
| 161 | void SourceCodePane::adjustCodeList(int addressDelta) |
| 162 | { |
| 163 | for (int i = 0; i < codeList.length(); i++) { |
| 164 | codeList[i]->adjustMemAddress(addressDelta); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void SourceCodePane::installOS() |
| 169 | { |
| 170 | for (int i = 0; i < 65536; i++) { |
| 171 | Sim::Mem[i] = 0; |
| 172 | } |
| 173 | int j = Pep::romStartAddress; |
| 174 | for (int i = 0; i < objectCode.size(); i++) { |
| 175 | Sim::Mem[j++] = objectCode[i]; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bool SourceCodePane::installDefaultOs() |
| 180 | { |
| 181 | QString sourceLine; |
| 182 | QString errorString; |
| 183 | QStringList sourceCodeList; |
| 184 | Code *code; |
| 185 | int lineNum = 0; |
| 186 | bool dotEndDetected = false; |
| 187 | |
| 188 | Asm::listOfReferencedSymbols.clear(); |
| 189 | Pep::memAddrssToAssemblerListing->clear(); |
| 190 | Pep::symbolTable.clear(); |
| 191 | Pep::adjustSymbolValueForBurn.clear(); |
| 192 | while (!codeList.isEmpty()) { |
| 193 | delete codeList.takeFirst(); |
| 194 | } |
| 195 | QString sourceCode = Pep::resToString(":/help/figures/pep8os.pep"); |
| 196 | sourceCodeList = sourceCode.split('\n'); |
| 197 | Pep::byteCount = 0; |
| 198 | Pep::burnCount = 0; |
| 199 | while (lineNum < sourceCodeList.size() && !dotEndDetected) { |
| 200 | sourceLine = sourceCodeList[lineNum]; |
| 201 | if (!Asm::processSourceLine(sourceLine, lineNum, code, errorString, dotEndDetected)) { |
| 202 | return false; |
| 203 | } |
| 204 | codeList.append(code); |
| 205 | lineNum++; |
| 206 | } |
| 207 | if (!dotEndDetected) { |
| 208 | return false; |
| 209 | } |
| 210 | if (Pep::byteCount > 65535) { |
| 211 | return false; |
| 212 | } |
| 213 | for (int i = 0; i < Asm::listOfReferencedSymbols.length(); i++) { |
| 214 | if (!Pep::symbolTable.contains(Asm::listOfReferencedSymbols[i])) { |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (Pep::burnCount != 1) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // Adjust for .BURN |
| 224 | int addressDelta = Pep::dotBurnArgument - Pep::byteCount + 1; |
| 225 | QMutableMapIterator <QString, int> i(Pep::symbolTable); |
| 226 | while (i.hasNext()) { |
| 227 | i.next(); |
| 228 | if (Pep::adjustSymbolValueForBurn.value(i.key())) { |
| 229 | i.setValue(i.value() + addressDelta); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | adjustCodeList(addressDelta); |
| 234 | Pep::romStartAddress += addressDelta; |
| 235 | getObjectCode(); |
| 236 | installOS(); |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | void SourceCodePane::removeErrorMessages() |
| 242 | { |
| 243 | QTextCursor cursor(ui->textEdit->document()->find(";ERROR:")); |
| 244 | while (!cursor.isNull()) { |
| 245 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); |
| 246 | cursor.removeSelectedText(); |
| 247 | cursor = ui->textEdit->document()->find(";ERROR:", cursor); |
| 248 | } |
| 249 | cursor = ui->textEdit->document()->find(";WARNING:"); |
| 250 | while (!cursor.isNull()) { |
| 251 | cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); |
| 252 | cursor.removeSelectedText(); |
| 253 | cursor = ui->textEdit->document()->find(";WARNING:", cursor); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void SourceCodePane::appendMessageInSourceCodePaneAt(int lineNumber, QString message) |
| 258 | { |
| 259 | QTextCursor cursor(ui->textEdit->document()); |
| 260 | cursor.setPosition(0); |
| 261 | for (int i = 0; i < lineNumber; i++) { |
| 262 | cursor.movePosition(QTextCursor::NextBlock); |
| 263 | } |
| 264 | cursor.movePosition(QTextCursor::EndOfLine); |
| 265 | cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1); |
| 266 | if (cursor.selectedText() == " ") { |
| 267 | cursor.setPosition(cursor.anchor()); |
| 268 | } |
| 269 | else { |
| 270 | cursor.setPosition(cursor.anchor()); |
| 271 | cursor.insertText(" "); |
| 272 | } |
| 273 | cursor.insertText(message); |
| 274 | } |
| 275 | |
| 276 | void SourceCodePane::setSourceCodePaneText(QString string) |
| 277 | { |
| 278 | ui->textEdit->setText(string); |
| 279 | } |
| 280 | |
| 281 | void SourceCodePane::clearSourceCode() |
| 282 | { |
| 283 | ui->textEdit->clear(); |
| 284 | codeList.clear(); // This may cause issues with "format from listing" - but this needs to be cleared regardless. |
| 285 | } |
| 286 | |
| 287 | bool SourceCodePane::isModified() |
| 288 | { |
| 289 | return ui->textEdit->document()->isModified(); |
| 290 | } |
| 291 | |
| 292 | void SourceCodePane::setModifiedFalse() |
| 293 | { |
| 294 | ui->textEdit->document()->setModified(false); |
| 295 | } |
| 296 | |
| 297 | QString SourceCodePane::toPlainText() |
| 298 | { |
| 299 | return ui->textEdit->toPlainText(); |
| 300 | } |
| 301 | |
| 302 | void SourceCodePane::setCurrentFile(QString string) |
| 303 | { |
| 304 | ui->label->setText("Source Code - " + string); |
| 305 | } |
| 306 | |
| 307 | void SourceCodePane::highlightOnFocus() |
| 308 | { |
| 309 | if (ui->textEdit->hasFocus()) { |
| 310 | ui->label->setAutoFillBackground(true); |
| 311 | } |
| 312 | else { |
| 313 | ui->label->setAutoFillBackground(false); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | bool SourceCodePane::hasFocus() |
| 318 | { |
| 319 | return ui->textEdit->hasFocus(); |
| 320 | } |
| 321 | |
| 322 | void SourceCodePane::undo() |
| 323 | { |
| 324 | ui->textEdit->undo(); |
| 325 | } |
| 326 | |
| 327 | void SourceCodePane::redo() |
| 328 | { |
| 329 | ui->textEdit->redo(); |
| 330 | } |
| 331 | |
| 332 | bool SourceCodePane::isUndoable() |
| 333 | { |
| 334 | return ui->textEdit->document()->isUndoAvailable(); |
| 335 | } |
| 336 | |
| 337 | bool SourceCodePane::isRedoable() |
| 338 | { |
| 339 | return ui->textEdit->document()->isRedoAvailable(); |
| 340 | } |
| 341 | |
| 342 | void SourceCodePane::cut() |
| 343 | { |
| 344 | ui->textEdit->cut(); |
| 345 | } |
| 346 | |
| 347 | void SourceCodePane::copy() |
| 348 | { |
| 349 | ui->textEdit->copy(); |
| 350 | } |
| 351 | |
| 352 | void SourceCodePane::paste() |
| 353 | { |
| 354 | ui->textEdit->paste(); |
| 355 | } |
| 356 | |
| 357 | void SourceCodePane::setReadOnly(bool b) |
| 358 | { |
| 359 | ui->textEdit->setReadOnly(b); |
| 360 | } |
| 361 | |
| 362 | void SourceCodePane::tab() |
| 363 | { |
| 364 | if (!ui->textEdit->isReadOnly()) { |
| 365 | QTextCursor cursor = ui->textEdit->textCursor(); |
| 366 | cursor.movePosition(QTextCursor::StartOfLine); |
| 367 | QString string; |
| 368 | int curLinePos = ui->textEdit->textCursor().position() - cursor.position(); |
| 369 | int spaces; |
| 370 | if (curLinePos < 9) { |
| 371 | spaces = 9 - curLinePos; |
| 372 | } |
| 373 | else if (curLinePos < 17) { |
| 374 | spaces = 17 - curLinePos; |
| 375 | } |
| 376 | else if (curLinePos < 29) { |
| 377 | spaces = 29 - curLinePos; |
| 378 | } |
| 379 | else if (curLinePos == 29) { |
| 380 | spaces = 5; |
| 381 | } |
| 382 | else { |
| 383 | spaces = 4 - ((curLinePos - 30) % 4); |
| 384 | } |
| 385 | |
| 386 | for (int i = 0; i < spaces; i++) { |
| 387 | string.append(" "); |
| 388 | } |
| 389 | |
| 390 | ui->textEdit->insertPlainText(string); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void SourceCodePane::onFontChanged(QFont font) |
| 395 | { |
| 396 | ui->textEdit->setFont(font); |
| 397 | } |
| 398 | |
| 399 | void SourceCodePane::mouseReleaseEvent(QMouseEvent *) |
| 400 | { |
| 401 | ui->textEdit->setFocus(); |
| 402 | } |
| 403 | |
| 404 | void SourceCodePane::mouseDoubleClickEvent(QMouseEvent *) |
| 405 | { |
| 406 | emit labelDoubleClicked(Enu::ESource); |
| 407 | } |
| 408 | |
| 409 | void SourceCodePane::setLabelToModified(bool modified) |
| 410 | { |
| 411 | QString temp = ui->label->text(); |
| 412 | if (modified) { |
| 413 | ui->label->setText(temp.append(temp.endsWith(QChar('*')) ? "" : "*")); |
| 414 | } |
| 415 | else if (temp.endsWith(QChar('*'))) { |
| 416 | temp.chop(1); |
| 417 | ui->label->setText(temp); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 |