From 266d3d6af1ba8d338bb8b9ad09431483276dae46 Mon Sep 17 00:00:00 2001 From: c+1 Date: Wed, 10 May 2023 00:50:10 -0400 Subject: halk. --- src/include/lexer.h | 22 +++++++++++++--------- src/include/token.h | 6 +++++- src/include/tree.h | 48 +++++++++++++++++++++++++++++++++++------------- src/lexer.c | 8 ++++++-- src/token.c | 6 +++++- src/tree.c | 37 +++++++++++++++++++++++++------------ 6 files changed, 89 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/include/lexer.h b/src/include/lexer.h index a0e40af..eb80646 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -1,28 +1,32 @@ #ifndef LEXER_H #define LEXER_H + + #include "token.h" + typedef struct LEXER_STRUC { char c; // current character in content unsigned int i; // index of c char* content; // the code itself } lexer_t; -lexer_t* lexer_init(char* content); +extern lexer_t* lexer_init(char* content); + +extern void lexer_next(lexer_t* lexer); +extern void lexer_pass(lexer_t* lexer); -void lexer_next(lexer_t* lexer); -void lexer_pass(lexer_t* lexer); +extern token_t* lexer_get_next_token(lexer_t* lexer); -token_t* lexer_get_next_token(lexer_t* lexer); +extern token_t* lexer_get_string(lexer_t* lexer); -token_t* lexer_get_string(lexer_t* lexer); +extern token_t* lexer_get_comment(lexer_t* lexer); -token_t* lexer_get_comment(lexer_t* lexer); +extern token_t* lexer_get_id(lexer_t* lexer); -token_t* lexer_get_id(lexer_t* lexer); +extern token_t* lexer_next_token(lexer_t* lexer, token_t* token); -token_t* lexer_next_token(lexer_t* lexer, token_t* token); +extern char* lexer_get_c_as_string(lexer_t* lexer); -char* lexer_get_c_as_string(lexer_t* lexer); #endif diff --git a/src/include/token.h b/src/include/token.h index 709051a..147862c 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -1,5 +1,7 @@ #ifndef TOKEN_H #define TOKEN_H + + typedef struct TOKEN_STRUC { enum { TOKEN_ID, // keyword @@ -21,5 +23,7 @@ typedef struct TOKEN_STRUC { char* value; } token_t; -token_t* token_init(int type, char* value); +extern token_t* token_init(int type, char* value); + + #endif diff --git a/src/include/tree.h b/src/include/tree.h index 34e8e4f..1cf029c 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -19,19 +19,41 @@ typedef struct TREE_STRUC { TREE_SUBTREE, } type; - char* var_def_name; - struct TREE_STRUC* var_def_val; - - char* var_name; - - char* fn_call_name; - struct TREE_STRUC** fn_call_argv; - size_t fn_call_argsize; - - char* str_val; - - struct TREE_STRUC* subtree_val; - size_t subtree_size; + union tree_union { + + struct var_def_struc { + char* name; + struct TREE_STRUC* val; + int is_const; + } var_def; + + struct var_struc { + char* name; + } var; + + struct fn_def_struc { + char* name; + struct TREE_STRUC** val; + struct TREE_STRUC** argv; + size_t argsize; + } fn_def; + + struct fn_call_struc { + char* name; + struct TREE_STRUC** argv; + size_t argsize; + } fn_call; + + struct str_struc { + char* val; + } str; + + struct subtree_struc { + struct TREE_STRUC* val; + size_t size; + } subtree; + + } data; } tree_t; diff --git a/src/lexer.c b/src/lexer.c index 7ddabdf..e23b078 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -75,7 +75,9 @@ token_t* lexer_get_next_token(lexer_t* lexer) { lexer_get_c_as_string(lexer) ) ); break; - case '[': return lexer_get_comment(lexer); break; + case '[': + return lexer_get_comment(lexer); + break; case '#': return lexer_next_token( lexer, token_init( @@ -97,6 +99,8 @@ token_t* lexer_get_next_token(lexer_t* lexer) { lexer_get_c_as_string(lexer) ) ); break; + default: + exit(1); } } @@ -155,7 +159,7 @@ token_t* lexer_next_token(lexer_t* lexer, token_t* token) { } char* lexer_get_c_as_string(lexer_t* lexer) { - char* str = calloc(2, sizeof(char)); + char* str = calloc(2, 1 * sizeof(char)); str[0] = lexer->c; str[1] = '\0'; diff --git a/src/token.c b/src/token.c index 3021a64..e57ecd5 100644 --- a/src/token.c +++ b/src/token.c @@ -1,6 +1,10 @@ -#include "include/token.h" #include + +#include "include/token.h" + + +// token constructor token_t* token_init(int type, char* val) { token_t* token = calloc(1, sizeof(struct TOKEN_STRUC)); token->type = type; diff --git a/src/tree.c b/src/tree.c index 2949c21..338ab24 100644 --- a/src/tree.c +++ b/src/tree.c @@ -5,18 +5,31 @@ tree_t* tree_init(int type) { tree->type = type; - char* var_def_name = NULL; - struct TREE_STRUC* var_def_val = NULL; - char* var_name = NULL; - - char* fn_call_name = NULL; - struct TREE_STRUC** fn_call_argv = NULL; - size_t fn_call_argsize = 0; - - char* str_val = NULL; - - struct TREE_STRUC* subtree_val = NULL; - size_t subtree_size = 0; + switch (type) { + case TREE_VAR_DEF: + tree->data.var_def.name = NULL; + tree->data.var_def.val = NULL; + break; + case TREE_VAR: + tree->data.var.name = NULL; + break; + case TREE_FN_DEF: + tree->data.fn_def.name = NULL; + tree->data.fn_def.val = NULL; + tree->data.fn_def.argv = NULL; + tree->data.fn_def.argsize = 0; + break; + case TREE_FN_CALL: + tree->data.fn_call.name = NULL; + tree->data.fn_call.argv = NULL; + tree->data.fn_call.argsize = 0; + break; + case TREE_STR: + tree->data.str.val = NULL; + tree->data.subtree.val = NULL; + tree->data.subtree.size = 0; + break; + } return tree; } -- cgit v1.2.3