"Preprocessor Error:"
};
-int error(struct lex_file *file, int status, const char *msg, ...) {
+int error(lex_file *file, int status, const char *msg, ...) {
char bu[1024*4]; /* enough? */
char fu[1024*4]; /* enough? */
va_list va;
//===================================================================
//============================ lex.c ================================
//===================================================================
-struct lex_file {
+typedef struct lex_file_t {
FILE *file; /* file handler */
char *name; /* name of file */
char peek [5];
int length; /* bytes left to parse */
int size; /* never changes (size of file) */
int line; /* what line are we on? */
-};
+} lex_file;
/*
* It's important that this table never exceed 32 keywords, the ascii
LEX_IDENT
};
-int lex_token (struct lex_file *);
-void lex_reset (struct lex_file *);
-void lex_close (struct lex_file *);
-struct lex_file *lex_include(struct lex_file *, char *);
-struct lex_file *lex_open (FILE *);
+int lex_token (lex_file *);
+void lex_reset (lex_file *);
+void lex_close (lex_file *);
+lex_file *lex_include(lex_file *, char *);
+lex_file *lex_open (FILE *);
//===================================================================
//========================== error.c ================================
#define ERROR_INTERNAL (SHRT_MAX+2)
#define ERROR_COMPILER (SHRT_MAX+3)
#define ERROR_PREPRO (SHRT_MAX+4)
-int error(struct lex_file *, int, const char *, ...);
+int error(lex_file *, int, const char *, ...);
//===================================================================
//========================== parse.c ================================
//===================================================================
-int parse_gen(struct lex_file *);
+int parse_gen(lex_file *);
//===================================================================
//========================== typedef.c ==============================
void typedef_init();
void typedef_clear();
typedef_node *typedef_find(const char *);
-int typedef_add (struct lex_file *file, const char *, const char *);
+int typedef_add (lex_file *file, const char *, const char *);
//===================================================================
"for", "typedef"
};
-struct lex_file *lex_open(FILE *fp) {
- struct lex_file *lex = mem_a(sizeof(struct lex_file));
+lex_file *lex_open(FILE *fp) {
+ lex_file *lex = mem_a(sizeof(lex_file));
if (!lex || !fp)
return NULL;
return lex;
}
-void lex_close(struct lex_file *file) {
+void lex_close(lex_file *file) {
if (!file) return;
fclose(file->file); /* may already be closed */
mem_d (file);
}
-static void lex_addch(int ch, struct lex_file *file) {
+static void lex_addch(int ch, lex_file *file) {
if (file->current < sizeof(file->lastok)-1)
file->lastok[file->current++] = (char)ch;
if (file->current == sizeof(file->lastok)-1)
file->lastok[file->current] = (char)'\0';
}
-static inline void lex_clear(struct lex_file *file) {
+static inline void lex_clear(lex_file *file) {
file->current = 0;
}
* This doesn't play with file streams, the lexer has
* it's own internal state for this.
*/
-static int lex_inget(struct lex_file *file) {
+static int lex_inget(lex_file *file) {
file->length --;
if (file->last > 0)
return file->peek[--file->last];
return fgetc(file->file);
}
-static void lex_unget(int ch, struct lex_file *file) {
+static void lex_unget(int ch, lex_file *file) {
if (file->last < sizeof(file->peek))
file->peek[file->last++] = ch;
file->length ++;
* This is trigraph and digraph support, a feature not qc compiler
* supports. Moving up in this world!
*/
-static int lex_trigraph(struct lex_file *file) {
+static int lex_trigraph(lex_file *file) {
int ch;
if ((ch = lex_inget(file)) != '?') {
lex_unget(ch, file);
}
return '?';
}
-static int lex_digraph(struct lex_file *file, int first) {
+static int lex_digraph(lex_file *file, int first) {
int ch = lex_inget(file);
switch (first) {
case '<':
return first;
}
-static int lex_getch(struct lex_file *file) {
+static int lex_getch(lex_file *file) {
int ch = lex_inget(file);
static int str = 0;
return ch;
}
-static int lex_get(struct lex_file *file) {
+static int lex_get(lex_file *file) {
int ch;
if (!isspace(ch = lex_getch(file)))
return ch;
return ' ';
}
-static int lex_skipchr(struct lex_file *file) {
+static int lex_skipchr(lex_file *file) {
int ch;
int it;
return LEX_CHRLIT;
}
-static int lex_skipstr(struct lex_file *file) {
+static int lex_skipstr(lex_file *file) {
int ch;
lex_clear(file);
lex_addch('"', file);
return LEX_STRLIT;
}
-static int lex_skipcmt(struct lex_file *file) {
+static int lex_skipcmt(lex_file *file) {
int ch;
lex_clear(file);
ch = lex_getch(file);
return LEX_COMMENT;
}
-static int lex_getsource(struct lex_file *file) {
+static int lex_getsource(lex_file *file) {
int ch = lex_get(file);
/* skip char/string/comment */
}
}
-int lex_token(struct lex_file *file) {
+int lex_token(lex_file *file) {
int ch = lex_getsource(file);
int it;
return ch;
}
-void lex_reset(struct lex_file *file) {
+void lex_reset(lex_file *file) {
file->current = 0;
file->last = 0;
file->length = file->size;
* should check if names are the same to prevent endless include
* recrusion.
*/
-struct lex_file *lex_include(struct lex_file *lex, char *file) {
+lex_file *lex_include(lex_file *lex, char *file) {
util_strrq(file);
if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
error(lex, ERROR_LEX, "Source file cannot include itself\n");
switch (items_data[itr].type) {
case 0:
fpp = fopen(items_data[itr].name, "r");
- struct lex_file *lex = lex_open(fpp);
+ lex_file *lex = lex_open(fpp);
parse_gen(lex);
lex_close(lex);
break;
* Generates a parse tree out of the lexees generated by the lexer. This
* is where the tree is built. This is where valid check is performed.
*/
-int parse_gen(struct lex_file *file) {
+int parse_gen(lex_file *file) {
int token = 0;
while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
switch (token) {
if (token == '\n')
return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
- char *copy = util_strdup(file->lastok);
- struct lex_file *next = lex_include(file, copy);
+ char *copy = util_strdup(file->lastok);
+ lex_file *next = lex_include(file, copy);
if (!next) {
error(file, ERROR_INTERNAL, "Include subsystem failure\n");
}
}
-int typedef_add(struct lex_file *file, const char *from, const char *to) {
+int typedef_add(lex_file *file, const char *from, const char *to) {
unsigned int hash = typedef_hash(to);
typedef_node *find = typedef_table[hash];