3 ms·
This 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
by ufo 7y ago
This 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;
}