Esempio n. 1
0
File: ast.c Progetto: sysdb/sysdb
static void
value_destroy(sdb_object_t *obj)
{
	sdb_ast_value_t *value = SDB_AST_VALUE(obj);
	if (value->name)
		free(value->name);
	value->name = NULL;
} /* value_destroy */
Esempio n. 2
0
File: ast.c Progetto: sysdb/sysdb
sdb_ast_node_t *
sdb_ast_value_create(int type, char *name)
{
	sdb_ast_value_t *value;
	value = SDB_AST_VALUE(sdb_object_create("VALUE", value_type));
	if (! value)
		return NULL;

	value->super.type = SDB_AST_TYPE_VALUE;

	value->type = type;
	value->name = name;
	return SDB_AST_NODE(value);
} /* sdb_ast_value_create */
Esempio n. 3
0
static int
analyze_iter(int context, sdb_ast_iter_t *iter, sdb_strbuf_t *errbuf)
{
	sdb_ast_const_t c = SDB_AST_CONST_INIT;
	int iter_context = context;
	int status;

	if (iter->iter->type == SDB_AST_TYPE_TYPED)
		iter_context = SDB_AST_TYPED(iter->iter)->type;

	if (analyze_node(iter_context, iter->iter, errbuf))
		return -1;
	/* TODO: support other setups as well */
	assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
			&& (! SDB_AST_OP(iter->expr)->left));
	/* determine the data-type for better error messages */
	analyze_node(iter_context, SDB_AST_OP(iter->expr)->right, NULL);

	if (iter->iter->type == SDB_AST_TYPE_TYPED) {
		int iter_type = SDB_AST_TYPED(iter->iter)->type;

		c.value.type = iter->iter->data_type;

		if (iter_type == SDB_ATTRIBUTE) {
			/* attributes are always iterable */
		}
		else if ((context != SDB_HOST) && (context != SDB_SERVICE)
				&& (context != SDB_METRIC) && (context != UNSPEC_CONTEXT)) {
			iter_error(errbuf, iter, "%s not iterable in %s context",
					SDB_STORE_TYPE_TO_NAME(iter_type),
					SDB_STORE_TYPE_TO_NAME(context));
			return -1;
		}

		if ((context == iter_type)
				|| ((iter_type != SDB_SERVICE)
					&& (iter_type != SDB_METRIC)
					&& (iter_type != SDB_ATTRIBUTE))
				|| ((context == SDB_SERVICE)
					&& (iter_type == SDB_METRIC))
				|| ((context == SDB_METRIC)
					&& (iter_type == SDB_SERVICE))) {
			iter_error(errbuf, iter, "%s not iterable in %s context",
					SDB_STORE_TYPE_TO_NAME(iter_type),
					SDB_STORE_TYPE_TO_NAME(context));
			return -1;
		}
	}
	else if (iter->iter->type == SDB_AST_TYPE_VALUE) {
		int iter_type = SDB_AST_VALUE(iter->iter)->type;

		c.value.type = iter->iter->data_type & 0xff;

		if (iter_type != SDB_FIELD_BACKEND) {
			iter_error(errbuf, iter, "%s not iterable in %s context",
					(iter_type == SDB_ATTRIBUTE)
						? "attribute"
						: SDB_FIELD_TO_NAME(iter_type),
					SDB_STORE_TYPE_TO_NAME(context));
			return -1;
		}
	}
	else if (iter->iter->type == SDB_AST_TYPE_CONST) {
		c.value.type = iter->iter->data_type & 0xff;

		if (! (SDB_AST_CONST(iter->iter)->value.type & SDB_TYPE_ARRAY)) {
			iter_error(errbuf, iter, "%s not iterable",
					SDB_TYPE_TO_STRING(SDB_AST_CONST(iter->iter)->value.type));
			return -1;
		}
	}
	else {
		/* TODO: if we know the data-type of iter->iter and it's an array,
		 * we should support an iterator for it as well */
		iter_error(errbuf, iter, "%s expression not iterable",
				SDB_AST_TYPE_TO_STRING(iter->iter));
		return -1;
	}

	SDB_AST_OP(iter->expr)->left = SDB_AST_NODE(&c);
	status = analyze_node(context, iter->expr, errbuf);
	SDB_AST_OP(iter->expr)->left = NULL;
	if (status)
		return -1;
	return 0;
} /* analyze_iter */