4 ms·
I can agree that there's a repeating pattern in handling each of the binary operators (they are around a dozen), but I'm failing to see a pattern in fragments l
by bluetomcat 7y ago
I can agree that there's a repeating pattern in handling each of the binary operators (they are around a dozen), but I'm failing to see a pattern in fragments like these:
if (tk == ']') next(); else { printf("%d: close bracket expected\n", line); exit(-1); }
if (t > PTR) { *++e = PSH; *++e = IMM; *++e = sizeof(int); *++e = MUL; }
else if (t < PTR) { printf("%d: pointer type expected\n", line); exit(-1); }
- ufo 7y agoThis code is implementing pointer indexing: p[i]. The first line is reading the closing `]`. The second line is computing the pointer offset, which is equal to `i * sizeof(int)`. The third line is producing an error if `p` does not have a pointer type. I think I agree with you that this part could be refactored a bit. I would be tempted to put the "PSH" corresponding to the "i" next to when we parse the "i". I would also write the check that "p" has a pointer type before the code that indexes it. else if (tk == Brak) { next(); *++e = PSH; expr(Assign); *++e = PSH; if (tk == ']') next(); else { printf("%d: close bracket expected\n", line); exit(-1); } if (t < PTR) { printf("%d: pointer type expected\n", line); exit(-1); } *++e = IMM; *++e = sizeof(int); *++e = MUL; *++e = ADD; *++e = ((ty = t - PTR) == CHAR) ? LC : LI; }