Skip to content

Commit 764b373

Browse files
committed
Fix compile error with string literals in ternary operator
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
1 parent 5173284 commit 764b373

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

stringize.patch

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
99
static cell litchar(const unsigned char **lptr,int flags);
1010
static symbol *find_symbol(const symbol *root,const char *name,int fnumber,int automaton,int *cmptag);
1111

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

@@ -37,7 +37,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
3737
+ lptr--;
3838
+ instring=1;
3939
+ *flags |= STRINGIZE;
40-
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' || *lptr=='\r' || *lptr=='\n') {
40+
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' |
41+
+ *lptr==':' || *lptr=='\r' || *lptr=='\n') {
4142
+ break;
4243
+ } else if (*lptr!=' ' && *lptr!='\t') {
4344
+ error(1,"-string end-","-identifier-");
@@ -59,7 +60,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
5960
+ lptr=stringize+1;
6061
+ *flags &= ~STRINGIZE;
6162
+ continue;
62-
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' || *stringize==';') { /* end */
63+
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' ||
64+
+ *stringize==';' || *stringize==':') { /* end */
6365
+ lptr=stringize;
6466
+ break;
6567
+ } else if (*stringize=='\0') {
@@ -78,9 +80,9 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
7880
} /* while */
7981
- litadd(0); /* terminate string */
8082
+ litadd(0);
81-
+
83+
+
8284
+ if (*lptr==',' || *lptr==')' || *lptr=='}' || *lptr==';' ||
83-
+ *lptr=='\n' || *lptr=='\r')
85+
+ *lptr==':' || *lptr=='\n' || *lptr=='\r')
8486
+ lptr=stringize; /* backtrack to end of last string for closing " */
8587
return lptr;
8688
}
@@ -92,7 +94,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
9294
ucell val,c;
9395
+ unsigned char *stringize;
9496
+ int instring=1;
95-
+ if (*flags & STRINGIZE)
97+
+ if (*flags & STRINGIZE)
9698
+ while (*lptr==' ' || *lptr=='\t')
9799
+ lptr++;
98100

@@ -113,7 +115,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
113115
+ lptr--;
114116
+ instring=1;
115117
+ *flags |= STRINGIZE;
116-
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' || *lptr=='\r' || *lptr=='\n') {
118+
+ } else if (*lptr==')' || *lptr==',' || *lptr=='}' || *lptr==';' ||
119+
+ *lptr==':' || *lptr=='\r' || *lptr=='\n') {
117120
+ break;
118121
+ } else if (*lptr!=' ' && *lptr!='\t') {
119122
+ error(1,"-string end-","-identifier-");
@@ -135,7 +138,8 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
135138
+ lptr=stringize+1;
136139
+ *flags &= ~STRINGIZE;
137140
+ continue;
138-
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' || *stringize==';') { /* end */
141+
+ } else if (*stringize==',' || *stringize==')' || *stringize=='}' |
142+
+ *stringize==';' || *stringize==':') { /* end */
139143
+ lptr=stringize;
140144
+ break;
141145
+ } else if (*stringize=='\0') {
@@ -167,7 +171,7 @@ diff -ruNpw SOURCE/COMPILER/sc2.c SOURCE_patched/COMPILER/sc2.c
167171
litadd(0); /* add full cell of zeros */
168172
+
169173
+ if (*lptr==',' || *lptr==')' || *lptr=='}' || *lptr==';' ||
170-
+ *lptr=='\n' || *lptr=='\r')
174+
+ *lptr==':' || *lptr=='\n' || *lptr=='\r')
171175
+ lptr=stringize; /* backtrack to end of last string for closing " */
172176
return lptr;
173177
}

stringize.pwn

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,46 @@
22
#error Must be compiled with runtime checks
33
#endif
44

5+
forward force_compile();
6+
7+
native print(const s[]);
58
native strcmp(const string1[], const string2[],
69
bool:ignorecase=false, length=cellmax);
710

8-
#define N 1
9-
#define F 2.34
10-
11-
assert_equal(const s1[], const s2[]) {
11+
static assert_equal(const s1[], const s2[]) {
1212
assert(strcmp(s1, s2) == 0);
1313
}
1414

15-
main() {
15+
test_concat() {
1616
assert_equal("foo""bar", "foobar");
1717
assert_equal("foo" "bar", "foobar");
1818
assert_equal("foo" "bar", "foobar");
1919
assert_equal("foo"#"bar", "foobar");
2020
assert_equal("foo"##"bar", "foobar");
2121
assert_equal("foo"#######"bar", "foobar");
22+
}
23+
24+
test_stringize() {
25+
#define N 1
26+
#define F 2.34
2227
assert_equal(#N, "1");
2328
assert_equal(#F, "2.34");
2429
assert_equal(#N #F, "12.34");
30+
#undef N
31+
#undef F
32+
}
33+
34+
main() {
35+
test_concat();
36+
test_stringize();
37+
}
38+
39+
ternary_op() {
40+
new a = 5;
41+
print((a == 5) ? "is five" : !"is not five");
42+
print((a != 5) ? !"is not five" : "is five");
43+
}
44+
45+
public force_compile() {
46+
ternary_op();
2547
}

0 commit comments

Comments
 (0)