Skip to content

Commit ef897db

Browse files
Char value for define values
1 parent ebb05d6 commit ef897db

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

Masfix.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -907,17 +907,17 @@ const map<char, char> escapeSequences = {
907907
{'\'','\''},
908908
{'0','\0'},
909909
};
910-
bool chopStrlit(char first, string& line, string& run, int& col, Loc loc, bool escape=false) {
910+
char escapeCharToken(Token& t) {
911+
assert(t.data.size() == 1 || (t.data.size() == 2 && escapeSequences.count(t.data.at(1))));
912+
return t.data.size() == 2 ? escapeSequences.at(t.data.at(1)) : t.data.at(0);
913+
}
914+
bool chopStrlit(char first, string& line, string& run, int& col, Loc loc) {
911915
run = "";
912916
for (size_t i = 0; i < line.size(); ++i) {
913917
if (line[i] == '\\') {
914918
checkReturnOnFail(++i < line.size() && escapeSequences.count(line.at(i)), "Invalid escape sequence" + errorQuoted(line.substr(i-1, 2)), loc);
915-
if (escape) {
916-
run.push_back(escapeSequences.at(line[i]));
917-
} else {
918-
run.push_back('\\');
919-
run.push_back(line[i]);
920-
}
919+
run.push_back('\\');
920+
run.push_back(line[i]);
921921
col += 2;
922922
} else if (line[i] == first) {
923923
line = line.substr(++i);
@@ -1104,19 +1104,29 @@ bool arglistFromTlist(Scope& scope, Loc& loc, Macro& mac) {
11041104
}
11051105
return true;
11061106
}
1107+
bool eatDefineValue(Scope& scope, Token& outToken, Loc& loc, bool sameLine) {
1108+
TokenTypes type = scope.hasNext() && scope->type == Tchar ? Tchar : Tnumeric;
1109+
returnOnFalse(eatToken(scope, loc, outToken, type, "Define value expected", sameLine));
1110+
if (type == Tchar) {
1111+
outToken = Token::fromCtx(Tnumeric, to_string((int)escapeCharToken(outToken)), outToken);
1112+
assert(to_string(stoi(outToken.data)) == outToken.data); // check good int
1113+
assert(stoi(outToken.data) <= WORD_MAX_VAL);
1114+
}
1115+
return true;
1116+
}
11071117
bool processDefineDef(Scope& scope, string name, Loc loc, Loc percentLoc) {
11081118
Token numeric;
11091119
if (scope.hasNext() && scope->type == Tlist && !scope->firstOnLine) {
11101120
Token& token = scope.currToken(); loc = token.loc;
11111121
checkReturnOnFail(!token.continued, "Unexpected continued field", token);
11121122
processArglistWrapper( bool retval = preprocess(scope); );
11131123
processArglistWrapper(
1114-
retval = eatToken(scope, loc, numeric, Tnumeric, "Define value expected", false);
1124+
retval = eatDefineValue(scope, numeric, loc, false);
11151125
retval = retval && check(!scope.hasNext(), "Unexpected token after value", scope.currToken());
11161126
);
11171127
scope.eatenToken(); // tlist
11181128
} else {
1119-
returnOnFalse(eatToken(scope, loc, numeric, Tnumeric, "Define value expected", true));
1129+
returnOnFalse(eatDefineValue(scope, numeric, loc, true));
11201130
}
11211131
scope.topNamespace().defines[name] = Define(name, percentLoc, numeric.data);
11221132
return true;
@@ -1565,9 +1575,7 @@ bool parseNumericalImmediate(Token& imm, Instr& instr) {
15651575
instr.immediate = stoi(imm.data);
15661576
checkReturnOnFail(to_string(instr.immediate) == imm.data, "Invalid instruction immediate", instr);
15671577
} else if (imm.type == Tchar) {
1568-
assert(imm.data.size() == 1 || (imm.data.size() == 2 && escapeSequences.count(imm.data.at(1))));
1569-
char val = imm.data.size() == 2 ? escapeSequences.at(imm.data.at(1)) : imm.data.at(0);
1570-
instr.immediate = val;
1578+
instr.immediate = escapeCharToken(imm);
15711579
} else {
15721580
unreachable();
15731581
}

tests/chars.mx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ lll ' ' ' '
4444

4545
; complex fields
4646
%(def('i')ne) ["LMA\0"]
47-
%(def('i')ne) [("LMAO")] ('^') # TODO
48-
%(def('i')ne) [("LMAO")] (65)
47+
%(def('i')ne) [("LMAO")] ('^')
48+
%define escaped '\r'
4949
out('u') '\n'
5050

5151
%macro testValues(defineName) {
@@ -59,6 +59,7 @@ out('u') '\n'
5959
outc '\n'
6060
outc '8'
6161
outc %(%defineName)
62+
outu %("escaped")
6263
out('c') '\n'
6364
}
6465
ld !testValues("LMAO")

tests/chars.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
:returncode 1
22

3-
:stdout 11
3+
:stdout 13
44
56'8" 
5-
8A
5+
8^13
66

77

8-
:stderr 2214
8+
:stderr 2149
99
tests\chars.mx:16:1 ERROR: Expected string or character termination
1010
tests\chars.mx:17:1 ERROR: Expected string or character termination
1111
tests\chars.mx:18:1 ERROR: Invalid character value
@@ -18,7 +18,6 @@ tests\chars.mx:36:3 ERROR: Expected string or character termination
1818
tests\chars.mx:41:1 ERROR: Expected string or character termination
1919
tests\chars.mx:43:1 ERROR: Invalid escape sequence '\;'
2020
tests\chars.mx:46:15 ERROR: Invalid define name 'LMA\0'
21-
tests\chars.mx:47:27 ERROR: Expected numeric token type, got '^'
2221
tests\chars.mx:2:1 ERROR: Unexpected token ';'
2322
tests\chars.mx:3:1 ERROR: Unexpected token '%'
2423
tests\chars.mx:4:1 ERROR: Unexpected token "%"

0 commit comments

Comments
 (0)