diff options
author | c+1 | 2023-10-02 13:13:23 -0400 |
---|---|---|
committer | c+1 | 2023-10-02 13:13:23 -0400 |
commit | dee72dfcc0124be81e338a1d85948b8f1250085d (patch) | |
tree | e013ac2a9410cff9ef55ce10531505af72edf67f | |
parent | 1e62e5f079bdd61c37f2f510a77279345680ccbf (diff) |
emalloc(), ecalloc(), erealloc()
-rw-r--r-- | src/include/lexer.h | 3 | ||||
-rw-r--r-- | src/include/token.h | 2 | ||||
-rw-r--r-- | src/include/util.h | 8 | ||||
-rw-r--r-- | src/lexer.c | 40 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/util.c | 24 |
6 files changed, 52 insertions, 27 deletions
diff --git a/src/include/lexer.h b/src/include/lexer.h index 28d0e6f..8d4f75f 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -1,11 +1,12 @@ #ifndef LEXER_H #define LEXER_H +#include "util.h" +#include "syntax.h" #include "token.h" #define LEXER_VALID (lexer->c != '\0' && lexer->i < strlen(lexer->content)) - /* the lexer struct */ typedef struct LEXER_STRUC { /* current character in content */ diff --git a/src/include/token.h b/src/include/token.h index d3dda67..1a307cd 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -13,7 +13,7 @@ typedef struct TOKEN_STRUC { enum TOKEN_ENUM { TOKEN_KEYWORD, // keyword - TOKEN_PRIM_STR, // 'string' + TOKEN_PRIM_STR, // "string" TOKEN_PRIM_INT, // 42 TOKEN_COMM, // `comment` TOKEN_STMNT_END, // ; diff --git a/src/include/util.h b/src/include/util.h index a2f887b..712af43 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -18,7 +18,11 @@ void log_raw(const char*, ...); /* log a warning */ void log_war(const char*, ...); -/* does c appear in s? */ -int appears_in_str(char*, char*); +/* if calloc() returns null, die */ +void* ecalloc(size_t, size_t); +/* if malloc() returns null, die */ +void* emalloc(size_t); +/* if realloc() returns null, die */ +void* erealloc(void*, size_t); #endif diff --git a/src/lexer.c b/src/lexer.c index 7a6aed5..8374a90 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -3,10 +3,8 @@ #include <ctype.h> #include <stdio.h> -#include "include/util.h" #include "include/lexer.h" - lexer_t* lexer_init(char* content) { lexer_t* lexer = calloc(1, sizeof(struct LEXER_STRUC)); @@ -23,7 +21,7 @@ void lexer_destroy(lexer_t* lexer) { void lexer_next(lexer_t* lexer) { if (LEXER_VALID) { - lexer->i += 1; + lexer->i ++; lexer->c = lexer->content[lexer->i]; } } @@ -42,56 +40,52 @@ token_t* lexer_get_next_token(lexer_t* lexer) { if (char_could_start_keyword(&lexer->c)) { return lexer_collect(lexer, token_char_kywrd, 0, 0, TOKEN_KEYWORD); } switch (lexer->c) { - case '\'': + case SYNTAX_STR_DELIM: return lexer_collect(lexer, token_char_quote, 1, 1, TOKEN_PRIM_STR); break; - case '`': - return lexer_collect(lexer, token_char_grave, 1, 1, TOKEN_COMM); - break; - case ';': + case SYNTAX_EXPR_END: return lexer_next_token(lexer, TOKEN_STMNT_END); break; - case '=': + case SYNTAX_SET: return lexer_next_token(lexer, TOKEN_DEF_SET); break; - case '(': + case SYNTAX_LGROUP: return lexer_next_token(lexer, TOKEN_LGROUP); break; - case ')': + case SYNTAX_RGROUP: return lexer_next_token(lexer, TOKEN_RGROUP); break; - case '#': - return lexer_collect(lexer, token_char_pound, 1, 1, TOKEN_DIRECTIVE); - break; - case '.': + case SYNTAX_APPLY: return lexer_next_token(lexer, TOKEN_FN_APPLY); break; - case ',': + case SYNTAX_LIST_DELIM: return lexer_next_token(lexer, TOKEN_LIST_DELIM); break; - case ':': + case SYNTAX_TAG_DELIM: return lexer_collect(lexer, token_char_kywrd, 1, 0, TOKEN_DEF_TAG); break; - case '/': + case SYNTAX_NAMESPACE_DELIM: return lexer_next_token(lexer, TOKEN_NAMESPACE_DELIM); break; - case '{': + case SYNTAX_LBLOCK: return lexer_next_token(lexer, TOKEN_BLOCK_START); break; - case '}': + case SYNTAX_RBLOCK: return lexer_next_token(lexer, TOKEN_BLOCK_END); break; - case '[': + case SYNTAX_LLIST: return lexer_next_token(lexer, TOKEN_ARRAY_START); break; - case ']': + case SYNTAX_RLIST: return lexer_next_token(lexer, TOKEN_ARRAY_END); break; case '\0': + case EOF: return token_init(TOKEN_EOF, lexer_get_c_as_string(lexer)); break; default: - return token_init(TOKEN_UNKNOWN, lexer_get_c_as_string(lexer)); + return lexer_next_token(lexer, TOKEN_UNKNOWN); + break; } } @@ -31,6 +31,7 @@ int main(int argc, char* argv[]) { log_inf("preprocessor ran"); log_inf("preprocessed source: %s", source); + /* lexer = lexer_init(source); HLKT_ASS(lexer); log_inf("lexer created"); @@ -38,6 +39,7 @@ int main(int argc, char* argv[]) { lexer_run(lexer); lexer_destroy(lexer); + */ free(source); /*free(pp->psrc);*/ @@ -58,3 +58,27 @@ void log_war(const char* fmt, ...) { fprintf(stderr, "\n"); } + +void* ecalloc(size_t nmemb, size_t size) { + void* p; + + if (! (p = calloc(nmemb, size))) { die("failed to calloc:"); } + + return p; +} + +void* emalloc(size_t size) { + void* p; + + if (! (p = malloc(size))) { die("failed to malloc:"); } + + return p; +} + +void* erealloc(void* dest, size_t size) { + void* p; + + if (! (p = realloc(dest, size))) { die("failed to realloc:"); } + + return p; +} |