aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2024-11-16 17:20:23 +0200
committerkartofen <mladenovnasko0@gmail.com>2024-11-16 17:20:23 +0200
commit1945dbf8345a2b59e9057d21e38c78913272bdaa (patch)
tree6f908d5feb358d5deb37e3137684208408a52186 /src/lexer.c
parent0dd38d08551ac2fcff53eb604f6363f37b25aef9 (diff)
cleanup and minor refactoringHEADmaster
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/lexer.c b/src/lexer.c
index cd83006..29e084c 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -133,11 +133,22 @@ 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)
+char *token_value_static_string(struct token *token)
+{
+ static char str[1024];
+
+ if(token_value_string(token, sizeof(str), str) > 0)
+ return str;
+ return NULL;
+}
+
+static char *str_alloc_copy(char *src)
+{
+ if(!src) return src;
+
+ size_t len = strlen(src) + 1;
+ return memcpy(malloc(len), src, len);
+}
void token_clone(struct token *dest, struct token *src)
{
@@ -145,10 +156,10 @@ void token_clone(struct token *dest, struct token *src)
switch(src->type) {
case TOKEN_ID:
- STR_ALLOC_COPY(dest->value.id, src->value.id);
+ dest->value.id = str_alloc_copy(src->value.id);
return;
case TOKEN_STR:
- STR_ALLOC_COPY(dest->value.str, src->value.str);
+ dest->value.str = str_alloc_copy(src->value.str);
return;
case TOKEN_INT:
dest->value.num = src->value.num;
@@ -170,7 +181,20 @@ void token_dealloc(struct token *token)
}
}
-void toklist_destroy(struct toklist *toklist)
+struct toklist *toklist_alloc(struct token *tokens, size_t tokens_len)
+{
+ struct toklist *toklist;
+ toklist = malloc(sizeof(*toklist));
+ LIST_EMPTY(&toklist->list);
+
+ toklist->tokens = calloc(tokens_len, sizeof(*tokens));
+ memcpy(toklist->tokens, tokens, tokens_len * sizeof(*tokens));
+ toklist->tokens_len = tokens_len;
+
+ return toklist;
+}
+
+void toklist_dealloc(struct toklist *toklist)
{
list_for_each_safe(head, &toklist->list) {
toklist = list_entry(head, struct toklist, list);
@@ -181,6 +205,25 @@ void toklist_destroy(struct toklist *toklist)
free(toklist);
}
}
+#ifdef DEBUG
+void token_print(struct token *token)
+{
+ printf("%-12s %s\n",
+ token_type_string[token->type],
+ token_value_static_string(token));
+}
+
+void toklist_print(struct toklist *toklist)
+{
+ printf("--- toklis start ---\n");
+ list_for_each(pos, &toklist->list) {
+ struct toklist *entry = list_entry(pos, struct toklist, list);
+ for(size_t i = 0; i < entry->tokens_len; i++)
+ token_print(&entry->tokens[i]);
+ }
+ printf("---- toklis end ----\n");
+}
+#endif
static int on_separator(lexer_t lexer, enum token_type type)
{