Create rules for no-op operations instead of special-case handling
Previously, ecall and nop were handled separately. This is unnecessary, since the underlying logic perfectly allows for no-op operations.
Furthermore, add strings as an instruction argument type.
MODIFY
src/syntaxhighlighter.cpp
src/syntaxhighlighter.cpp
@@ -70,6 +70,14 @@ QString FieldType::validateField(const QString& field) const {
return QString("label \"%1\" is undefined").arg(field);
}
}
+ case Type::String: {
+ if ((field[0] == "\'" || field[0] == "\"") &&
+ (field[field.length() - 1] == "\'" || field[field.length() - 1] == "\"")) {
+ return QString();
+ } else {
+ return QString("Invalid string").arg(field);
+ }
+ }
}
return QString("Validation error");
}
@@ -208,6 +216,12 @@ SyntaxHighlighter::SyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter
rule.pattern = QRegularExpression("(.*?[:])");
rule.format = commentFormat;
m_highlightingRules.append(rule);
+
+ // Create string highlighting rule
+ stringFormat.setForeground(QColor(Colors::Medalist));
+ rule.pattern = QRegularExpression("([\"'])(?:\\\1|.)*?\1");
+ rule.format = stringFormat;
+ m_highlightingRules.append(rule);
}
void SyntaxHighlighter::highlightBlock(const QString& text) {
@@ -390,6 +404,20 @@ void SyntaxHighlighter::createSyntaxRules() {
m_syntaxRules.insert(name, QList<SyntaxRule>() << rule);
}
+ // No-op instructions
+ // I type instructions
+ types.clear();
+ names.clear();
+ types.clear();
+ names << "ecall"
+ << "nop";
+ for (const auto& name : names) {
+ rule.instr = name;
+ rule.fields = 1;
+ rule.inputs = types;
+ m_syntaxRules.insert(name, QList<SyntaxRule>() << rule);
+ }
+
// Load instructions
QMap<QString, QList<SyntaxRule>> loadRules;
types.clear();
@@ -458,6 +486,7 @@ void SyntaxHighlighter::createSyntaxRules() {
rule.inputs = types;
storeRules.insert(name, QList<SyntaxRule>() << rule);
}
+
// S type pseudo-ops
types.clear();
names.clear();
@@ -537,24 +566,6 @@ QString SyntaxHighlighter::checkSyntax(const QString& input) {
return QString();
}
- // Check for assembler directives
- string = fields[0];
- if (fields.size() == 1) {
- if (string[0] == '.') {
- string = string.remove('.');
- if (ASMDirectives.contains(string)) {
- fields.removeFirst(); // valid assembler directive detected
- } else {
- return QString("Unknown assembler directive");
- }
- } else if (fields[0] == "ecall" || fields[0] == "nop") {
- // Allow specific 1-word instructions
- fields.removeFirst();
- } else {
- return QString("Unknown instruction");
- }
- }
-
// -- Validate remaining fields --
// For the corresponding rules to an instruction in m_syntaxRules, the SyntaxRule list will be iterated through
// until either a rule has validated a field, or the end of the syntaxrule list is reached

MODIFY
src/syntaxhighlighter.h
src/syntaxhighlighter.h
Matches instruction names directly, and register aliases/true name.
Matches immediate values by regex*/
-enum class Type { Immediate, Register, Offset };
+enum class Type { Immediate, Register, Offset, String };
class SyntaxHighlighter;
class FieldType {
@@ -66,6 +66,7 @@ private:
QTextCharFormat instrFormat;
QTextCharFormat immFormat;
QTextCharFormat commentFormat;
+ QTextCharFormat stringFormat;
QTextCharFormat errorFormat;
QMap<QString, int> m_labelPosMap;

Create rules for no-op operations instead of special-case handling

Previously, ecall and nop were handled separately. This is unnecessary, since the underlying logic perfectly allows for no-op operations.

Furthermore, add strings as an instruction argument type.