mie: builtin: add string type
This commit is contained in:
@@ -2,4 +2,5 @@
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, "builtin")
|
||||
MIE_DIALECT_ADD_TYPE(mie_builtin_string);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
84
mie/dialect/builtin/string-cache.c
Normal file
84
mie/dialect/builtin/string-cache.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/ds/hashmap.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mie_string_cache {
|
||||
b_hashmap *c_entries;
|
||||
};
|
||||
|
||||
static struct mie_string *mie_string_create(
|
||||
struct mie_ctx *ctx, const char *s, size_t len)
|
||||
{
|
||||
struct mie_string *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_base.v_type = mie_ctx_get_type(ctx, "builtin", "string");
|
||||
if (!out->str_base.v_type) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_val = b_strdup(s);
|
||||
if (!out->str_val) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out->str_len = len;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static void mie_string_destroy(struct mie_string *str)
|
||||
{
|
||||
}
|
||||
|
||||
struct mie_string_cache *mie_string_cache_create(void)
|
||||
{
|
||||
struct mie_string_cache *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->c_entries = b_hashmap_create(
|
||||
NULL, (b_hashmap_value_destructor)mie_string_destroy);
|
||||
if (!out->c_entries) {
|
||||
free(out);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void mie_string_cache_destroy(struct mie_string_cache *cache)
|
||||
{
|
||||
b_hashmap_unref(cache->c_entries);
|
||||
free(cache);
|
||||
}
|
||||
|
||||
struct mie_string *mie_string_cache_get(
|
||||
struct mie_string_cache *cache, struct mie_ctx *ctx, const char *s)
|
||||
{
|
||||
size_t s_len = strlen(s);
|
||||
b_hashmap_key key = {.key_data = s, .key_size = s_len};
|
||||
const b_hashmap_value *value = b_hashmap_get(cache->c_entries, &key);
|
||||
if (value) {
|
||||
return value->value_data;
|
||||
}
|
||||
|
||||
struct mie_string *str = mie_string_create(ctx, s, s_len);
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
b_hashmap_value new_val = {.value_data = str, .value_size = sizeof *str};
|
||||
b_hashmap_put(cache->c_entries, &key, &new_val);
|
||||
|
||||
return str;
|
||||
}
|
||||
41
mie/dialect/builtin/string.c
Normal file
41
mie/dialect/builtin/string.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <blue/core/bstr.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/dialect/type.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
static void value_print(
|
||||
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
||||
{
|
||||
const struct mie_string *str = (const struct mie_string *)value;
|
||||
b_stream_write_fmt(out, NULL, "\"%s\"", str->str_val);
|
||||
}
|
||||
|
||||
static void type_init(const struct mie_dialect_type *type_info, struct mie_type *type)
|
||||
{
|
||||
type->ty_instance_size = sizeof(struct mie_string);
|
||||
type->ty_value_print = value_print;
|
||||
}
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_dialect_type *def, const struct mie_type *ty, b_stream *out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_dialect_type *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_DIALECT_TYPE_BEGIN(mie_builtin_string, "string")
|
||||
MIE_DIALECT_TYPE_STRUCT(struct mie_type);
|
||||
MIE_DIALECT_TYPE_INIT(type_init);
|
||||
MIE_DIALECT_TYPE_PRINT(print);
|
||||
MIE_DIALECT_TYPE_PARSE(parse);
|
||||
MIE_DIALECT_TYPE_END()
|
||||
@@ -1,7 +1,9 @@
|
||||
#ifndef MIE_DIALECT_BUILTIN_H_
|
||||
#define MIE_DIALECT_BUILTIN_H_
|
||||
|
||||
#include <blue/ds/string.h>
|
||||
#include <mie/misc.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
struct mie_dialect;
|
||||
|
||||
@@ -9,6 +11,19 @@ struct mie_ctx;
|
||||
struct mie_int_type;
|
||||
struct mie_float_type;
|
||||
|
||||
struct mie_string {
|
||||
struct mie_value str_base;
|
||||
char *str_val;
|
||||
size_t str_len;
|
||||
};
|
||||
|
||||
struct mie_string_cache;
|
||||
|
||||
MIE_API struct mie_dialect *mie_builtin_dialect_create(struct mie_ctx *ctx);
|
||||
|
||||
MIE_API struct mie_string_cache *mie_string_cache_create(void);
|
||||
MIE_API void mie_string_cache_destroy(struct mie_string_cache *cache);
|
||||
MIE_API struct mie_string *mie_string_cache_get(
|
||||
struct mie_string_cache *cache, struct mie_ctx *ctx, const char *val);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user