From 3ca9bd11165cc86bc38334cac5a0b30fe0e946b9 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 17 Mar 2023 05:36:27 +0000 Subject: note string_ptr in token --- memory.c | 14 +++++++------- onlymain.golem | 1 + token.c | 9 +++++++-- token.h | 1 + 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/memory.c b/memory.c index b65ad42..2134ff8 100644 --- a/memory.c +++ b/memory.c @@ -16,17 +16,17 @@ strings_len(Token *list) } static int -strings_cat(char *mem, Token *list) +strings_cat(char *mem, Token *list, int off) { - int len = 0; for (Token *e = list; e != NULL; e = e->next) if (e->type == TOK_GROUP) - len += strings_cat(mem + len, e->group.tokens); + off = strings_cat(mem, e->group.tokens, off); else if (e->type == TOK_STRING) { - slice_cpy(mem + len, e->slice); - len += slice_len(e->slice) + 1; + slice_cpy(mem + off, e->slice); + e->string_ptr = off; + off += slice_len(e->slice) + 1; } - return len; + return off; } int @@ -38,7 +38,7 @@ memory_create(Memory *memory, Token *list) perror("memory_create:malloc"); return 1; } - const int cated = strings_cat(memory->strings, list); + const int cated = strings_cat(memory->strings, list, 0); if (cated != len) { fprintf(stderr, "strings_cat wrote %d bytes instead of %d", cated, len); diff --git a/onlymain.golem b/onlymain.golem index 43556bb..e34c1c0 100644 --- a/onlymain.golem +++ b/onlymain.golem @@ -1,4 +1,5 @@ main argc argv { (print [argv (sub argc 1)]); + (print "hello world"); return (less argc 2); } diff --git a/token.c b/token.c index 6ec9f40..dbe4125 100644 --- a/token.c +++ b/token.c @@ -14,6 +14,7 @@ token_create(Slice slice, unsigned int type) } token->slice = slice; token->type = type; + token->string_ptr = 0; token->prev = NULL; token->next = NULL; return token; @@ -120,8 +121,12 @@ token_print(Token *token, int recurse, int depth) token_print(token->group.scope, 1, depth); token_print(token->group.tokens, 1, depth + 1); } else { - printf("type: %c slice: ", token->type); - slice_print(token->slice); + printf("type: %c slice: \"", token->type); + slice_write(token->slice); + printf("\""); + if (token->type == TOK_STRING) + printf(" string_ptr: %ld", token->string_ptr); + printf("\n"); } if (!recurse) return tok; diff --git a/token.h b/token.h index fd378a5..f5b202d 100644 --- a/token.h +++ b/token.h @@ -27,6 +27,7 @@ enum { typedef struct Token Token; struct Token { unsigned char type; + unsigned long string_ptr; union { Slice slice; Group group; -- cgit v1.2.3