Add line and block comments

This commit is contained in:
Rui Ueyama 2019-08-07 08:56:28 +09:00
parent a0388bada4
commit 6c0a42926a
2 changed files with 21 additions and 0 deletions

View File

@ -213,4 +213,8 @@ assert 1 'int main() { ({ 0; return 1; 2; }); return 3; }'
assert 6 'int main() { return ({ 1; }) + ({ 2; }) + ({ 3; }); }'
assert 3 'int main() { return ({ int x=3; x; }); }'
assert 2 'int main() { /* return 1; */ return 2; }'
assert 2 'int main() { // return 1;
return 2; }'
echo OK

View File

@ -230,6 +230,23 @@ static Token *tokenize(char *filename, char *p) {
Token *cur = &head;
while (*p) {
// Skip line comments.
if (startswith(p, "//")) {
p += 2;
while (*p != '\n')
p++;
continue;
}
// Skip block comments.
if (startswith(p, "/*")) {
char *q = strstr(p + 2, "*/");
if (!q)
error_at(p, "unclosed block comment");
p = q + 2;
continue;
}
// Skip whitespace characters.
if (isspace(*p)) {
p++;