mie: implemented array value type; restructure const value structures
there are now separate structs for all const types (int, string, etc), rather than a single mie_const union.
This commit is contained in:
@@ -44,11 +44,11 @@ struct mie_func *mie_builder_get_current_func(struct mie_builder *builder)
|
|||||||
return block->b_parent;
|
return block->b_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mie_builder_put_record(
|
struct mie_record *mie_builder_put_record(
|
||||||
struct mie_builder *builder, struct mie_const *val, const char *name)
|
struct mie_builder *builder, struct mie_const *val, const char *name)
|
||||||
{
|
{
|
||||||
if (!builder->b_module) {
|
if (!builder->b_module) {
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
b_queue_iterator it = {};
|
b_queue_iterator it = {};
|
||||||
@@ -57,13 +57,36 @@ void mie_builder_put_record(
|
|||||||
= b_unbox(struct mie_value, it.entry, v_entry);
|
= b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
|
||||||
if (!strcmp(rec->v_name.n_str, name)) {
|
if (!strcmp(rec->v_name.n_str, name)) {
|
||||||
return;
|
/* TODO what to do about `val` here? */
|
||||||
|
return MIE_RECORD(rec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mie_record *rec = mie_record_create(val);
|
struct mie_record *rec = mie_record_create(val);
|
||||||
rec->r_base.v_name.n_str = b_strdup(name);
|
rec->r_base.v_name.n_str = b_strdup(name);
|
||||||
b_queue_push_back(&builder->b_module->m_records, &rec->r_base.v_entry);
|
b_queue_push_back(&builder->b_module->m_records, &rec->r_base.v_entry);
|
||||||
|
|
||||||
|
return rec;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mie_record *mie_builder_get_record(
|
||||||
|
struct mie_builder *builder, const char *name)
|
||||||
|
{
|
||||||
|
if (!builder->b_module) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_queue_iterator it = {};
|
||||||
|
b_queue_foreach (&it, &builder->b_module->m_records) {
|
||||||
|
struct mie_value *rec
|
||||||
|
= b_unbox(struct mie_value, it.entry, v_entry);
|
||||||
|
|
||||||
|
if (!strcmp(rec->v_name.n_str, name)) {
|
||||||
|
return MIE_RECORD(rec);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
|
|||||||
10
mie/const.c
10
mie/const.c
@@ -2,20 +2,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct mie_const *mie_const_create(struct mie_type *type)
|
void mie_const_init(struct mie_const *c, struct mie_type *type)
|
||||||
{
|
{
|
||||||
struct mie_const *c = malloc(sizeof *c);
|
|
||||||
if (!c) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(c, 0x0, sizeof *c);
|
memset(c, 0x0, sizeof *c);
|
||||||
|
|
||||||
mie_value_init(&c->c_base, MIE_VALUE_CONST);
|
mie_value_init(&c->c_base, MIE_VALUE_CONST);
|
||||||
|
|
||||||
c->c_type = type;
|
c->c_type = type;
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct mie_type *get_type(struct mie_value *v)
|
static struct mie_type *get_type(struct mie_value *v)
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ static void mie_type_to_string(struct mie_type *type, char *out, size_t max)
|
|||||||
case MIE_TYPE_LABEL:
|
case MIE_TYPE_LABEL:
|
||||||
snprintf(out, max, "label");
|
snprintf(out, max, "label");
|
||||||
break;
|
break;
|
||||||
|
case MIE_TYPE_ARRAY:
|
||||||
|
snprintf(out, max, "array");
|
||||||
|
break;
|
||||||
case MIE_TYPE_SELECTOR:
|
case MIE_TYPE_SELECTOR:
|
||||||
snprintf(out, max, "");
|
snprintf(out, max, "");
|
||||||
break;
|
break;
|
||||||
@@ -150,23 +153,62 @@ static b_status write_operand_const(
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (c->c_type->t_id) {
|
switch (c->c_type->t_id) {
|
||||||
case MIE_TYPE_INT:
|
case MIE_TYPE_INT: {
|
||||||
write_string_f(converter, "#%" PRId64, c->c_v.v_int);
|
struct mie_int *v = MIE_INT(c);
|
||||||
|
write_string_f(converter, "#%" PRId64, v->i_value);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case MIE_TYPE_PTR:
|
case MIE_TYPE_PTR:
|
||||||
case MIE_TYPE_ID:
|
case MIE_TYPE_ID:
|
||||||
write_string_f(converter, "%%%s", value->v_name.n_str);
|
write_string_f(converter, "%%%s", value->v_name.n_str);
|
||||||
break;
|
break;
|
||||||
case MIE_TYPE_STR:
|
case MIE_TYPE_STR: {
|
||||||
case MIE_TYPE_ATOM:
|
struct mie_string *v = MIE_STRING(c);
|
||||||
write_string_f(converter, "\"%s\"", c->c_v.v_str);
|
|
||||||
|
write_string_f(converter, "\"%s\"", v->s_value);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
case MIE_TYPE_ATOM: {
|
||||||
|
struct mie_atom *v = MIE_ATOM(c);
|
||||||
|
|
||||||
|
write_string_f(converter, "\"%s\"", v->a_value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case MIE_TYPE_SELECTOR: {
|
||||||
|
struct mie_selector *v = MIE_SELECTOR(c);
|
||||||
|
|
||||||
|
write_string_f(converter, "\"%s\"", v->sel_value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case MIE_TYPE_CLASS:
|
case MIE_TYPE_CLASS:
|
||||||
write_string_f(converter, "@%s", value->v_name.n_str);
|
write_string_f(converter, "@%s", value->v_name.n_str);
|
||||||
break;
|
break;
|
||||||
case MIE_TYPE_SELECTOR:
|
case MIE_TYPE_ARRAY: {
|
||||||
write_string_f(converter, "@%s", c->c_v.v_selector);
|
struct mie_array *array = MIE_ARRAY(value);
|
||||||
|
b_list_iterator it;
|
||||||
|
b_list_iterator_begin(array->a_values, &it);
|
||||||
|
|
||||||
|
write_char(converter, '{');
|
||||||
|
while (b_list_iterator_is_valid(&it)) {
|
||||||
|
if (it.i > 0) {
|
||||||
|
write_char(converter, ',');
|
||||||
|
}
|
||||||
|
|
||||||
|
write_string(converter, "\n ");
|
||||||
|
|
||||||
|
struct mie_value *child = it.item;
|
||||||
|
write_operand_const(converter, child, F_INCLUDE_TYPE);
|
||||||
|
|
||||||
|
b_list_iterator_next(&it);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!b_list_empty(array->a_values)) {
|
||||||
|
write_char(converter, '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
write_char(converter, '}');
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
49
mie/ctx.c
49
mie/ctx.c
@@ -1,4 +1,5 @@
|
|||||||
#include <blue/object/hashmap.h>
|
#include <blue/object/hashmap.h>
|
||||||
|
#include <blue/object/list.h>
|
||||||
#include <blue/object/string.h>
|
#include <blue/object/string.h>
|
||||||
#include <mie/const.h>
|
#include <mie/const.h>
|
||||||
#include <mie/ctx.h>
|
#include <mie/ctx.h>
|
||||||
@@ -13,7 +14,7 @@ struct ctx_int_cache_entry {
|
|||||||
|
|
||||||
struct ctx_int_value_cache_entry {
|
struct ctx_int_value_cache_entry {
|
||||||
b_btree_node i_node;
|
b_btree_node i_node;
|
||||||
struct mie_const i_value;
|
struct mie_int i_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
B_BTREE_DEFINE_SIMPLE_INSERT(
|
B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||||
@@ -23,10 +24,10 @@ B_BTREE_DEFINE_SIMPLE_GET(
|
|||||||
get_cached_int_type)
|
get_cached_int_type)
|
||||||
|
|
||||||
B_BTREE_DEFINE_SIMPLE_INSERT(
|
B_BTREE_DEFINE_SIMPLE_INSERT(
|
||||||
struct ctx_int_value_cache_entry, i_node, i_value.c_v.v_int,
|
struct ctx_int_value_cache_entry, i_node, i_value.i_value,
|
||||||
put_cached_int_value)
|
put_cached_int_value)
|
||||||
B_BTREE_DEFINE_SIMPLE_GET(
|
B_BTREE_DEFINE_SIMPLE_GET(
|
||||||
struct ctx_int_value_cache_entry, int64_t, i_node, i_value.c_v.v_int,
|
struct ctx_int_value_cache_entry, int64_t, i_node, i_value.i_value,
|
||||||
get_cached_int_value)
|
get_cached_int_value)
|
||||||
|
|
||||||
struct mie_ctx *mie_ctx_create(void)
|
struct mie_ctx *mie_ctx_create(void)
|
||||||
@@ -172,9 +173,8 @@ struct mie_value *mie_ctx_get_int(
|
|||||||
|
|
||||||
memset(value, 0x0, sizeof *value);
|
memset(value, 0x0, sizeof *value);
|
||||||
|
|
||||||
mie_value_init(&value->i_value.c_base, MIE_VALUE_CONST);
|
mie_const_init(&value->i_value.i_base, &entry->i_type);
|
||||||
value->i_value.c_type = &entry->i_type;
|
value->i_value.i_value = val;
|
||||||
value->i_value.c_v.v_int = val;
|
|
||||||
|
|
||||||
put_cached_int_value(&entry->i_values, value);
|
put_cached_int_value(&entry->i_values, value);
|
||||||
|
|
||||||
@@ -195,16 +195,16 @@ struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel)
|
|||||||
return cache_entry->value_data;
|
return cache_entry->value_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mie_const *sel_value = malloc(sizeof *sel_value);
|
struct mie_selector *sel_value = malloc(sizeof *sel_value);
|
||||||
if (!sel_value) {
|
if (!sel_value) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mie_value_init(&sel_value->c_base, MIE_VALUE_CONST);
|
struct mie_type *sel_type = mie_ctx_get_type(ctx, MIE_TYPE_SELECTOR);
|
||||||
sel_value->c_type = mie_ctx_get_type(ctx, MIE_TYPE_SELECTOR);
|
mie_const_init(&sel_value->sel_base, sel_type);
|
||||||
sel_value->c_v.v_selector = b_strdup(sel);
|
sel_value->sel_value = b_strdup(sel);
|
||||||
|
|
||||||
key.key_data = sel_value->c_v.v_selector;
|
key.key_data = sel_value->sel_value;
|
||||||
|
|
||||||
b_hashmap_value hashmap_value = {
|
b_hashmap_value hashmap_value = {
|
||||||
.value_data = sel_value,
|
.value_data = sel_value,
|
||||||
@@ -230,16 +230,16 @@ struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
|
|||||||
return cache_entry->value_data;
|
return cache_entry->value_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mie_const *string_value = malloc(sizeof *string_value);
|
struct mie_string *string_value = malloc(sizeof *string_value);
|
||||||
if (!string_value) {
|
if (!string_value) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
mie_value_init(&string_value->c_base, MIE_VALUE_CONST);
|
struct mie_type *string_type = mie_ctx_get_type(ctx, MIE_TYPE_STR);
|
||||||
string_value->c_type = mie_ctx_get_type(ctx, MIE_TYPE_STR);
|
mie_const_init(&string_value->s_base, string_type);
|
||||||
string_value->c_v.v_str = b_strdup(s);
|
string_value->s_value = b_strdup(s);
|
||||||
|
|
||||||
key.key_data = string_value->c_v.v_str;
|
key.key_data = string_value->s_value;
|
||||||
|
|
||||||
b_hashmap_value hashmap_value = {
|
b_hashmap_value hashmap_value = {
|
||||||
.value_data = string_value,
|
.value_data = string_value,
|
||||||
@@ -250,3 +250,20 @@ struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
|
|||||||
|
|
||||||
return MIE_VALUE(string_value);
|
return MIE_VALUE(string_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx)
|
||||||
|
{
|
||||||
|
struct mie_type *array_type = mie_ctx_get_type(ctx, MIE_TYPE_ARRAY);
|
||||||
|
struct mie_array *array = malloc(sizeof *array);
|
||||||
|
|
||||||
|
if (!array) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(array, 0x0, sizeof *array);
|
||||||
|
mie_const_init(&array->a_base, array_type);
|
||||||
|
|
||||||
|
array->a_values = b_list_create();
|
||||||
|
|
||||||
|
return MIE_VALUE(array);
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,8 +38,10 @@ static inline struct mie_block *mie_builder_get_current_block(
|
|||||||
return builder->b_current_block;
|
return builder->b_current_block;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void mie_builder_put_record(
|
extern struct mie_record *mie_builder_put_record(
|
||||||
struct mie_builder *builder, struct mie_const *val, const char *name);
|
struct mie_builder *builder, struct mie_const *val, const char *name);
|
||||||
|
extern struct mie_record *mie_builder_get_record(
|
||||||
|
struct mie_builder *builder, const char *name);
|
||||||
extern void mie_builder_put_data(struct mie_builder *builder, struct mie_data *data);
|
extern void mie_builder_put_data(struct mie_builder *builder, struct mie_data *data);
|
||||||
extern void mie_builder_put_type(struct mie_builder *builder, struct mie_type *type);
|
extern void mie_builder_put_type(struct mie_builder *builder, struct mie_type *type);
|
||||||
extern void mie_builder_set_insert_point(
|
extern void mie_builder_set_insert_point(
|
||||||
|
|||||||
@@ -3,21 +3,52 @@
|
|||||||
|
|
||||||
#include <mie/value.h>
|
#include <mie/value.h>
|
||||||
|
|
||||||
#define MIE_CONST(p) ((struct mie_const *)(p))
|
#define MIE_CONST(p) ((struct mie_const *)(p))
|
||||||
|
|
||||||
|
#define MIE_INT(p) ((struct mie_int *)(p))
|
||||||
|
#define MIE_DOUBLE(p) ((struct mie_double *)(p))
|
||||||
|
#define MIE_STRING(p) ((struct mie_string *)(p))
|
||||||
|
#define MIE_ATOM(p) ((struct mie_atom *)(p))
|
||||||
|
#define MIE_SELECTOR(p) ((struct mie_selector *)(p))
|
||||||
|
#define MIE_ARRAY(p) ((struct mie_array *)(p))
|
||||||
|
|
||||||
|
struct b_list;
|
||||||
|
|
||||||
struct mie_const {
|
struct mie_const {
|
||||||
struct mie_value c_base;
|
struct mie_value c_base;
|
||||||
struct mie_type *c_type;
|
struct mie_type *c_type;
|
||||||
|
|
||||||
union {
|
|
||||||
int64_t v_int;
|
|
||||||
double v_float;
|
|
||||||
char *v_str;
|
|
||||||
char *v_atom;
|
|
||||||
char *v_selector;
|
|
||||||
} c_v;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct mie_const *mie_const_create(struct mie_type *type);
|
struct mie_int {
|
||||||
|
struct mie_const i_base;
|
||||||
|
int64_t i_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_double {
|
||||||
|
struct mie_const d_base;
|
||||||
|
double d_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_string {
|
||||||
|
struct mie_const s_base;
|
||||||
|
char *s_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_atom {
|
||||||
|
struct mie_const a_base;
|
||||||
|
char *a_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_selector {
|
||||||
|
struct mie_const sel_base;
|
||||||
|
char *sel_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mie_array {
|
||||||
|
struct mie_const a_base;
|
||||||
|
struct b_list *a_values;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void mie_const_init(struct mie_const *c, struct mie_type *type);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -24,5 +24,6 @@ extern struct mie_value *mie_ctx_get_int(
|
|||||||
struct mie_ctx *ctx, long long val, unsigned int nr_bits);
|
struct mie_ctx *ctx, long long val, unsigned int nr_bits);
|
||||||
extern struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s);
|
extern struct mie_value *mie_ctx_get_string(struct mie_ctx *ctx, const char *s);
|
||||||
extern struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel);
|
extern struct mie_value *mie_ctx_get_selector(struct mie_ctx *ctx, const char *sel);
|
||||||
|
extern struct mie_value *mie_ctx_create_array(struct mie_ctx *ctx);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -95,7 +95,8 @@ enum b_status mie_module_put_data(
|
|||||||
|
|
||||||
struct mie_const *const_data = data->d_const.c_value;
|
struct mie_const *const_data = data->d_const.c_value;
|
||||||
if (const_data->c_type->t_id == MIE_TYPE_STR) {
|
if (const_data->c_type->t_id == MIE_TYPE_STR) {
|
||||||
key.key_data = const_data->c_v.v_str;
|
struct mie_string *s = MIE_STRING(const_data);
|
||||||
|
key.key_data = s->s_value;
|
||||||
key.key_size = strlen(key.key_data);
|
key.key_size = strlen(key.key_data);
|
||||||
b_hashmap_put(mod->m_data_strings, &key, &value);
|
b_hashmap_put(mod->m_data_strings, &key, &value);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user