diff options
author | kartofen <mladenovnasko0@gmail.com> | 2024-08-25 12:30:48 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2024-08-25 12:30:48 +0300 |
commit | 54f071ac7d47ef515a3f6a4db9e83f2f9aca3c8c (patch) | |
tree | 63e9958c5961f32d191c580f6960b6f9eba02f20 /src/lexer.c | |
parent | df95e5bfca1c5e723b39f25f32401db8f9ebf6fe (diff) |
lambda nearly done
Diffstat (limited to 'src/lexer.c')
-rw-r--r-- | src/lexer.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lexer.c b/src/lexer.c index 407be25..b8897da 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -120,6 +120,43 @@ int token_value_string(struct token *token, size_t buf_sz, char *buf) return 0; } +#define STR_ALLOC_COPY(dest, str) do { \ + size_t len = strlen(str) + 1; \ + dest = malloc(len); \ + memcpy((dest), (str), len); \ + } while(0) + +void token_clone(struct token *dest, struct token *src) +{ + dest->type = src->type; + + switch(src->type) { + case TOKEN_ID: + STR_ALLOC_COPY(dest->value.id, src->value.id); + return; + case TOKEN_STR: + STR_ALLOC_COPY(dest->value.str, src->value.str); + return; + case TOKEN_INT: + dest->value.num = src->value.num; + return; + default: return; + } +} + +void token_dealloc(struct token *token) +{ + switch(token->type) { + case TOKEN_ID: + free(token->value.id); + return; + case TOKEN_STR: + free(token->value.str); + return; + default: return; + } +} + static int on_separator(lexer_t lexer, enum token_type type) { if(lexer->acc_idx > 0) return acc_empty(lexer); |