diff options
author | kartofen <mladenovnasko0@gmail.com> | 2024-11-16 17:20:23 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2024-11-16 17:20:23 +0200 |
commit | 1945dbf8345a2b59e9057d21e38c78913272bdaa (patch) | |
tree | 6f908d5feb358d5deb37e3137684208408a52186 /src/main.c | |
parent | 0dd38d08551ac2fcff53eb604f6363f37b25aef9 (diff) |
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 68 |
1 files changed, 18 insertions, 50 deletions
@@ -50,34 +50,6 @@ } while(0) -#define NOT_IMPLEMENTED() die("Not Implemented. ABORTING") - -static void print_token(struct token *token) -{ - char buf[256] = {0}; - ERR_Z(token_value_string(token, LEN(buf), buf), return); - info("%-12s %s", token_type_string[token->type], buf); -} - -static void print_value(value_t value) -{ - char buf[256] = {0}; - value_string(value, sizeof(buf), buf); - info("%-12s %s", value ? - value_type_string[vvalue_type(value)] : "VALUE", buf); -} - -static void print_toklist(struct toklist *toklist) -{ - info("TOKLIST_START"); - 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++) - print_token(&entry->tokens[i]); - } - info("TOKLIST_END"); -} - struct tctx { enum tctx_type { TCTX_LEXER, @@ -313,7 +285,7 @@ static value_t apply_macro(env_t env, struct proc *proc, value_t *args) ret = evaluate_expr(env, &tctx); exit: - if(toklist) toklist_destroy(toklist); + if(toklist) toklist_dealloc(toklist); value_destroy(macro_ret); return ret; @@ -506,7 +478,7 @@ value_t evaluate_lambda(env_t env, struct tctx *tctx) fail: err("Procedure creation failed"); for(size_t i = 0; i < argc; i++) value_destroy(args[i]); - if(body) toklist_destroy(body); + if(body) toklist_dealloc(body); if(arg_keys) free(arg_keys); return VALUE_EMPTY; } @@ -539,7 +511,13 @@ value_t evaluate_define(env_t env, struct tctx *tctx) value_t prevval = VALUE_EMPTY; int flags = ENV_KV_FREE_KEY; - flags |= (vvalue_type(val) == VALUE_PROC) ? ENV_KV_CIRCULAR_REF : 0; + + if(vvalue_type(val) == VALUE_PROC) { + flags |= (env == global_env || + env_depend(env, vvalue_proc(val).parent_env)) + ? ENV_KV_CIRCULAR_REF : 0; + + } ERR_NZ( env_insert(env, key, val, &prevval, flags), @@ -759,19 +737,6 @@ exit: } -static struct toklist *toklist_create(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; -} - #define IS_OP(token) (token->type == TOKEN_QUOTE || \ token->type == TOKEN_QUASI || \ token->type == TOKEN_UNQUOTE) @@ -790,7 +755,7 @@ static int toklist_expr(struct tctx *tctx, struct toklist **toklist) if(tokens_len >= LEN(tokens)) { if(toklist) { - tail = list_add(tail, &toklist_create(tokens, tokens_len)->list); + tail = list_add(tail, &toklist_alloc(tokens, tokens_len)->list); } tokens_len = 0; } @@ -799,7 +764,7 @@ static int toklist_expr(struct tctx *tctx, struct toklist **toklist) else if(TOKEN(tctx)->type == TOKEN_RP) depth--; // printf("%zu\n", depth); - // print_token(TOKEN(tctx)); + // token_print(TOKEN(tctx)); if(toklist) token_clone(&tokens[tokens_len++], TOKEN(tctx)); @@ -807,7 +772,7 @@ static int toklist_expr(struct tctx *tctx, struct toklist **toklist) } while(depth != 0 || IS_OP(TOKEN(tctx))); if(toklist) { - tail = list_add(tail, &toklist_create(tokens, tokens_len)->list); + tail = list_add(tail, &toklist_alloc(tokens, tokens_len)->list); *toklist = list_entry(list_get_head(tail), struct toklist, list); } @@ -836,6 +801,9 @@ static struct toklist *value_to_toklist(value_t value) } else if(strcmp(vvalue_atom(value), "'") == 0) { SET_TOKEN_TYPE(&token, TOKEN_QUOTE); break; + } else if(strcmp(vvalue_atom(value), "define") == 0) { + SET_TOKEN_TYPE(&token, TOKEN_DEFINE); + break; } SET_TOKEN_TYPE(&token, TOKEN_ID); @@ -858,14 +826,14 @@ static struct toklist *value_to_toklist(value_t value) return NULL; } - return toklist_create(&token, 1); + return toklist_alloc(&token, 1); } static struct list_head *add_token_toklist(enum token_type type, struct list_head *tail) { struct token token = {0}; token.type = type; - return list_add(tail, &(toklist_create(&token, 1)->list)); + return list_add(tail, &(toklist_alloc(&token, 1)->list)); } static struct toklist *cons_to_toklist(value_t value) @@ -902,6 +870,6 @@ static struct toklist *cons_to_toklist(value_t value) return list_entry(list_get_head(tail), struct toklist, list); fail: err("Failed to turn value to toklist"); - toklist_destroy(list_entry(list_get_head(tail), struct toklist, list)); + toklist_dealloc(list_entry(list_get_head(tail), struct toklist, list)); return NULL; } |