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:
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();
|
||||
Reference in New Issue
Block a user