mie: replace fixed value system with an extensible attribute interface
under this new system, dialects can define their own custom attributes, complete with their own print() and parse() callbacks, which can then be used as values in an op's attribute dictionary. alongside custom dialect attributes, the former int, float, and string constant values have been converted to attributes provided by the arith and builtin dialects respectively. the caches for these attributes have also been moved from mie_ctx to their respective dialect data structures. this system will allow new types of attributes to be implemented, including dictionaries, arrays, and references to types themselves (rather than just particular values of a given type).
This commit is contained in:
0
mie/dialect/builtin/attribute/array.c
Normal file
0
mie/dialect/builtin/attribute/array.c
Normal file
47
mie/dialect/builtin/attribute/string.c
Normal file
47
mie/dialect/builtin/attribute/string.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <mie/attribute/attribute-definition.h>
|
||||
#include <mie/attribute/attribute.h>
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/macros.h>
|
||||
#include <mie/parse/parser.h>
|
||||
#include <mie/print/printer.h>
|
||||
|
||||
static enum mie_status print(
|
||||
const struct mie_attribute *value, struct mie_printer *out)
|
||||
{
|
||||
const struct mie_string *str = (const struct mie_string *)value;
|
||||
if (out->p_flags & MIE_PRINT_F_ABBREVIATED) {
|
||||
b_stream_write_fmt(out->p_stream, NULL, "\"%s\"", str->str_val);
|
||||
} else {
|
||||
b_stream_write_fmt(
|
||||
out->p_stream, NULL, "#builtin.string<\"%s\">",
|
||||
str->str_val);
|
||||
}
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
struct mie_parser *ctx, const struct mie_attribute **out)
|
||||
{
|
||||
b_string *str = mie_parser_get_tempstr(ctx);
|
||||
struct mie_file_span span;
|
||||
if (!mie_parser_parse_string(ctx, str, &span)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mie_attribute *v = mie_ctx_get_string(
|
||||
mie_parser_get_mie_ctx(ctx), b_string_ptr(str));
|
||||
|
||||
if (!v) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_string, "string")
|
||||
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_string);
|
||||
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
||||
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
||||
MIE_ATTRIBUTE_DEFINITION_END();
|
||||
0
mie/dialect/builtin/attribute/type.c
Normal file
0
mie/dialect/builtin/attribute/type.c
Normal file
@@ -1,7 +1,43 @@
|
||||
#include <mie/ctx.h>
|
||||
#include <mie/dialect/builtin.h>
|
||||
#include <mie/dialect/dialect.h>
|
||||
#include <mie/macros.h>
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, "builtin")
|
||||
struct builtin_dialect {
|
||||
struct mie_dialect d_base;
|
||||
struct mie_string_cache *ctx_strings;
|
||||
};
|
||||
|
||||
static enum mie_status init(struct mie_dialect *d)
|
||||
{
|
||||
struct builtin_dialect *dialect = (struct builtin_dialect *)d;
|
||||
dialect->ctx_strings = mie_string_cache_create();
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status cleanup(struct mie_dialect *d)
|
||||
{
|
||||
struct builtin_dialect *dialect = (struct builtin_dialect *)d;
|
||||
mie_string_cache_destroy(dialect->ctx_strings);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
struct mie_attribute *mie_ctx_get_string(struct mie_ctx *ctx, const char *s)
|
||||
{
|
||||
struct builtin_dialect *dialect
|
||||
= (struct builtin_dialect *)mie_ctx_get_dialect(ctx, "builtin");
|
||||
if (!dialect) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (struct mie_attribute *)mie_string_cache_get(
|
||||
dialect->ctx_strings, ctx, s);
|
||||
}
|
||||
|
||||
MIE_DIALECT_BEGIN(mie_builtin, struct builtin_dialect, "builtin")
|
||||
MIE_DIALECT_INIT(init);
|
||||
MIE_DIALECT_CLEANUP(cleanup);
|
||||
MIE_DIALECT_ADD_TYPE(mie_builtin_string);
|
||||
MIE_DIALECT_ADD_ATTRIBUTE(mie_builtin_string);
|
||||
MIE_DIALECT_ADD_TRAIT(mie_builtin_isolated_from_above);
|
||||
MIE_DIALECT_END()
|
||||
|
||||
@@ -17,12 +17,8 @@ static struct mie_string *mie_string_create(
|
||||
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_base.a_def
|
||||
= mie_ctx_get_attribute_definition(ctx, "builtin", "string");
|
||||
out->str_val = b_strdup(s);
|
||||
if (!out->str_val) {
|
||||
return NULL;
|
||||
|
||||
@@ -6,15 +6,6 @@
|
||||
#include <mie/print/printer.h>
|
||||
#include <mie/type/type-definition.h>
|
||||
#include <mie/type/type.h>
|
||||
#include <mie/value.h>
|
||||
|
||||
static enum mie_status value_print(
|
||||
const struct mie_value *value, struct mie_printer *out)
|
||||
{
|
||||
const struct mie_string *str = (const struct mie_string *)value;
|
||||
b_stream_write_fmt(out->p_stream, NULL, "\"%s\"", str->str_val);
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static void type_init(
|
||||
const struct mie_type_definition *type_info, struct mie_type *type)
|
||||
@@ -27,17 +18,8 @@ static enum mie_status print(const struct mie_type *ty, struct mie_printer *out)
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
static enum mie_status parse(
|
||||
const struct mie_type_definition *def, struct mie_parser *parser,
|
||||
struct mie_type **out)
|
||||
{
|
||||
return MIE_SUCCESS;
|
||||
}
|
||||
|
||||
MIE_TYPE_DEFINITION_BEGIN(mie_builtin_string, "string")
|
||||
MIE_TYPE_DEFINITION_STRUCT(struct mie_type);
|
||||
MIE_TYPE_DEFINITION_INIT(type_init);
|
||||
MIE_TYPE_DEFINITION_PRINT(print);
|
||||
MIE_TYPE_DEFINITION_PARSE(parse);
|
||||
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
||||
MIE_TYPE_DEFINITION_END()
|
||||
Reference in New Issue
Block a user