boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

250-stringize-punct.c (1033B)


      1 /* Stringize (`#x`) must reproduce each token's source spelling
      2  * (C11 ยง6.10.3.2). Two related bugs in %pp-toks->display /
      3  * %pp-tok->bv:
      4  *   1. PUNCT tokens were stringified by symbol->string, so `+`
      5  *      came out as "plus" instead of "+".
      6  *   2. A space was inserted between every two tokens regardless
      7  *      of source whitespace, so `S(1+2)` produced "1 plus 2"
      8  *      where C11 requires "1+2" (no whitespace = no space).
      9  * Spaces should only appear when the token sources don't abut.
     10  */
     11 
     12 #define S(x) #x
     13 
     14 static int slen(const char *s) { int n = 0; while (s[n] != 0) n++; return n; }
     15 static int smatch(const char *a, const char *b) {
     16     int i = 0;
     17     while (a[i] != 0 && b[i] != 0) { if (a[i] != b[i]) return 0; i++; }
     18     return a[i] == b[i];
     19 }
     20 
     21 int main(void) {
     22     if (!smatch(S(1+2), "1+2"))     return 1;
     23     if (!smatch(S(a*b), "a*b"))     return 2;
     24     if (!smatch(S(x->y), "x->y"))   return 3;
     25     /* Real whitespace becomes a single space. */
     26     if (!smatch(S(a + b), "a + b")) return 4;
     27     return 0;
     28 }