From 2569231ab9876d59a5e14261bae81dca4d24e4e3 Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Sat, 30 Sep 2023 16:59:13 -0400 Subject: use flag vars for preprocessor states --- src/include/lexer.h | 42 +++++++++++++++++++++++++++++------------- src/include/util.h | 18 +++++++++++++----- src/lexer.c | 30 +++++++++++++++++++++++------- src/main.c | 32 +++++++++++++++++--------------- src/util.c | 3 +++ 5 files changed, 85 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/include/lexer.h b/src/include/lexer.h index a65f119..28d0e6f 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -1,37 +1,53 @@ #ifndef LEXER_H #define LEXER_H - #include "token.h" - #define LEXER_VALID (lexer->c != '\0' && lexer->i < strlen(lexer->content)) +/* the lexer struct */ typedef struct LEXER_STRUC { - char c; // current character in content - unsigned int i; // index of c - char* content; // the code itself + /* current character in content */ + char c; + + /* index of c */ + unsigned int i; + + /* source being read */ + char* content; } lexer_t; -// constructor/destroyer +/* create lexer from source code */ extern lexer_t* lexer_init (char* content); + +/* destroy the lexer */ extern void lexer_destroy (lexer_t* lexer); -// advance the lexer +/* move lexer forward one char */ extern void lexer_next (lexer_t* lexer); + +/* skip useless characters */ extern void lexer_pass (lexer_t* lexer); -extern token_t* lexer_get_next_token (lexer_t* lexer); // chars -> tokens + +/* create tokens */ +extern token_t* lexer_get_next_token (lexer_t* lexer); + +/* create token and move 1 char */ extern token_t* lexer_next_token (lexer_t* lexer, int token_type); + +/* create string from lexer->c */ extern char* lexer_get_c_as_string (lexer_t* lexer); -// collectors -extern token_t* lexer_get_arr (lexer_t* lexer); +/* + int fskip: skip first char? + + int lskip: skip last char? +*/ extern token_t* lexer_collect (lexer_t* lexer, int (*end_char)(char), int fskip, int lskip, int type); -// special def collectors -extern token_t* lexer_get_directive (lexer_t* lexer); -extern token_t* lexer_get_keyword (lexer_t* lexer); +/* run lexer from source */ +lexer_t* lexer_run(lexer_t*); #endif diff --git a/src/include/util.h b/src/include/util.h index a7879e9..a2f887b 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -7,10 +7,18 @@ #include -void die(const char* fmt, ...); -void log_err(const char* fmt, ...); -void log_inf(const char* fmt, ...); -void log_raw(const char* fmt, ...); -void log_war(const char* fmt, ...); +/* die and leave message */ +void die(const char*, ...); +/* log an error */ +void log_err(const char*, ...); +/* log some information */ +void log_inf(const char*, ...); +/* log something with no formatting */ +void log_raw(const char*, ...); +/* log a warning */ +void log_war(const char*, ...); + +/* does c appear in s? */ +int appears_in_str(char*, char*); #endif diff --git a/src/lexer.c b/src/lexer.c index 09831bb..7a6aed5 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -104,21 +104,16 @@ token_t* lexer_next_token(lexer_t* lexer, int token_type) { return token; } -/* get the current character as a string */ char* lexer_get_c_as_string(lexer_t* lexer) { char* str; /* the string to return */ - str = calloc(2, sizeof(char)); + str = malloc(2); str[0] = lexer->c; str[1] = '\0'; return str; } -/* - fskip: skip first char? - lskip: skip last char? -*/ token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lskip, int type) { size_t len; /* length of collected token so far */ char* token; /* collected token so far */ @@ -146,7 +141,28 @@ token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lsk if (lskip) { lexer_next(lexer); } - token[len] = '\0'; /* null terminate */ + token[len] = '\0'; /* terminate */ return token_init(type, token); } + +lexer_t* lexer_run(lexer_t* lexer) { + while (1) { + token_t* token; + char* type; + + token = lexer_get_next_token(lexer); + type = token_get_type(token->type); + + log_inf("type: %s\t\tval:%s", type, token->value); + + if (token->type == TOKEN_EOF) { + token_destroy(token); + break; + } + + token_destroy(token); + } + + return lexer; +} diff --git a/src/main.c b/src/main.c index 7366932..c084002 100644 --- a/src/main.c +++ b/src/main.c @@ -10,37 +10,39 @@ int main(int argc, char* argv[]) { char* source; - /*lexer_t* lexer;*/ - /*int in_file;*/ + + lexer_t* lexer; pp_t* pp; source = source_get(argv[1]); + HLKT_ASS(source); + log_inf("source gotten"); + log_inf("source: %s", source); pp = pp_init(source); HLKT_ASS(pp); log_inf("preprocessor created"); + pp_run(pp); + free(source); + source = pp->psrc; + pp_destroy(pp); + HLKT_ASS(source); + log_inf("preprocessor ran"); + log_inf("preprocessed source: %s", source); + /* lexer = lexer_init(source); HLKT_ASS(lexer); log_inf("lexer created"); - log_inf("BEGIN INPUT"); - log_raw(lexer->content); - log_inf("END INPUT"); + lexer_run(lexer); - in_file = 1; + lexer_destroy(lexer); */ - - pp_run(pp); - - log_raw("%s\n%s", source, pp->psrc); - - // clean up - /*lexer_destroy(lexer);*/ - pp_destroy(pp); - free(source); + /*free(pp->psrc);*/ + HLKT_LOG(); diff --git a/src/util.c b/src/util.c index ba99bda..943a597 100644 --- a/src/util.c +++ b/src/util.c @@ -39,9 +39,12 @@ void log_inf(const char* fmt, ...) { void log_raw(const char* fmt, ...) { va_list ap; + va_start(ap, fmt); vfprintf(stdout, fmt, ap); va_end(ap); + + fprintf(stderr, "\n"); } void log_war(const char* fmt, ...) { -- cgit v1.2.3