104 lines
2.4 KiB
C
104 lines
2.4 KiB
C
#include <mie/attribute/attribute-definition.h>
|
|
#include <mie/attribute/attribute.h>
|
|
#include <mie/ctx.h>
|
|
#include <mie/dialect/builtin.h>
|
|
#include <mie/dialect/dialect.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_array *array = (const struct mie_array *)value;
|
|
|
|
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
|
b_stream_write_string(out->p_stream, "#builtin.array<", NULL);
|
|
}
|
|
|
|
b_stream_write_char(out->p_stream, '[');
|
|
|
|
for (size_t i = 0; i < MIE_VECTOR_COUNT(array->a_items); i++) {
|
|
if (i > 0) {
|
|
b_stream_write_char(out->p_stream, ',');
|
|
}
|
|
|
|
b_stream_write_char(out->p_stream, ' ');
|
|
mie_printer_print_attribute(out, array->a_items.items[i]);
|
|
}
|
|
|
|
if (MIE_VECTOR_COUNT(array->a_items) != 0) {
|
|
b_stream_write_char(out->p_stream, ' ');
|
|
}
|
|
b_stream_write_char(out->p_stream, ']');
|
|
|
|
if (!(out->p_flags & MIE_PRINT_F_ABBREVIATED)) {
|
|
b_stream_write_char(out->p_stream, '>');
|
|
}
|
|
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
static struct mie_array *array_create(struct mie_ctx *ctx)
|
|
{
|
|
struct mie_array *array = malloc(sizeof *array);
|
|
if (!array) {
|
|
return NULL;
|
|
}
|
|
|
|
memset(array, 0x0, sizeof *array);
|
|
|
|
array->a_base.a_def
|
|
= mie_ctx_get_attribute_definition(ctx, "builtin", "array");
|
|
|
|
return array;
|
|
}
|
|
|
|
static enum mie_status parse(
|
|
struct mie_parser *ctx, const struct mie_attribute **out)
|
|
{
|
|
if (!mie_parser_parse_symbol(ctx, MIE_SYM_LEFT_BRACKET)) {
|
|
return MIE_ERR_BAD_SYNTAX;
|
|
}
|
|
|
|
struct mie_array *array = array_create(mie_parser_get_mie_ctx(ctx));
|
|
if (!array) {
|
|
return MIE_ERR_NO_MEMORY;
|
|
}
|
|
|
|
const struct mie_attribute *item = NULL;
|
|
|
|
if (!mie_parser_parse_attribute(ctx, &item)) {
|
|
free(array);
|
|
return MIE_ERR_BAD_FORMAT;
|
|
}
|
|
|
|
mie_vector_push_back(array->a_items, &item, NULL);
|
|
|
|
while (1) {
|
|
if (mie_parser_parse_symbol(ctx, MIE_SYM_RIGHT_BRACKET)) {
|
|
break;
|
|
}
|
|
|
|
if (!mie_parser_parse_symbol(ctx, MIE_SYM_COMMA)) {
|
|
return false;
|
|
}
|
|
|
|
if (!mie_parser_parse_attribute(ctx, &item)) {
|
|
free(array);
|
|
return MIE_ERR_BAD_FORMAT;
|
|
}
|
|
|
|
mie_vector_push_back(array->a_items, &item, NULL);
|
|
}
|
|
|
|
*out = (struct mie_attribute *)array;
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
MIE_ATTRIBUTE_DEFINITION_BEGIN(mie_builtin_array, "array")
|
|
MIE_ATTRIBUTE_DEFINITION_STRUCT(struct mie_array);
|
|
MIE_ATTRIBUTE_DEFINITION_PRINT(print);
|
|
MIE_ATTRIBUTE_DEFINITION_PARSE(parse);
|
|
MIE_ATTRIBUTE_DEFINITION_END()
|