Skip to content

Commit

Permalink
Fix compile error with string literals in ternary operator
Browse files Browse the repository at this point in the history
The ':' symbol was not recognized as a separator in stringize mode
so you always got a weird compile error with a code like this:

(condition) ? "yes : "no"

error 001: expected token: "-string end-", but found "-identifier-"

See 2) here: http://forum.sa-mp.com/showthread.php?t=355877
  • Loading branch information
Zeex committed Dec 31, 2013
1 parent 5173284 commit 764b373
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
22 changes: 13 additions & 9 deletions stringize.patch
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
static cell litchar(const unsigned char **lptr,int flags);
static symbol *find_symbol(const symbol *root,const char *name,int fnumber,int automaton,int *cmptag);

@@ -1724,32 +1725,136 @@ SC_FUNC void preprocess(void)
@@ -1724,32 +1725,140 @@ SC_FUNC void preprocess(void)
} while (iscommand!=CMD_NONE && iscommand!=CMD_TERM && freading); /* enddo */
}

Expand Down Expand Up @@ -37,7 +37,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
+ lptr--;
+ instring=1;
+ *flags |= STRINGIZE;
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' || *lptr=='\r' || *lptr=='\n') {
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' |

This comment has been minimized.

Copy link
@jte

jte Jan 11, 2014

You mistyped the bitwise OR operator "|" instead of logical OR operator "||".

This comment has been minimized.

Copy link
@Zeex

Zeex Jan 12, 2014

Author Member

Thanks.

+ *lptr==':' || *lptr=='\r' || *lptr=='\n') {
+ break;
+ } else if (*lptr!=' ' && *lptr!='\t') {
+ error(1,"-string end-","-identifier-");
Expand All @@ -59,7 +60,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
+ lptr=stringize+1;
+ *flags &= ~STRINGIZE;
+ continue;
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' || *stringize==';') { /* end */
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' ||
+ *stringize==';' || *stringize==':') { /* end */
+ lptr=stringize;
+ break;
+ } else if (*stringize=='\0') {
Expand All @@ -78,9 +80,9 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
} /* while */
- litadd(0); /* terminate string */
+ litadd(0);
+
+
+ if (*lptr==',' || *lptr==')' || *lptr=='}' || *lptr==';' ||
+ *lptr=='\n' || *lptr=='\r')
+ *lptr==':' || *lptr=='\n' || *lptr=='\r')
+ lptr=stringize; /* backtrack to end of last string for closing " */
return lptr;
}
Expand All @@ -92,7 +94,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
ucell val,c;
+ unsigned char *stringize;
+ int instring=1;
+ if (*flags & STRINGIZE)
+ if (*flags & STRINGIZE)
+ while (*lptr==' ' || *lptr=='\t')
+ lptr++;

Expand All @@ -113,7 +115,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
+ lptr--;
+ instring=1;
+ *flags |= STRINGIZE;
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' || *lptr=='\r' || *lptr=='\n') {
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' ||
+ *lptr==':' || *lptr=='\r' || *lptr=='\n') {
+ break;
+ } else if (*lptr!=' ' && *lptr!='\t') {
+ error(1,"-string end-","-identifier-");
Expand All @@ -135,7 +138,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
+ lptr=stringize+1;
+ *flags &= ~STRINGIZE;
+ continue;
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' || *stringize==';') { /* end */
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' |

This comment has been minimized.

Copy link
@jte

jte Jan 11, 2014

You mistyped the bitwise OR operator "|" instead of logical OR operator "||".

+ *stringize==';' || *stringize==':') { /* end */
+ lptr=stringize;
+ break;
+ } else if (*stringize=='\0') {
Expand Down Expand Up @@ -167,7 +171,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
litadd(0); /* add full cell of zeros */
+
+ if (*lptr==',' || *lptr==')' || *lptr=='}' || *lptr==';' ||
+ *lptr=='\n' || *lptr=='\r')
+ *lptr==':' || *lptr=='\n' || *lptr=='\r')
+ lptr=stringize; /* backtrack to end of last string for closing " */
return lptr;
}
Expand Down
32 changes: 27 additions & 5 deletions stringize.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,46 @@
#error Must be compiled with runtime checks
#endif

forward force_compile();

native print(const s[]);
native strcmp(const string1[], const string2[],
bool:ignorecase=false, length=cellmax);

#define N 1
#define F 2.34

assert_equal(const s1[], const s2[]) {
static assert_equal(const s1[], const s2[]) {
assert(strcmp(s1, s2) == 0);
}

main() {
test_concat() {
assert_equal("foo""bar", "foobar");
assert_equal("foo" "bar", "foobar");
assert_equal("foo" "bar", "foobar");
assert_equal("foo"#"bar", "foobar");
assert_equal("foo"##"bar", "foobar");
assert_equal("foo"#######"bar", "foobar");
}

test_stringize() {
#define N 1
#define F 2.34
assert_equal(#N, "1");
assert_equal(#F, "2.34");
assert_equal(#N #F, "12.34");
#undef N
#undef F
}

main() {
test_concat();
test_stringize();
}

ternary_op() {
new a = 5;
print((a == 5) ? "is five" : !"is not five");
print((a != 5) ? !"is not five" : "is five");
}

public force_compile() {
ternary_op();
}

0 comments on commit 764b373

Please sign in to comment.