From f60047d4b013eb7f75ad4f5c63eda63153a4bf8e Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 8 Sep 2024 20:02:06 +0300 Subject: value is now opaque --- src/main.c | 58 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 8baaa94..9c59c56 100644 --- a/src/main.c +++ b/src/main.c @@ -65,7 +65,7 @@ static void print_value(value_t value) char buf[256] = {0}; value_string(value, sizeof(buf), buf); info("%-12s %s", value ? - value_type_string[value->type] : "VALUE", buf); + value_type_string[vvalue_type(value)] : "VALUE", buf); } static void print_toklist(struct toklist *toklist) @@ -239,7 +239,7 @@ int main(int argc, char **argv) if(val == VALUE_EMPTY) { if(!repl_flag) { - printf("%s:%zu: FAILED\n", filename, lexer->line); + printf("%s:%zu: FAILED\n", filename, lexer_get_line(lexer)); break; } else { printf("=> FAILED\n"); @@ -250,7 +250,7 @@ int main(int argc, char **argv) value_string(val, sizeof(buf), buf); if(!repl_flag) { - printf("%s:%zu: %s\n", filename, lexer->line, buf); + printf("%s:%zu: %s\n", filename, lexer_get_line(lexer), buf); } else { printf("=> %s\n", buf); } @@ -277,9 +277,9 @@ static char *str_alloc_copy(char *src) } #define HAS_ENOUGH_ARGS(proc, type, argc, fail) \ - if(argc != proc->value.type.argc) { \ + if(argc != vvalue_##type(proc).argc) { \ err("Wrong number of arguemnts, expected %zu, but got %zu", \ - proc->value.type.argc, argc); \ + vvalue_##type(proc).argc, argc); \ fail; \ } @@ -333,18 +333,18 @@ value_t apply(env_t env, value_t proc, size_t argc, value_t *argv) { if(proc == VALUE_EMPTY) return value_copy(global_nil); - switch(proc->type) { + switch(vvalue_type(proc)) { case VALUE_PROC: HAS_ENOUGH_ARGS(proc, proc, argc, return VALUE_EMPTY); - return apply_lambda(&proc->value.proc, argv); + return apply_lambda(&vvalue_proc(proc), argv); case VALUE_MACRO: HAS_ENOUGH_ARGS(proc, proc, argc, return VALUE_EMPTY); - return apply_macro(env, &proc->value.proc, argv); + return apply_macro(env, &vvalue_proc(proc), argv); case VALUE_PROC_BUILTIN: HAS_ENOUGH_ARGS(proc, proc_builtin, argc, return VALUE_EMPTY); - return proc->value.proc_builtin.proc(argv); + return vvalue_proc_builtin(proc).proc(argv); default: - err("'%s' is not a procedure", value_type_string[proc->type]); + err("'%s' is not a procedure", value_type_string[vvalue_type(proc)]); return VALUE_EMPTY; } } @@ -591,11 +591,11 @@ value_t evaluate_if(env_t env, struct tctx *tctx) // goto exit; // } - ERR_Z(cond->type == VALUE_INT, + ERR_Z(vvalue_type(cond) == VALUE_INT, err("expected condition to evaluate to VALUE_INT"); goto exit); - if(cond->value.num) { + if(vvalue_num(cond)) { TOKEN_NEXT(tctx); ret = evaluate_expr(env, tctx); @@ -635,7 +635,7 @@ value_t evaluate_defmacro(env_t env, struct tctx *tctx) tctx->token->type = TOKEN_LAMBDA; ERR_Z(lambda = evaluate_lambda(env, tctx), goto fail); - lambda->type = VALUE_MACRO; + value_set_type(lambda, VALUE_MACRO); // TOKEN_MATCH(tctx, TOKEN_RP, goto fail); @@ -759,7 +759,7 @@ value_t quote_sexp(env_t env, struct tctx *tctx, bool is_quasi) goto exit); *rightmost = new_cons; - rightmost = &new_cons->value.cons.right; + rightmost = &vvalue_cons(new_cons).right; TOKEN_NEXT(tctx); } @@ -811,10 +811,10 @@ static int toklist_expr(struct tctx *tctx, struct toklist **toklist) if(TOKEN(tctx)->type == TOKEN_LP) depth++; else if(TOKEN(tctx)->type == TOKEN_RP) depth--; - + // printf("%zu\n", depth); // print_token(TOKEN(tctx)); - + if(toklist) token_clone(&tokens[tokens_len++], TOKEN(tctx)); @@ -835,40 +835,40 @@ static struct toklist *value_to_toklist(value_t value) { struct token token = {0}; - switch(value->type) { + switch(vvalue_type(value)) { case VALUE_ATOM: // fix me - if(strcmp(value->value.atom, "lambda") == 0) { + if(strcmp(vvalue_atom(value), "lambda") == 0) { SET_TOKEN_TYPE(&token, TOKEN_LAMBDA); break; - } else if(strcmp(value->value.atom, "if") == 0) { + } else if(strcmp(vvalue_atom(value), "if") == 0) { SET_TOKEN_TYPE(&token, TOKEN_IF); break; - } else if(strcmp(value->value.atom, "quote") == 0) { + } else if(strcmp(vvalue_atom(value), "quote") == 0) { SET_TOKEN_TYPE(&token, TOKEN_QUOTE_FORM); break; - } else if(strcmp(value->value.atom, "'") == 0) { + } else if(strcmp(vvalue_atom(value), "'") == 0) { SET_TOKEN_TYPE(&token, TOKEN_QUOTE); break; } SET_TOKEN_TYPE(&token, TOKEN_ID); SET_TOKEN_VALUE(&token, id, - str_alloc_copy(value->value.atom)); + str_alloc_copy(vvalue_atom(value))); break; case VALUE_STR: SET_TOKEN_TYPE(&token, TOKEN_STR); SET_TOKEN_VALUE(&token, str, - str_alloc_copy(value->value.str)); + str_alloc_copy(vvalue_atom(value))); break; case VALUE_INT: SET_TOKEN_TYPE(&token, TOKEN_INT); - SET_TOKEN_VALUE(&token, num, value->value.num); + SET_TOKEN_VALUE(&token, num, vvalue_num(value)); break; case VALUE_CONS: return cons_to_toklist(value); default: - err("Cant turn '%s' to a token", value_type_string[value->type]); + err("Cant turn '%s' to a token", value_type_string[vvalue_type(value)]); return NULL; } @@ -894,15 +894,15 @@ static struct toklist *cons_to_toklist(value_t value) ADD_TOKEN(tail, TOKEN_LP); while(1) { - value_t left = value->value.cons.left; - value = value->value.cons.right; + value_t left = vvalue_cons(value).left; + value = vvalue_cons(value).right; struct toklist *new = NULL; ERR_Z(new = value_to_toklist(left), goto fail); ADD_TOKLIST(tail, new); - if(value->type == VALUE_NIL) break; - if(value->type != VALUE_CONS) { + if(vvalue_type(value) == VALUE_NIL) break; + if(vvalue_type(value) != VALUE_CONS) { ADD_TOKEN(tail, TOKEN_DOT); ERR_Z(new = value_to_toklist(value), goto fail); -- cgit v1.2.3