@@ -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+ }
11071117bool 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 }
0 commit comments