david hoskin found a bug in cpp. - is evaluated right to left, so it computes the wrong result. for example # if 0 - 0 + 4 != 4 # error bogus preprocessor # endif errors, because cpp computes this as if it were # if 0 - (0 + 4) != 4 this is because it evaluates right to left, not right to left. it turns out this is because the evalop uses < rather than <= as the pop (loop) condition for the prec stack. thus a + b + c will push to the end then pop from the right. while the according-to-hoyle fix is to change < to <=, this was done on purpose. it allows symbols that need no eval to be omitted. it would be a major rework to change this assumption. so the hack-upon-hack solution might be to just convert - to + UMINUS(thing). this is going to cause trouble if someone is doing fancy arithmetic with division and counting on non-underflow. Reference: /n/atom/patch/applied/cppifltorhack Date: Sun Mar 2 17:51:24 CET 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/cpp/eval.c Sun Mar 2 17:51:10 2014 +++ /sys/src/cmd/cpp/eval.c Sun Mar 2 17:51:11 2014 @@ -122,6 +122,7 @@ op = ops; *op++ = END; for (rand=0, tp = trp->bp+ntok; tp < trp->lp; tp++) { + retry: if(op >= ops + NSTAK) sysfatal("cpp: can't evaluate #if: increase NSTAK"); switch(tp->type) { @@ -161,6 +162,12 @@ } continue; } + if(tp->type==MINUS){ + *op++ = UMINUS; + tp->type = PLUS; + goto retry; + } + /* flow through */ /* plain binary */