struct lex_file *lex_open(FILE *fp) {
struct lex_file *lex = mem_a(sizeof(struct lex_file));
- if (lex) {
- lex->file = fp;
- fseek(lex->file, 0, SEEK_END);
- lex->length = ftell(lex->file);
- lex->size = lex->length; /* copy, this is never changed */
- fseek(lex->file, 0, SEEK_SET);
- lex->last = 0;
+ if (!lex || !fp)
+ return NULL;
- memset(lex->peek, 0, sizeof(lex->peek));
- }
+ lex->file = fp;
+ fseek(lex->file, 0, SEEK_END);
+ lex->length = ftell(lex->file);
+ lex->size = lex->length; /* copy, this is never changed */
+ fseek(lex->file, 0, SEEK_SET);
+ lex->last = 0;
+
+ memset(lex->peek, 0, sizeof(lex->peek));
return lex;
}
lex_addch(ch, file);
while ((ch = lex_getch(file)) != '*') {
if (ch == EOF)
- return error(ERROR_LEX, "malformatted comment at line %d", file->line);
+ return error(ERROR_LEX, "malformatted comment at line", "");
else
lex_addch(ch, file);
}
return error(ERROR_COMPILER, "Source file: %s not found\n", ifile);
} else {
struct lex_file *lex = lex_open(fp);
+ if (!lex) {
+ fclose(fp);
+ return 0;
+ }
parse_tree(lex); /* generate parse tree */
lex_close (lex); /* cleanup lexer */
}