From 54f071ac7d47ef515a3f6a4db9e83f2f9aca3c8c Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 25 Aug 2024 12:30:48 +0300 Subject: lambda nearly done --- src/lexer.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/lexer.c') 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); -- cgit v1.2.3