Shorter name for internal enum apfl_value_type

This commit is contained in:
Laria 2022-02-10 21:55:40 +01:00
parent dd314c5abb
commit d86c9375a4
3 changed files with 63 additions and 63 deletions

View file

@ -175,7 +175,7 @@ variable_new(apfl_ctx ctx)
}
var->refcount = 1;
var->value.type = APFL_VALUE_NIL;
var->value.type = VALUE_NIL;
return var;
}
@ -395,26 +395,26 @@ constant_to_value(struct apfl_expr_const *constant)
{
switch (constant->type) {
case APFL_EXPR_CONST_NIL:
return (struct apfl_value) { .type = APFL_VALUE_NIL };
return (struct apfl_value) { .type = VALUE_NIL };
case APFL_EXPR_CONST_BOOLEAN:
return (struct apfl_value) {
.type = APFL_VALUE_BOOLEAN,
.type = VALUE_BOOLEAN,
.boolean = constant->boolean,
};
case APFL_EXPR_CONST_STRING:
return (struct apfl_value) {
.type = APFL_VALUE_STRING,
.type = VALUE_STRING,
.string = apfl_refcounted_string_incref(constant->string),
};
case APFL_EXPR_CONST_NUMBER:
return (struct apfl_value) {
.type = APFL_VALUE_NUMBER,
.type = VALUE_NUMBER,
.number = constant->number,
};
}
assert(false);
return (struct apfl_value) { .type = APFL_VALUE_NIL };
return (struct apfl_value) { .type = VALUE_NIL };
}
static bool
@ -517,7 +517,7 @@ match_pattern_from_var_or_member(
pattern->value = (struct match_pattern_value) {
.varname = NULL,
.var = NULL,
.value = {.type = APFL_VALUE_NIL},
.value = {.type = VALUE_NIL},
.member_keys = NULL,
.member_keys_len = 0,
.member_keys_cap = 0,
@ -540,7 +540,7 @@ next:
}
return MATCH_OK;
case APFL_EXPR_ASSIGNABLE_VAR_OR_MEMBER_DOT:
str_value.type = APFL_VALUE_STRING;
str_value.type = VALUE_STRING;
if ((str_value.string = apfl_string_copy_into_new_refcounted(
ctx->allocator,
apfl_string_view_from(var_or_member->dot.rhs)
@ -887,7 +887,7 @@ match_pattern_match_list_inner(
allocator,
&pattern_list->subpatterns[pattern_list->expand_index],
(struct apfl_value) {
.type = APFL_VALUE_LIST,
.type = VALUE_LIST,
.list = mid_list,
}
);
@ -899,7 +899,7 @@ match_pattern_match_list(
struct match_pattern_list *pattern_list,
struct apfl_value value
) {
if (value.type != APFL_VALUE_LIST) {
if (value.type != VALUE_LIST) {
apfl_value_deinit(allocator, &value);
return MATCH_DOESNT_MATCH;
}
@ -970,7 +970,7 @@ match_pattern_apply_value_keys(
apfl_value_incref(*key),
&value
)) {
if (value.type != APFL_VALUE_DICT) {
if (value.type != VALUE_DICT) {
apfl_value_deinit(allocator, &value);
return MATCH_ERROR;
}
@ -1005,7 +1005,7 @@ match_pattern_apply_value_keys(
ed,
apfl_value_incref(*key),
(struct apfl_value) {
.type = APFL_VALUE_DICT,
.type = VALUE_DICT,
.dict = next_dict,
}
)) {
@ -1027,7 +1027,7 @@ match_pattern_apply_value(apfl_ctx ctx, struct match_pattern_value *pattern_valu
return MATCH_OK;
}
if (pattern_value->var->value.type != APFL_VALUE_DICT) {
if (pattern_value->var->value.type != VALUE_DICT) {
return MATCH_ERROR;
}
@ -1056,7 +1056,7 @@ match_pattern_apply_value(apfl_ctx ctx, struct match_pattern_value *pattern_valu
}
variable_set(ctx, pattern_value->var, (struct apfl_value) {
.type = APFL_VALUE_DICT,
.type = VALUE_DICT,
.dict = d,
});
@ -1109,7 +1109,7 @@ evaluate_list_expr_to_list(apfl_ctx ctx, struct apfl_expr *expr, apfl_editable_l
return APFL_RESULT_ERR_FATAL;
}
if (value.type != APFL_VALUE_LIST) {
if (value.type != VALUE_LIST) {
apfl_value_deinit(ctx->allocator, &value);
return APFL_RESULT_ERR;
}
@ -1163,7 +1163,7 @@ evaluate_list(apfl_ctx ctx, struct apfl_expr_list *list)
}
if (!stack_push(ctx, (struct apfl_value) {
.type = APFL_VALUE_LIST,
.type = VALUE_LIST,
.list = out,
})) {
apfl_list_unref(ctx->allocator, out);
@ -1218,7 +1218,7 @@ evaluate_dict(apfl_ctx ctx, struct apfl_expr_dict *dict)
}
if (!stack_push(ctx, (struct apfl_value) {
.type = APFL_VALUE_DICT,
.type = VALUE_DICT,
.dict = out,
})) {
apfl_dict_unref(ctx->allocator, out);
@ -1235,7 +1235,7 @@ evaluate_dot(apfl_ctx ctx, struct apfl_expr_dot *dot)
struct apfl_value lhs = stack_must_pop(ctx, -1);
struct apfl_value key = (struct apfl_value) {
.type = APFL_VALUE_STRING,
.type = VALUE_STRING,
.string = apfl_refcounted_string_incref(dot->rhs),
};
@ -1393,7 +1393,7 @@ evaluate(apfl_ctx ctx, struct apfl_expr *expr)
return evaluate_var(ctx, apfl_refcounted_string_incref(expr->var));
case APFL_EXPR_BLANK:
return stack_push(ctx, (struct apfl_value) {
.type = APFL_VALUE_NIL,
.type = VALUE_NIL,
})
? APFL_RESULT_OK
: APFL_RESULT_ERR_FATAL;

View file

@ -153,20 +153,20 @@ print(unsigned indent, FILE *out, struct apfl_value value, bool skip_first_inden
unsigned first_indent = skip_first_indent ? 0 : indent;
switch (value.type) {
case APFL_VALUE_NIL:
case VALUE_NIL:
apfl_print_indented(first_indent, out, "nil");
return;
case APFL_VALUE_BOOLEAN:
case VALUE_BOOLEAN:
apfl_print_indented(first_indent, out, value.boolean ? "true" : "false");
return;
case APFL_VALUE_NUMBER:
case VALUE_NUMBER:
apfl_print_indented(first_indent, out, "%f", value.number);
return;
case APFL_VALUE_STRING:
case VALUE_STRING:
sv = apfl_string_view_from(value.string);
apfl_print_indented(first_indent, out, "\"" APFL_STR_FMT "\"", APFL_STR_FMT_ARGS(sv));
return;
case APFL_VALUE_LIST:
case VALUE_LIST:
if (value.list->len == 0) {
apfl_print_indented(first_indent, out, "[]");
return;
@ -178,7 +178,7 @@ print(unsigned indent, FILE *out, struct apfl_value value, bool skip_first_inden
}
apfl_print_indented(indent, out, "]");
return;
case APFL_VALUE_DICT:
case VALUE_DICT:
if (apfl_hashmap_count(value.dict->map) == 0) {
apfl_print_indented(first_indent, out, "[->]");
return;
@ -209,7 +209,7 @@ struct apfl_value
apfl_value_move(struct apfl_value *src)
{
struct apfl_value out = *src;
src->type = APFL_VALUE_NIL;
src->type = VALUE_NIL;
return out;
}
@ -217,18 +217,18 @@ struct apfl_value
apfl_value_incref(struct apfl_value value)
{
switch (value.type) {
case APFL_VALUE_NIL:
case APFL_VALUE_BOOLEAN:
case APFL_VALUE_NUMBER:
case VALUE_NIL:
case VALUE_BOOLEAN:
case VALUE_NUMBER:
// Nothing to do
return value;
case APFL_VALUE_STRING:
case VALUE_STRING:
value.string = apfl_refcounted_string_incref(value.string);
return value;
case APFL_VALUE_LIST:
case VALUE_LIST:
value.list = apfl_list_incref(value.list);
return value;
case APFL_VALUE_DICT:
case VALUE_DICT:
value.dict = apfl_dict_incref(value.dict);
return value;
}
@ -304,17 +304,17 @@ apfl_value_eq(const struct apfl_value a, const struct apfl_value b)
}
switch (a.type) {
case APFL_VALUE_NIL:
case VALUE_NIL:
return true;
case APFL_VALUE_BOOLEAN:
case VALUE_BOOLEAN:
return a.boolean == b.boolean;
case APFL_VALUE_NUMBER:
case VALUE_NUMBER:
return a.number == b.number;
case APFL_VALUE_STRING:
case VALUE_STRING:
return apfl_string_eq(a.string, b.string);
case APFL_VALUE_LIST:
case VALUE_LIST:
return list_eq(a.list, b.list);
case APFL_VALUE_DICT:
case VALUE_DICT:
return dict_eq(a.dict, b.dict);
}
@ -543,19 +543,19 @@ void
apfl_value_deinit(struct apfl_allocator allocator, struct apfl_value *value)
{
switch (value->type) {
case APFL_VALUE_NIL:
case APFL_VALUE_BOOLEAN:
case APFL_VALUE_NUMBER:
case VALUE_NIL:
case VALUE_BOOLEAN:
case VALUE_NUMBER:
goto ok;
case APFL_VALUE_STRING:
case VALUE_STRING:
apfl_refcounted_string_unref(allocator, value->string);
value->string = NULL;
goto ok;
case APFL_VALUE_LIST:
case VALUE_LIST:
apfl_list_unref(allocator, value->list);
value->list = NULL;
goto ok;
case APFL_VALUE_DICT:
case VALUE_DICT:
apfl_dict_unref(allocator, value->dict);
value->dict = NULL;
goto ok;
@ -564,7 +564,7 @@ apfl_value_deinit(struct apfl_allocator allocator, struct apfl_value *value)
assert(false);
ok:
value->type = APFL_VALUE_NIL;
value->type = VALUE_NIL;
}
bool
@ -592,8 +592,8 @@ apfl_dict_get_item(struct apfl_allocator allocator, apfl_dict dict, struct apfl_
static enum get_item_result
get_item(struct apfl_allocator allocator, /*borrowed*/ struct apfl_value container, /*borrowed*/ struct apfl_value key, struct apfl_value *out)
{
if (container.type == APFL_VALUE_LIST) {
if (key.type != APFL_VALUE_NUMBER) {
if (container.type == VALUE_LIST) {
if (key.type != VALUE_NUMBER) {
return GET_ITEM_WRONG_KEY_TYPE;
}
@ -605,7 +605,7 @@ get_item(struct apfl_allocator allocator, /*borrowed*/ struct apfl_value contain
)
? GET_ITEM_OK
: GET_ITEM_KEY_DOESNT_EXIST;
} else if (container.type == APFL_VALUE_DICT) {
} else if (container.type == VALUE_DICT) {
return apfl_dict_get_item(
allocator,
apfl_dict_incref(container.dict),
@ -631,30 +631,30 @@ apfl_value_get_item(struct apfl_allocator allocator, struct apfl_value container
static apfl_hash
value_hash(const struct apfl_value value)
{
apfl_hash hash = apfl_hash_fnv1a(&value.type, sizeof(enum apfl_value_type));
apfl_hash hash = apfl_hash_fnv1a(&value.type, sizeof(enum value_type));
struct apfl_string_view sv;
switch (value.type) {
case APFL_VALUE_NIL:
case VALUE_NIL:
goto ok;
case APFL_VALUE_BOOLEAN:
case VALUE_BOOLEAN:
hash = apfl_hash_fnv1a_add(&value.boolean, sizeof(bool), hash);
goto ok;
case APFL_VALUE_NUMBER:
case VALUE_NUMBER:
hash = apfl_hash_fnv1a_add(&value.number, sizeof(apfl_number), hash);
goto ok;
case APFL_VALUE_STRING:
case VALUE_STRING:
sv = apfl_string_view_from(value.string);
hash = apfl_hash_fnv1a_add(sv.bytes, sv.len, hash);
goto ok;
case APFL_VALUE_LIST:
case VALUE_LIST:
for (size_t i = 0; i < value.list->len; i++) {
apfl_hash item_hash = value_hash(value.list->items[i]);
hash = apfl_hash_fnv1a_add(&item_hash, sizeof(apfl_hash), hash);
}
goto ok;
case APFL_VALUE_DICT:
case VALUE_DICT:
// TODO: This results in all dictionaries having the same hash. Since
// it's rather unusual to have dictionaries as keys, this is fine
// for now, but should be improved nonetheless!

View file

@ -10,13 +10,13 @@ extern "C" {
#include "apfl.h"
enum apfl_value_type {
APFL_VALUE_NIL,
APFL_VALUE_BOOLEAN,
APFL_VALUE_NUMBER,
APFL_VALUE_STRING,
APFL_VALUE_LIST,
APFL_VALUE_DICT,
enum value_type {
VALUE_NIL,
VALUE_BOOLEAN,
VALUE_NUMBER,
VALUE_STRING,
VALUE_LIST,
VALUE_DICT,
// TODO: functions/closures
};
@ -27,7 +27,7 @@ struct apfl_dict_data;
typedef struct apfl_dict_data *apfl_dict;
struct apfl_value {
enum apfl_value_type type;
enum value_type type;
union {
bool boolean;
apfl_number number;