diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/include/tree.h | 34 | ||||
-rw-r--r-- | src/lexer.c | 95 | ||||
-rw-r--r-- | src/tree.c | 19 |
5 files changed, 86 insertions, 65 deletions
@@ -3,7 +3,6 @@ sources = $(wildcard src/*.c) objects = $(sources:.c=.o) flags = -g - $(exec): $(objects) clang $(objects) $(flags) -o $(exec) @@ -35,7 +35,7 @@ Note that all syntax described is liable to sudden and violent change. let variable = 4; let another_variable = "Hello, World!\n"; -fn main { +fn main _ { while variable >= 0 { say another_variable; variable -= 1; diff --git a/src/include/tree.h b/src/include/tree.h index 9ba713b..34e8e4f 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -1,25 +1,41 @@ #ifndef TREE_H #define TREE_H + #include <stdlib.h> + typedef struct TREE_STRUC { + enum { TREE_VAR_DEF, - //TREE_FUN_DEF, TREE_VAR, - //TREE_FUN_CALL, - //TREE_STR, - //TREE_COMP + + TREE_FN_DEF, + TREE_FN_CALL, + + TREE_STR, + + TREE_SUBTREE, } type; - char* VAR_DEF_name; - struct TREE_STRUC* VAR_DEF_value; + 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; - char* VAR_name; +} tree_t; -} TREE_t; +tree_t* tree_init(int type); -TREE_t* tree_init(int type); #endif diff --git a/src/lexer.c b/src/lexer.c index db1d994..7ddabdf 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,11 +1,13 @@ #include "include/lexer.h" #include "include/token.h" + #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdio.h> + lexer_t* lexer_init(char* content) { lexer_t* lexer = calloc(1, sizeof(struct LEXER_STRUC)); @@ -46,49 +48,48 @@ token_t* lexer_get_next_token(lexer_t* lexer) { switch (lexer->c) { case '"': return lexer_get_string(lexer); break; case '=': return lexer_next_token( - lexer, - token_init( - TOKEN_EQ, - lexer_get_c_as_string(lexer) - ) - ); break; - case '/': return lexer_next_token( - lexer, - token_init( - TOKEN_LORD, - lexer_get_c_as_string(lexer) - ) - ); break; - case '\\': return lexer_next_token( - lexer, - token_init( - TOKEN_RORD, - lexer_get_c_as_string(lexer) - ) - ); break; + lexer, + token_init( + TOKEN_EQ, + lexer_get_c_as_string(lexer) + ) + ); break; + case '{': return lexer_next_token( + lexer, + token_init( + TOKEN_LORD, + lexer_get_c_as_string(lexer) + ) + ); break; + case '}': return lexer_next_token( + lexer, + token_init( + TOKEN_RORD, + lexer_get_c_as_string(lexer) + ) + ); break; case '&': return lexer_next_token( - lexer, - token_init( - TOKEN_AMP, - lexer_get_c_as_string(lexer) - ) - ); break; + lexer, + token_init( + TOKEN_AMP, + lexer_get_c_as_string(lexer) + ) + ); break; case '[': return lexer_get_comment(lexer); break; - case ']': lexer_next(lexer); break; case '#': return lexer_next_token( - lexer, - token_init( - TOKEN_POUND, - lexer_get_c_as_string(lexer) - ) - ); break; + lexer, + token_init( + TOKEN_POUND, + lexer_get_c_as_string(lexer) + ) + ); break; case '~': return lexer_next_token( - lexer, - token_init( - TOKEN_TILDE, - lexer_get_c_as_string(lexer) - ) - ); break; + lexer, + token_init( + TOKEN_TILDE, + lexer_get_c_as_string(lexer) + ) + ); break; case ';': return lexer_next_token( lexer, token_init( @@ -99,7 +100,6 @@ token_t* lexer_get_next_token(lexer_t* lexer) { } } - // return token_init(TOKEN_EOF, "\0"); return NULL; } @@ -110,25 +110,26 @@ token_t* lexer_get_string(lexer_t* lexer) { str_so_far[0] = '\0'; while (lexer->c != '"') { + // until reaching the closing ", add each character to str_so_far and adjust size to match. char* current = lexer_get_c_as_string(lexer); - str_so_far = realloc(str_so_far, (strlen(str_so_far) + strlen(current) * sizeof(char))); // give str so far some more memory - strcat(str_so_far, current); // add current to str so far + str_so_far = realloc(str_so_far, (strlen(str_so_far) + strlen(current) * sizeof(char))); + strcat(str_so_far, current); lexer_next(lexer); } - lexer_next(lexer); + lexer_next(lexer); // skip over closing " + return token_init(TOKEN_QUOTE, str_so_far); } token_t* lexer_get_comment(lexer_t* lexer) { - lexer_next(lexer); - while (lexer->c != ']') { lexer_next(lexer); } - lexer_next(lexer); + lexer_next(lexer); // skip over closing ] + return token_init(TOKEN_COMM, lexer_get_c_as_string(lexer)); } @@ -144,8 +145,6 @@ token_t* lexer_get_id(lexer_t* lexer) { lexer_next(lexer); } - lexer_next(lexer); - return token_init(TOKEN_ID, str_so_far); } @@ -1,15 +1,22 @@ #include "include/tree.h" -TREE_t* tree_init(int type) { - // give the tree just enough room - TREE_t* tree = calloc(1, sizeof(struct TREE_STRUC)); +tree_t* tree_init(int type) { + tree_t* tree = calloc(1, sizeof(struct TREE_STRUC)); tree->type = type; - tree->VAR_DEF_name = NULL; - tree->VAR_DEF_value = NULL; - tree->VAR_name = NULL; + 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; return tree; } |