mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-01-19 06:40:49 +00:00
struct lex_file -> lex_file
This commit is contained in:
parent
89ed723efa
commit
33b1995fa5
6 changed files with 34 additions and 34 deletions
2
error.c
2
error.c
|
@ -57,7 +57,7 @@ static const char *const error_list[] = {
|
||||||
"Preprocessor Error:"
|
"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 bu[1024*4]; /* enough? */
|
||||||
char fu[1024*4]; /* enough? */
|
char fu[1024*4]; /* enough? */
|
||||||
va_list va;
|
va_list va;
|
||||||
|
|
20
gmqcc.h
20
gmqcc.h
|
@ -113,7 +113,7 @@ typedef char intptr_size_is_correct [sizeof(uintptr_t)== sizeof(int*)?1:-1];
|
||||||
//===================================================================
|
//===================================================================
|
||||||
//============================ lex.c ================================
|
//============================ lex.c ================================
|
||||||
//===================================================================
|
//===================================================================
|
||||||
struct lex_file {
|
typedef struct lex_file_t {
|
||||||
FILE *file; /* file handler */
|
FILE *file; /* file handler */
|
||||||
char *name; /* name of file */
|
char *name; /* name of file */
|
||||||
char peek [5];
|
char peek [5];
|
||||||
|
@ -124,7 +124,7 @@ struct lex_file {
|
||||||
int length; /* bytes left to parse */
|
int length; /* bytes left to parse */
|
||||||
int size; /* never changes (size of file) */
|
int size; /* never changes (size of file) */
|
||||||
int line; /* what line are we on? */
|
int line; /* what line are we on? */
|
||||||
};
|
} lex_file;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* It's important that this table never exceed 32 keywords, the ascii
|
* It's important that this table never exceed 32 keywords, the ascii
|
||||||
|
@ -164,11 +164,11 @@ enum {
|
||||||
LEX_IDENT
|
LEX_IDENT
|
||||||
};
|
};
|
||||||
|
|
||||||
int lex_token (struct lex_file *);
|
int lex_token (lex_file *);
|
||||||
void lex_reset (struct lex_file *);
|
void lex_reset (lex_file *);
|
||||||
void lex_close (struct lex_file *);
|
void lex_close (lex_file *);
|
||||||
struct lex_file *lex_include(struct lex_file *, char *);
|
lex_file *lex_include(lex_file *, char *);
|
||||||
struct lex_file *lex_open (FILE *);
|
lex_file *lex_open (FILE *);
|
||||||
|
|
||||||
//===================================================================
|
//===================================================================
|
||||||
//========================== error.c ================================
|
//========================== error.c ================================
|
||||||
|
@ -178,12 +178,12 @@ struct lex_file *lex_open (FILE *);
|
||||||
#define ERROR_INTERNAL (SHRT_MAX+2)
|
#define ERROR_INTERNAL (SHRT_MAX+2)
|
||||||
#define ERROR_COMPILER (SHRT_MAX+3)
|
#define ERROR_COMPILER (SHRT_MAX+3)
|
||||||
#define ERROR_PREPRO (SHRT_MAX+4)
|
#define ERROR_PREPRO (SHRT_MAX+4)
|
||||||
int error(struct lex_file *, int, const char *, ...);
|
int error(lex_file *, int, const char *, ...);
|
||||||
|
|
||||||
//===================================================================
|
//===================================================================
|
||||||
//========================== parse.c ================================
|
//========================== parse.c ================================
|
||||||
//===================================================================
|
//===================================================================
|
||||||
int parse_gen(struct lex_file *);
|
int parse_gen(lex_file *);
|
||||||
|
|
||||||
//===================================================================
|
//===================================================================
|
||||||
//========================== typedef.c ==============================
|
//========================== typedef.c ==============================
|
||||||
|
@ -195,7 +195,7 @@ typedef struct typedef_node_t {
|
||||||
void typedef_init();
|
void typedef_init();
|
||||||
void typedef_clear();
|
void typedef_clear();
|
||||||
typedef_node *typedef_find(const char *);
|
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 *);
|
||||||
|
|
||||||
|
|
||||||
//===================================================================
|
//===================================================================
|
||||||
|
|
36
lex.c
36
lex.c
|
@ -32,8 +32,8 @@ static const char *const lex_keywords[] = {
|
||||||
"for", "typedef"
|
"for", "typedef"
|
||||||
};
|
};
|
||||||
|
|
||||||
struct lex_file *lex_open(FILE *fp) {
|
lex_file *lex_open(FILE *fp) {
|
||||||
struct lex_file *lex = mem_a(sizeof(struct lex_file));
|
lex_file *lex = mem_a(sizeof(lex_file));
|
||||||
if (!lex || !fp)
|
if (!lex || !fp)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -49,20 +49,20 @@ struct lex_file *lex_open(FILE *fp) {
|
||||||
return lex;
|
return lex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lex_close(struct lex_file *file) {
|
void lex_close(lex_file *file) {
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
|
|
||||||
fclose(file->file); /* may already be closed */
|
fclose(file->file); /* may already be closed */
|
||||||
mem_d (file);
|
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)
|
if (file->current < sizeof(file->lastok)-1)
|
||||||
file->lastok[file->current++] = (char)ch;
|
file->lastok[file->current++] = (char)ch;
|
||||||
if (file->current == sizeof(file->lastok)-1)
|
if (file->current == sizeof(file->lastok)-1)
|
||||||
file->lastok[file->current] = (char)'\0';
|
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;
|
file->current = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,13 +71,13 @@ static inline void lex_clear(struct lex_file *file) {
|
||||||
* This doesn't play with file streams, the lexer has
|
* This doesn't play with file streams, the lexer has
|
||||||
* it's own internal state for this.
|
* it's own internal state for this.
|
||||||
*/
|
*/
|
||||||
static int lex_inget(struct lex_file *file) {
|
static int lex_inget(lex_file *file) {
|
||||||
file->length --;
|
file->length --;
|
||||||
if (file->last > 0)
|
if (file->last > 0)
|
||||||
return file->peek[--file->last];
|
return file->peek[--file->last];
|
||||||
return fgetc(file->file);
|
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))
|
if (file->last < sizeof(file->peek))
|
||||||
file->peek[file->last++] = ch;
|
file->peek[file->last++] = ch;
|
||||||
file->length ++;
|
file->length ++;
|
||||||
|
@ -87,7 +87,7 @@ static void lex_unget(int ch, struct lex_file *file) {
|
||||||
* This is trigraph and digraph support, a feature not qc compiler
|
* This is trigraph and digraph support, a feature not qc compiler
|
||||||
* supports. Moving up in this world!
|
* supports. Moving up in this world!
|
||||||
*/
|
*/
|
||||||
static int lex_trigraph(struct lex_file *file) {
|
static int lex_trigraph(lex_file *file) {
|
||||||
int ch;
|
int ch;
|
||||||
if ((ch = lex_inget(file)) != '?') {
|
if ((ch = lex_inget(file)) != '?') {
|
||||||
lex_unget(ch, file);
|
lex_unget(ch, file);
|
||||||
|
@ -112,7 +112,7 @@ static int lex_trigraph(struct lex_file *file) {
|
||||||
}
|
}
|
||||||
return '?';
|
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);
|
int ch = lex_inget(file);
|
||||||
switch (first) {
|
switch (first) {
|
||||||
case '<':
|
case '<':
|
||||||
|
@ -132,7 +132,7 @@ static int lex_digraph(struct lex_file *file, int first) {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lex_getch(struct lex_file *file) {
|
static int lex_getch(lex_file *file) {
|
||||||
int ch = lex_inget(file);
|
int ch = lex_inget(file);
|
||||||
|
|
||||||
static int str = 0;
|
static int str = 0;
|
||||||
|
@ -153,7 +153,7 @@ static int lex_getch(struct lex_file *file) {
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lex_get(struct lex_file *file) {
|
static int lex_get(lex_file *file) {
|
||||||
int ch;
|
int ch;
|
||||||
if (!isspace(ch = lex_getch(file)))
|
if (!isspace(ch = lex_getch(file)))
|
||||||
return ch;
|
return ch;
|
||||||
|
@ -168,7 +168,7 @@ static int lex_get(struct lex_file *file) {
|
||||||
return ' ';
|
return ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lex_skipchr(struct lex_file *file) {
|
static int lex_skipchr(lex_file *file) {
|
||||||
int ch;
|
int ch;
|
||||||
int it;
|
int it;
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ static int lex_skipchr(struct lex_file *file) {
|
||||||
return LEX_CHRLIT;
|
return LEX_CHRLIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lex_skipstr(struct lex_file *file) {
|
static int lex_skipstr(lex_file *file) {
|
||||||
int ch;
|
int ch;
|
||||||
lex_clear(file);
|
lex_clear(file);
|
||||||
lex_addch('"', file);
|
lex_addch('"', file);
|
||||||
|
@ -211,7 +211,7 @@ static int lex_skipstr(struct lex_file *file) {
|
||||||
|
|
||||||
return LEX_STRLIT;
|
return LEX_STRLIT;
|
||||||
}
|
}
|
||||||
static int lex_skipcmt(struct lex_file *file) {
|
static int lex_skipcmt(lex_file *file) {
|
||||||
int ch;
|
int ch;
|
||||||
lex_clear(file);
|
lex_clear(file);
|
||||||
ch = lex_getch(file);
|
ch = lex_getch(file);
|
||||||
|
@ -257,7 +257,7 @@ static int lex_skipcmt(struct lex_file *file) {
|
||||||
return LEX_COMMENT;
|
return LEX_COMMENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lex_getsource(struct lex_file *file) {
|
static int lex_getsource(lex_file *file) {
|
||||||
int ch = lex_get(file);
|
int ch = lex_get(file);
|
||||||
|
|
||||||
/* skip char/string/comment */
|
/* skip char/string/comment */
|
||||||
|
@ -270,7 +270,7 @@ static int lex_getsource(struct lex_file *file) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int lex_token(struct lex_file *file) {
|
int lex_token(lex_file *file) {
|
||||||
int ch = lex_getsource(file);
|
int ch = lex_getsource(file);
|
||||||
int it;
|
int it;
|
||||||
|
|
||||||
|
@ -323,7 +323,7 @@ int lex_token(struct lex_file *file) {
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lex_reset(struct lex_file *file) {
|
void lex_reset(lex_file *file) {
|
||||||
file->current = 0;
|
file->current = 0;
|
||||||
file->last = 0;
|
file->last = 0;
|
||||||
file->length = file->size;
|
file->length = file->size;
|
||||||
|
@ -338,7 +338,7 @@ void lex_reset(struct lex_file *file) {
|
||||||
* should check if names are the same to prevent endless include
|
* should check if names are the same to prevent endless include
|
||||||
* recrusion.
|
* recrusion.
|
||||||
*/
|
*/
|
||||||
struct lex_file *lex_include(struct lex_file *lex, char *file) {
|
lex_file *lex_include(lex_file *lex, char *file) {
|
||||||
util_strrq(file);
|
util_strrq(file);
|
||||||
if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
|
if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
|
||||||
error(lex, ERROR_LEX, "Source file cannot include itself\n");
|
error(lex, ERROR_LEX, "Source file cannot include itself\n");
|
||||||
|
|
2
main.c
2
main.c
|
@ -138,7 +138,7 @@ int main(int argc, char **argv) {
|
||||||
switch (items_data[itr].type) {
|
switch (items_data[itr].type) {
|
||||||
case 0:
|
case 0:
|
||||||
fpp = fopen(items_data[itr].name, "r");
|
fpp = fopen(items_data[itr].name, "r");
|
||||||
struct lex_file *lex = lex_open(fpp);
|
lex_file *lex = lex_open(fpp);
|
||||||
parse_gen(lex);
|
parse_gen(lex);
|
||||||
lex_close(lex);
|
lex_close(lex);
|
||||||
break;
|
break;
|
||||||
|
|
6
parse.c
6
parse.c
|
@ -48,7 +48,7 @@ void compile_constant_debug() {
|
||||||
* Generates a parse tree out of the lexees generated by the lexer. This
|
* 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.
|
* 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;
|
int token = 0;
|
||||||
while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
|
while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) {
|
||||||
switch (token) {
|
switch (token) {
|
||||||
|
@ -262,8 +262,8 @@ int parse_gen(struct lex_file *file) {
|
||||||
if (token == '\n')
|
if (token == '\n')
|
||||||
return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
|
return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
|
||||||
|
|
||||||
char *copy = util_strdup(file->lastok);
|
char *copy = util_strdup(file->lastok);
|
||||||
struct lex_file *next = lex_include(file, copy);
|
lex_file *next = lex_include(file, copy);
|
||||||
|
|
||||||
if (!next) {
|
if (!next) {
|
||||||
error(file, ERROR_INTERNAL, "Include subsystem failure\n");
|
error(file, ERROR_INTERNAL, "Include subsystem failure\n");
|
||||||
|
|
|
@ -49,7 +49,7 @@ void typedef_clear() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
unsigned int hash = typedef_hash(to);
|
||||||
typedef_node *find = typedef_table[hash];
|
typedef_node *find = typedef_table[hash];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue