Skip to content

Commit

Permalink
rename some functions starting with hll_get_ to be without get_
Browse files Browse the repository at this point in the history
  • Loading branch information
Holodome committed Nov 17, 2023
1 parent 4ace44b commit 7f2e1b2
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 173 deletions.
133 changes: 62 additions & 71 deletions hololisp/hll_builtins.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions hololisp/hll_bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ void make_ident(void *file, size_t ident) {
void dump_function_info(void *file, hll_value value);

void hll_dump_value(void *file, hll_value value) {
uint8_t kind = hll_get_value_kind(value);
fprintf(file, "{ \"kind\": \"%s\"", hll_get_value_kind_str(kind));
uint8_t kind = hll_value_kind(value);
fprintf(file, "{ \"kind\": \"%s\"", hll_value_kind_str_(kind));
switch (kind) {
case HLL_VALUE_NIL:
case HLL_VALUE_TRUE:
Expand Down
22 changes: 11 additions & 11 deletions hololisp/hll_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ static uint16_t add_symb_const(hll_compiler *compiler, const char *symb_,
}

static void compile_symbol(hll_compiler *compiler, hll_value ast) {
assert(hll_get_value_kind(ast) == HLL_VALUE_SYMB);
assert(hll_is_symb(ast));
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_CONST);
hll_bytecode_emit_u16(compiler->bytecode,
add_symb_const(compiler, hll_unwrap_zsymb(ast),
Expand Down Expand Up @@ -849,7 +849,7 @@ static bool expand_macro(hll_compiler *compiler, hll_value list,
hll_value *expanded) {
hll_value macro = hll_unwrap_car(list);
hll_value args = hll_unwrap_cdr(list);
if (hll_get_value_kind(macro) != HLL_VALUE_SYMB) {
if (!hll_is_symb(macro)) {
return false;
}

Expand Down Expand Up @@ -960,11 +960,11 @@ static void compile_let(hll_compiler *compiler, hll_value args) {
hll_value name = hll_unwrap_car(pair);
hll_value value = hll_unwrap_cdr(pair);
if (hll_is_cons(value)) {
assert(hll_get_value_kind(hll_unwrap_cdr(value)) == HLL_VALUE_NIL);
assert(hll_is_nil(hll_unwrap_cdr(value)));
value = hll_unwrap_car(value);
}

assert(hll_get_value_kind(name) == HLL_VALUE_SYMB);
assert(hll_is_symb(name));
compile_expression(compiler, name);
compile_eval_expression(compiler, value);
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_LET);
Expand Down Expand Up @@ -1005,11 +1005,11 @@ HLL_ENUMERATE_CAR_CDR

static hll_location_form get_location_form(hll_value location) {
hll_location_form kind = HLL_LOC_NONE;
if (hll_get_value_kind(location) == HLL_VALUE_SYMB) {
if (hll_is_symb(location)) {
kind = HLL_LOC_FORM_SYMB;
} else if (hll_is_cons(location)) {
hll_value first = hll_unwrap_car(location);
if (hll_get_value_kind(first) == HLL_VALUE_SYMB) {
if (hll_is_symb(first)) {
const char *symb = hll_unwrap_zsymb(first);
if (strcmp(symb, "nth") == 0) {
kind = HLL_LOC_FORM_NTH;
Expand Down Expand Up @@ -1155,7 +1155,7 @@ static void compile_setcdr(hll_compiler *compiler, hll_value args) {
hll_value location = hll_unwrap_car(hll_unwrap_cdr(args));
hll_value value = hll_unwrap_cdr(hll_unwrap_cdr(args));
if (hll_is_cons(value)) {
assert(hll_get_value_kind(hll_unwrap_cdr(value)) == HLL_VALUE_NIL);
assert(hll_is_nil(hll_unwrap_cdr(value)));
value = hll_unwrap_car(value);
}

Expand Down Expand Up @@ -1371,15 +1371,15 @@ static void compile_define(hll_compiler *compiler, hll_value args) {
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_MAKEFUN);
hll_bytecode_emit_u16(compiler->bytecode, function_idx);
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_LET);
} else if (hll_get_value_kind(decide) == HLL_VALUE_SYMB) {
} else if (hll_is_symb(decide)) {
if (hll_list_length(args) > 3) {
compiler_error(compiler, args,
"'define' variable expects exactly 2 or 3 arguments");
return;
}
hll_value value = rest;
if (hll_is_cons(value)) {
assert(hll_get_value_kind(hll_unwrap_cdr(value)) == HLL_VALUE_NIL);
assert(hll_is_nil(hll_unwrap_cdr(value)));
value = hll_unwrap_car(value);
}

Expand Down Expand Up @@ -1449,7 +1449,7 @@ static void compile_form(hll_compiler *compiler, hll_value args,
}

static void compile_eval_expression(hll_compiler *compiler, hll_value ast) {
switch (hll_get_value_kind(ast)) {
switch (hll_value_kind(ast)) {
case HLL_VALUE_NIL:
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_NIL);
break;
Expand Down Expand Up @@ -1487,7 +1487,7 @@ static void compile_eval_expression(hll_compiler *compiler, hll_value ast) {
// Does not evaluate it.
// After it one value is located on top of the stack.
static void compile_expression(hll_compiler *compiler, hll_value ast) {
switch (hll_get_value_kind(ast)) {
switch (hll_value_kind(ast)) {
case HLL_VALUE_NIL:
hll_bytecode_emit_op(compiler->bytecode, HLL_BC_NIL);
break;
Expand Down
7 changes: 6 additions & 1 deletion hololisp/hll_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ static uint32_t djb2(const char *src, const char *dst) {
return hash;
}

const char *hll_get_value_kind_str(uint8_t kind) {
const char *hll_value_kind_str_(uint8_t kind) {
static const char *strs[] = {"num", "nil", "true", "cons",
"symb", "bind", "env", "func"};

assert(kind < sizeof(strs) / sizeof(strs[0]));
return strs[kind];
}

HLL_PUB const char *hll_value_kind_str(hll_value value) {
return hll_value_kind_str_(hll_value_kind(value));
}

void hll_free_obj(hll_vm *vm, hll_obj *obj) {
switch (obj->kind) {
case HLL_VALUE_CONS:
Expand Down Expand Up @@ -60,6 +64,7 @@ static void register_gc_obj(hll_vm *vm, hll_obj *obj) {
hll_value hll_new_symbol(hll_vm *vm, const char *symbol, size_t length) {
assert(symbol != NULL);
assert(length != 0);
assert(length < UINT32_MAX);

void *memory =
hll_gc_alloc(vm->gc, sizeof(hll_obj) + sizeof(hll_obj_symb) + length + 1);
Expand Down
8 changes: 4 additions & 4 deletions hololisp/hll_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ enum {
HLL_VALUE_FUNC = 0x7,
};

HLL_PUB const char *hll_get_value_kind_str(uint8_t kind);
HLL_PUB const char *hll_value_kind_str_(uint8_t kind);
HLL_PUB const char *hll_value_kind_str(hll_value value);

typedef struct hll_obj {
uint8_t kind;
Expand All @@ -42,7 +43,6 @@ typedef struct hll_obj_cons {

typedef struct hll_obj_func {
struct hll_bytecode *bytecode;
// List if function parameter names
hll_value param_names;
hll_value env;
} hll_obj_func;
Expand All @@ -57,7 +57,7 @@ typedef struct hll_obj_bind {
} hll_obj_bind;

typedef struct hll_obj_symb {
size_t length;
uint32_t length;
uint32_t hash;
char symb[];
} hll_obj_symb;
Expand Down Expand Up @@ -118,7 +118,7 @@ static inline bool hll_is_num(hll_value value) {
static inline bool hll_is_obj(hll_value value) {
return (((value) & (HLL_QNAN | HLL_SIGN_BIT)) == (HLL_QNAN | HLL_SIGN_BIT));
}
static inline uint8_t hll_get_value_kind(hll_value value) {
static inline uint8_t hll_value_kind(hll_value value) {
return hll_is_obj(value) ? nan_unbox_ptr(value)->kind
: nan_unbox_singleton(value);
}
Expand Down
29 changes: 14 additions & 15 deletions hololisp/hll_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ void hll_add_binding(hll_vm *vm, const char *symb_str,
}

bool hll_find_var(hll_value env, hll_value car, hll_value *found) {
assert(hll_get_value_kind(car) == HLL_VALUE_SYMB &&
"argument is not a symbol");
assert(hll_is_symb(car) && "argument is not a symbol");
hll_obj_symb *symb = hll_unwrap_symb(car);
for (; !hll_is_nil(env); env = hll_unwrap_env(env)->up) {
for (hll_value cons = hll_unwrap_env(env)->vars; hll_is_cons(cons);
cons = hll_unwrap_cdr(cons)) {
hll_value test = hll_unwrap_car(cons);
assert(hll_get_value_kind(hll_unwrap_car(test)) == HLL_VALUE_SYMB &&
assert(hll_is_symb(hll_unwrap_car(test)) &&
"Variable is not a cons of symbol and its value");
if (symb->hash == hll_unwrap_symb(hll_unwrap_car(test))->hash) {
if (found != NULL) {
Expand All @@ -137,7 +136,7 @@ bool hll_find_var(hll_value env, hll_value car, hll_value *found) {
}

void hll_print_value(hll_vm *vm, hll_value value) {
switch (hll_get_value_kind(value)) {
switch (hll_value_kind(value)) {
case HLL_VALUE_CONS:
hll_print(vm, "(");
while (hll_is_cons(value)) {
Expand All @@ -148,7 +147,7 @@ void hll_print_value(hll_vm *vm, hll_value value) {
hll_print(vm, " . ");
hll_print_value(vm, cdr);
break;
} else if (hll_get_value_kind(cdr) != HLL_VALUE_NIL) {
} else if (!hll_is_nil(cdr)) {
hll_print(vm, " ");
}

Expand Down Expand Up @@ -198,7 +197,7 @@ void hll_runtime_error(hll_vm *vm, const char *fmt, ...) {

static void call_func(hll_vm *vm, hll_value callable, hll_value args,
hll_call_frame **current_call_frame, bool mbtr) {
switch (hll_get_value_kind(callable)) {
switch (hll_value_kind(callable)) {
case HLL_VALUE_FUNC: {
hll_obj_func *func = hll_unwrap_func(callable);
hll_value new_env = hll_new_env(vm, func->env, hll_nil());
Expand All @@ -209,7 +208,7 @@ static void call_func(hll_vm *vm, hll_value callable, hll_value args,
for (; hll_is_cons(param_name);
param_name = hll_unwrap_cdr(param_name),
param_value = hll_unwrap_cdr(param_value)) {
if (hll_get_value_kind(param_value) != HLL_VALUE_CONS) {
if (!hll_is_cons(param_value)) {
hll_runtime_error(vm, "number of arguments does not match");
}
hll_value name = hll_unwrap_car(param_name);
Expand Down Expand Up @@ -249,7 +248,7 @@ static void call_func(hll_vm *vm, hll_value callable, hll_value args,
} break;
default:
hll_runtime_error(vm, "object is not callable (got %s)",
hll_get_value_kind_str(hll_get_value_kind(callable)));
hll_value_kind_str(callable));
break;
}
}
Expand Down Expand Up @@ -346,10 +345,10 @@ hll_value hll_interpret_bytecode_internal(hll_vm *vm, hll_value env_,
if (hll_is_nil(*headp)) {
*headp = *tailp = cons;
} else {
if (hll_get_value_kind(*tailp) != HLL_VALUE_CONS) {
if (!hll_is_cons(*tailp)) {
hll_runtime_error(vm,
"tail operand of APPEND is not a cons (found %s)",
hll_get_value_kind_str(hll_get_value_kind(*tailp)));
hll_value_kind_str(*tailp));
}
hll_unwrap_cons(*tailp)->cdr = cons;
*tailp = cons;
Expand All @@ -365,9 +364,9 @@ hll_value hll_interpret_bytecode_internal(hll_vm *vm, hll_value env_,
HLL_VM_CASE(HLL_BC_FIND) {
hll_value symb = hll_sb_pop(vm->stack);
hll_gc_push_temp_root(vm->gc, symb);
if (hll_get_value_kind(symb) != HLL_VALUE_SYMB) {
if (!hll_is_symb(symb)) {
hll_runtime_error(vm, "operand of FIND is not a symb (found %s)",
hll_get_value_kind_str(hll_get_value_kind(symb)));
hll_value_kind_str(symb));
}

hll_value found;
Expand Down Expand Up @@ -437,7 +436,7 @@ hll_value hll_interpret_bytecode_internal(hll_vm *vm, hll_value env_,
HLL_VM_CASE(HLL_BC_POPENV) {
vm->env = hll_unwrap_env(vm->env)->up;
assert(vm->env);
assert(hll_get_value_kind(vm->env) == HLL_VALUE_ENV);
assert(hll_value_kind(vm->env) == HLL_VALUE_ENV);
HLL_VM_NEXT();
}
HLL_VM_CASE(HLL_BC_CAR) {
Expand Down Expand Up @@ -483,7 +482,7 @@ hll_value hll_interpret_bytecode_internal(hll_vm *vm, hll_value env_,
assert(idx < hll_sb_len(current_call_frame->bytecode->constant_pool));

hll_value value = current_call_frame->bytecode->constant_pool[idx];
assert(hll_get_value_kind(value) == HLL_VALUE_FUNC);
assert(hll_value_kind(value) == HLL_VALUE_FUNC);
value = hll_new_func(vm, hll_unwrap_func(value)->param_names,
hll_unwrap_func(value)->bytecode);
hll_unwrap_func(value)->env = vm->env;
Expand Down Expand Up @@ -534,7 +533,7 @@ hll_expand_macro_result hll_expand_macro(hll_vm *vm, hll_value macro,
if (hll_is_cons(param_name) && hll_is_symb(hll_unwrap_car(param_name))) {
for (; hll_is_cons(param_name); param_name = hll_unwrap_cdr(param_name),
param_value = hll_unwrap_cdr(param_value)) {
if (hll_get_value_kind(param_value) != HLL_VALUE_CONS) {
if (!hll_is_cons(param_value)) {
return HLL_EXPAND_MACRO_ERR_ARGS;
}
hll_value name = hll_unwrap_car(param_name);
Expand Down
8 changes: 4 additions & 4 deletions tests/test_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ static void test_compiler_compiles_define(void) {

TEST_ASSERT(hll_sb_len(compiled->constant_pool) >= 1);
hll_value func = compiled->constant_pool[1];
TEST_ASSERT(hll_get_value_kind(func) == HLL_VALUE_FUNC);
TEST_ASSERT(hll_value_kind(func) == HLL_VALUE_FUNC);

struct hll_bytecode *function_bytecode_compiled =
hll_unwrap_func(func)->bytecode;
Expand Down Expand Up @@ -441,7 +441,7 @@ static void test_compiler_compiles_lambda(void) {

TEST_ASSERT(hll_sb_len(compiled->constant_pool) >= 1);
hll_value func = compiled->constant_pool[0];
TEST_ASSERT(hll_get_value_kind(func) == HLL_VALUE_FUNC);
TEST_ASSERT(hll_value_kind(func) == HLL_VALUE_FUNC);

struct hll_bytecode *function_bytecode_compiled =
hll_unwrap_func(func)->bytecode;
Expand All @@ -462,7 +462,7 @@ static void test_compiler_generates_mbtr(void) {
struct hll_bytecode *compiled = hll_unwrap_func(result)->bytecode;
TEST_ASSERT(hll_sb_len(compiled->constant_pool) >= 1);
hll_value func = compiled->constant_pool[1];
TEST_ASSERT(hll_get_value_kind(func) == HLL_VALUE_FUNC);
TEST_ASSERT(hll_value_kind(func) == HLL_VALUE_FUNC);
struct hll_bytecode *function_bytecode_compiled =
hll_unwrap_func(func)->bytecode;
TEST_ASSERT(hll_is_symb(function_bytecode_compiled->name));
Expand Down Expand Up @@ -510,7 +510,7 @@ static void test_compiler_generates_mbtr_in_if(void) {
struct hll_bytecode *compiled = hll_unwrap_func(result)->bytecode;
TEST_ASSERT(hll_sb_len(compiled->constant_pool) >= 1);
hll_value func = compiled->constant_pool[1];
TEST_ASSERT(hll_get_value_kind(func) == HLL_VALUE_FUNC);
TEST_ASSERT(hll_value_kind(func) == HLL_VALUE_FUNC);
struct hll_bytecode *function_bytecode_compiled =
hll_unwrap_func(func)->bytecode;
test_bytecode_equals(bytecode, sizeof(bytecode), function_bytecode_compiled);
Expand Down
Loading

0 comments on commit 7f2e1b2

Please sign in to comment.