@@ -61,6 +61,8 @@ static int dbltest(void (*oper)(),value *lval1,value *lval2);
61
61
static int commutative (void (* oper )());
62
62
static int constant (value * lval );
63
63
64
+ static const char str_w247unary []= "a \"bool:\" value" ;
65
+ static const char str_w247binary []= "\"bool:\" values" ;
64
66
static char lastsymbol [sNAMEMAX + 1 ]; /* name of last function/variable */
65
67
static int bitwise_opercount ; /* count of bitwise operators in an expression */
66
68
static int decl_heap = 0 ;
@@ -78,15 +80,15 @@ static void (*op1[17])(void) = {
78
80
os_le ,os_ge ,os_lt ,os_gt , /* hier9, index 11 */
79
81
ob_eq ,ob_ne , /* hier10, index 15 */
80
82
};
81
- /* These two functions are defined because the functions inc() and dec() in
82
- * SC4.C have a different prototype than the other code generation functions.
83
+ /* These two macros are defined because the functions inc() and dec() in SC4.C
84
+ * have a different prototype than the other code generation functions.
83
85
* The arrays for user-defined functions use the function pointers for
84
86
* identifying what kind of operation is requested; these functions must all
85
87
* have the same prototype. As inc() and dec() are special cases already, it
86
- * is simplest to add two "do-nothing" functions .
88
+ * is simplest to cast them into "void (*)(void)" .
87
89
*/
88
- static void user_inc ( void ) {}
89
- static void user_dec ( void ) {}
90
+ #define user_inc (( void (*)( void))inc)
91
+ #define user_dec (( void (*)( void))dec)
90
92
91
93
/*
92
94
* Searches for a binary operator a list of operators. The list is stored in
@@ -646,9 +648,29 @@ static void plnge2(void (*oper)(void),
646
648
lval1 -> ident = iEXPRESSION ;
647
649
lval1 -> constval = 0 ;
648
650
} else {
649
- if ((oper == ob_sal || oper == os_sar || oper == ou_sar )
650
- && (lval2 -> ident == iCONSTEXPR && (lval2 -> constval < 0 || lval2 -> constval >=PAWN_CELL_SIZE )))
651
+ if (lval1 -> tag == BOOLTAG && lval2 -> tag == BOOLTAG
652
+ && oper != ob_or && oper != ob_xor && oper != ob_and && oper != ob_eq && oper != ob_ne ) {
653
+ static const void (* opers [])(void ) = {
654
+ os_mult ,os_div ,os_mod ,ob_add ,ob_sub ,ob_sal ,
655
+ os_sar ,ou_sar ,os_le ,os_ge ,os_lt ,os_gt
656
+ };
657
+ static const char * opnames [] = {
658
+ "*" ,"/" ,"%" ,"+" ,"-" ,"<<" ,
659
+ ">>" ,">>>" ,"<=" ,">=" ,"<" ,">"
660
+ };
661
+ int i ;
662
+ assert_static (arraysize (opers )== arraysize (opnames )); /* make sure the array sizes match */
663
+ assert_static (arraysize (op1 )== 17 ); /* in case a new operator is added into the compiler,
664
+ * arrays "opers" and "opnames" might need to be updated */
665
+ for (i = 0 ; i < arraysize (opers ); i ++ )
666
+ if (oper == opers [i ])
667
+ break ;
668
+ assert (i < arraysize (opers ));
669
+ error (247 ,opnames [i ],str_w247binary ); /* use of operator on \"bool:\" values */
670
+ } else if ((oper == ob_sal || oper == os_sar || oper == ou_sar )
671
+ && (lval2 -> ident == iCONSTEXPR && (lval2 -> constval < 0 || lval2 -> constval >=PAWN_CELL_SIZE ))) {
651
672
error (241 ); /* negative or too big shift count */
673
+ } /* if */
652
674
if (lval1 -> ident == iCONSTEXPR && lval2 -> ident == iCONSTEXPR ) {
653
675
/* only constant expression if both constant */
654
676
stgdel (lval_stgidx ,lval_cidx ); /* scratch generated code and calculate */
@@ -1283,8 +1305,11 @@ static int hier2(value *lval)
1283
1305
assert (lval -> sym != NULL );
1284
1306
if ((lval -> sym -> usage & uCONST )!= 0 )
1285
1307
return error (22 ); /* assignment to const argument */
1286
- if (!check_userop (user_inc ,lval -> tag ,0 ,1 ,lval ,& lval -> tag ))
1308
+ if (!check_userop (user_inc ,lval -> tag ,0 ,1 ,lval ,& lval -> tag )) {
1309
+ if (lval -> tag == BOOLTAG )
1310
+ error (247 ,"++" ,str_w247unary ); /* use of operator "++" on a "bool:" value */
1287
1311
inc (lval ); /* increase variable first */
1312
+ } /* if */
1288
1313
rvalue (lval ); /* and read the result into PRI */
1289
1314
pc_sideeffect = TRUE;
1290
1315
return FALSE; /* result is no longer lvalue */
@@ -1294,8 +1319,11 @@ static int hier2(value *lval)
1294
1319
assert (lval -> sym != NULL );
1295
1320
if ((lval -> sym -> usage & uCONST )!= 0 )
1296
1321
return error (22 ); /* assignment to const argument */
1297
- if (!check_userop (user_dec ,lval -> tag ,0 ,1 ,lval ,& lval -> tag ))
1322
+ if (!check_userop (user_dec ,lval -> tag ,0 ,1 ,lval ,& lval -> tag )) {
1323
+ if (lval -> tag == BOOLTAG )
1324
+ error (247 ,"--" ,str_w247unary ); /* use of operator "--" on a "bool:" value */
1298
1325
dec (lval ); /* decrease variable first */
1326
+ } /* if */
1299
1327
rvalue (lval ); /* and read the result into PRI */
1300
1328
pc_sideeffect = TRUE;
1301
1329
return FALSE; /* result is no longer lvalue */
@@ -1307,7 +1335,7 @@ static int hier2(value *lval)
1307
1335
if (lval -> ident == iVARIABLE || lval -> ident == iARRAYCELL )
1308
1336
lval -> ident = iEXPRESSION ;
1309
1337
if (lval -> tag == BOOLTAG )
1310
- error (makelong (247 ,3 ),"!" ); /* use of operator "~" on a "bool:" value always results in "true" */
1338
+ error (makelong (247 ,3 ),"~" , str_w247unary , " !" ); /* use of operator "~" on a "bool:" value; did you mean to use operator "!"? */
1311
1339
return FALSE;
1312
1340
case '!' : /* ! (logical negate) */
1313
1341
if (hier2 (lval ))
@@ -1352,6 +1380,8 @@ static int hier2(value *lval)
1352
1380
lval -> constval = - lval -> constval ;
1353
1381
if (lval -> ident == iVARIABLE || lval -> ident == iARRAYCELL )
1354
1382
lval -> ident = iEXPRESSION ;
1383
+ if (lval -> tag == BOOLTAG )
1384
+ error (makelong (247 ,3 ),"-" ,str_w247unary ,"!" ); /* use of operator "-" on a "bool:" value; did you mean to use operator "!"? */
1355
1385
} /* if */
1356
1386
return FALSE;
1357
1387
case tLABEL : /* tagname override */
@@ -1676,8 +1706,11 @@ static int hier2(value *lval)
1676
1706
rvalue (lval ); /* read current value into PRI */
1677
1707
if (saveresult )
1678
1708
swap1 (); /* save PRI on the stack, restore address in PRI */
1679
- if (!check_userop (user_inc ,lval -> tag ,0 ,1 ,lval ,& lval -> tag ))
1709
+ if (!check_userop (user_inc ,lval -> tag ,0 ,1 ,lval ,& lval -> tag )) {
1710
+ if (lval -> tag == BOOLTAG )
1711
+ error (247 ,"++" ,str_w247unary ); /* use of operator "++" on a "bool:" value */
1680
1712
inc (lval ); /* increase variable afterwards */
1713
+ } /* if */
1681
1714
if (saveresult )
1682
1715
popreg (sPRI ); /* restore PRI (result of rvalue()) */
1683
1716
pc_sideeffect = TRUE;
@@ -1694,8 +1727,11 @@ static int hier2(value *lval)
1694
1727
rvalue (lval ); /* read current value into PRI */
1695
1728
if (saveresult )
1696
1729
swap1 (); /* save PRI on the stack, restore address in PRI */
1697
- if (!check_userop (user_dec ,lval -> tag ,0 ,1 ,lval ,& lval -> tag ))
1730
+ if (!check_userop (user_dec ,lval -> tag ,0 ,1 ,lval ,& lval -> tag )) {
1731
+ if (lval -> tag == BOOLTAG )
1732
+ error (247 ,"--" ,str_w247unary ); /* use of operator "--" on a "bool:" value */
1698
1733
dec (lval ); /* decrease variable afterwards */
1734
+ } /* if */
1699
1735
if (saveresult )
1700
1736
popreg (sPRI ); /* restore PRI (result of rvalue()) */
1701
1737
pc_sideeffect = TRUE;
0 commit comments