Skip to content

Commit

Permalink
Print an error when a syntax error occurs.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Apr 16, 2012
1 parent 966e5ea commit f3b36c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
20 changes: 14 additions & 6 deletions compiler.c
Expand Up @@ -143,14 +143,13 @@ int main (int argc, char *argv[])

char *source_file = argv[2];

int row = 0, column = 0;
unsigned char comment = 0;

FILE *file = fopen(source_file, "r");
if (file != NULL) {
unsigned char comment = 0;
while (1) {
char c = fgetc(file);
if (c == EOF) {
break;
}
char c;
while ((c = fgetc(file)) != EOF) {
switch (c) {
// This is where the language translation magic happens.
case ';':
Expand All @@ -162,6 +161,7 @@ int main (int argc, char *argv[])
// Without semicolon translation.
}
}
column++;
break;

// Pass-through new-lines, unless in a comment.
Expand All @@ -170,12 +170,20 @@ int main (int argc, char *argv[])
putc('\n', stdout);
}
comment = 0;
row++;
column = 0;
break;

// Comment, ignore the rest of the line.
case '#':
comment = 1;
break;

default:
if (!comment) {
fprintf(stderr, "Syntax error at row `%d' column `%d'.\n", row, column);
return 1;
}
}
}
fclose(file);
Expand Down
24 changes: 16 additions & 8 deletions compiler.gperf
Expand Up @@ -18,7 +18,7 @@
int main (int argc, char *argv[])
{
if (argc != 3) {
printf("Usage: %s [-ada|-algol|-c|-cpp|-cs|-java|-js|-pascal|-perl|-php|-ruby] FILE\n", argv[0]);
fprintf(stderr, "Usage: %s [-ada|-algol|-c|-cpp|-cs|-java|-js|-pascal|-perl|-php|-ruby] FILE\n", argv[0]);
return 1;
}

Expand All @@ -29,14 +29,13 @@ int main (int argc, char *argv[])

char *source_file = argv[2];

int row = 0, column = 0;
unsigned char comment = 0;

FILE *file = fopen(source_file, "r");
if (file != NULL) {
unsigned char comment = 0;
while (1) {
char c = fgetc(file);
if (c == EOF) {
break;
}
char c;
while ((c = fgetc(file)) != EOF) {
switch (c) {
// This is where the language translation magic happens.
case ';':
Expand All @@ -48,6 +47,7 @@ int main (int argc, char *argv[])
// Without semicolon translation.
}
}
column++;
break;

// Pass-through new-lines, unless in a comment.
Expand All @@ -56,17 +56,25 @@ int main (int argc, char *argv[])
putc('\n', stdout);
}
comment = 0;
row++;
column = 0;
break;

// Comment, ignore the rest of the line.
case '#':
comment = 1;
break;

default:
if (!comment) {
fprintf(stderr, "Syntax error at row `%d' column `%d'.\n", row, column);
return 1;
}
}
}
fclose(file);
} else {
printf("Unable to read semiscript source file `%s'.\n", source_file);
fprintf(stderr, "Unable to read semiscript source file `%s'.\n", source_file);
return 1;
}

Expand Down

0 comments on commit f3b36c3

Please sign in to comment.