+ if (!create_vector_members(param, me)) {
+ ast_block_delete(block);
+ return false;
+ }
+
+ for (e = 0; e < 3; ++e) {
+ parser_addlocal(parser, me[e]->name, (ast_expression*)me[e]);
+ ast_block_collect(block, (ast_expression*)me[e]);
+ }
+ }
+
+ func = ast_function_new(ast_ctx(var), var->name, var);
+ if (!func) {
+ parseerror(parser, "failed to allocate function for `%s`", var->name);
+ ast_block_delete(block);
+ goto enderr;
+ }
+ vec_push(parser->functions, func);
+
+ parser->function = func;
+ if (!parse_block_into(parser, block)) {
+ ast_block_delete(block);
+ goto enderrfn;
+ }
+
+ vec_push(func->blocks, block);
+
+ parser->function = old;
+ if (!parser_leaveblock(parser))
+ retval = false;
+ if (vec_size(parser->variables) != PARSER_HT_LOCALS) {
+ parseerror(parser, "internal error: local scopes left");
+ retval = false;
+ }
+
+ if (parser->tok == ';')
+ return parser_next(parser);
+ else if (opts.standard == COMPILER_QCC)
+ parseerror(parser, "missing semicolon after function body (mandatory with -std=qcc)");
+ return retval;
+
+enderrfn:
+ vec_pop(parser->functions);
+ ast_function_delete(func);
+ var->constval.vfunc = NULL;
+
+enderr:
+ (void)!parser_leaveblock(parser);
+ parser->function = old;
+ return false;
+}
+
+static ast_expression *array_accessor_split(
+ parser_t *parser,
+ ast_value *array,
+ ast_value *index,
+ size_t middle,
+ ast_expression *left,
+ ast_expression *right
+ )
+{
+ ast_ifthen *ifthen;
+ ast_binary *cmp;
+
+ lex_ctx ctx = ast_ctx(array);
+
+ if (!left || !right) {
+ if (left) ast_delete(left);
+ if (right) ast_delete(right);
+ return NULL;
+ }
+
+ cmp = ast_binary_new(ctx, INSTR_LT,
+ (ast_expression*)index,
+ (ast_expression*)parser_const_float(parser, middle));
+ if (!cmp) {
+ ast_delete(left);
+ ast_delete(right);
+ parseerror(parser, "internal error: failed to create comparison for array setter");
+ return NULL;
+ }
+
+ ifthen = ast_ifthen_new(ctx, (ast_expression*)cmp, left, right);
+ if (!ifthen) {
+ ast_delete(cmp); /* will delete left and right */
+ parseerror(parser, "internal error: failed to create conditional jump for array setter");
+ return NULL;
+ }
+
+ return (ast_expression*)ifthen;
+}
+
+static ast_expression *array_setter_node(parser_t *parser, ast_value *array, ast_value *index, ast_value *value, size_t from, size_t afterend)
+{
+ lex_ctx ctx = ast_ctx(array);
+
+ if (from+1 == afterend) {
+ /* set this value */
+ ast_block *block;
+ ast_return *ret;
+ ast_array_index *subscript;
+ ast_store *st;
+ int assignop = type_store_instr[value->expression.vtype];
+
+ if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
+ assignop = INSTR_STORE_V;
+
+ subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
+ if (!subscript)
+ return NULL;
+
+ st = ast_store_new(ctx, assignop, (ast_expression*)subscript, (ast_expression*)value);
+ if (!st) {
+ ast_delete(subscript);
+ return NULL;
+ }
+
+ block = ast_block_new(ctx);
+ if (!block) {
+ ast_delete(st);
+ return NULL;
+ }
+
+ if (!ast_block_add_expr(block, (ast_expression*)st)) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ ret = ast_return_new(ctx, NULL);
+ if (!ret) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ if (!ast_block_add_expr(block, (ast_expression*)ret)) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ return (ast_expression*)block;
+ } else {
+ ast_expression *left, *right;
+ size_t diff = afterend - from;
+ size_t middle = from + diff/2;
+ left = array_setter_node(parser, array, index, value, from, middle);
+ right = array_setter_node(parser, array, index, value, middle, afterend);
+ return array_accessor_split(parser, array, index, middle, left, right);
+ }
+}
+
+static ast_expression *array_field_setter_node(
+ parser_t *parser,
+ ast_value *array,
+ ast_value *entity,
+ ast_value *index,
+ ast_value *value,
+ size_t from,
+ size_t afterend)
+{
+ lex_ctx ctx = ast_ctx(array);
+
+ if (from+1 == afterend) {
+ /* set this value */
+ ast_block *block;
+ ast_return *ret;
+ ast_entfield *entfield;
+ ast_array_index *subscript;
+ ast_store *st;
+ int assignop = type_storep_instr[value->expression.vtype];
+
+ if (value->expression.vtype == TYPE_FIELD && value->expression.next->expression.vtype == TYPE_VECTOR)
+ assignop = INSTR_STOREP_V;
+
+ subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
+ if (!subscript)
+ return NULL;
+
+ entfield = ast_entfield_new_force(ctx,
+ (ast_expression*)entity,
+ (ast_expression*)subscript,
+ (ast_expression*)subscript);
+ if (!entfield) {
+ ast_delete(subscript);
+ return NULL;
+ }
+
+ st = ast_store_new(ctx, assignop, (ast_expression*)entfield, (ast_expression*)value);
+ if (!st) {
+ ast_delete(entfield);
+ return NULL;
+ }
+
+ block = ast_block_new(ctx);
+ if (!block) {
+ ast_delete(st);
+ return NULL;
+ }
+
+ if (!ast_block_add_expr(block, (ast_expression*)st)) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ ret = ast_return_new(ctx, NULL);
+ if (!ret) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ if (!ast_block_add_expr(block, (ast_expression*)ret)) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ return (ast_expression*)block;
+ } else {
+ ast_expression *left, *right;
+ size_t diff = afterend - from;
+ size_t middle = from + diff/2;
+ left = array_field_setter_node(parser, array, entity, index, value, from, middle);
+ right = array_field_setter_node(parser, array, entity, index, value, middle, afterend);
+ return array_accessor_split(parser, array, index, middle, left, right);
+ }
+}
+
+static ast_expression *array_getter_node(parser_t *parser, ast_value *array, ast_value *index, size_t from, size_t afterend)
+{
+ lex_ctx ctx = ast_ctx(array);
+
+ if (from+1 == afterend) {
+ ast_return *ret;
+ ast_array_index *subscript;
+
+ subscript = ast_array_index_new(ctx, (ast_expression*)array, (ast_expression*)parser_const_float(parser, from));
+ if (!subscript)
+ return NULL;
+
+ ret = ast_return_new(ctx, (ast_expression*)subscript);
+ if (!ret) {
+ ast_delete(subscript);
+ return NULL;
+ }
+
+ return (ast_expression*)ret;
+ } else {
+ ast_expression *left, *right;
+ size_t diff = afterend - from;
+ size_t middle = from + diff/2;
+ left = array_getter_node(parser, array, index, from, middle);
+ right = array_getter_node(parser, array, index, middle, afterend);
+ return array_accessor_split(parser, array, index, middle, left, right);
+ }
+}
+
+static bool parser_create_array_accessor(parser_t *parser, ast_value *array, const char *funcname, ast_value **out)
+{
+ ast_function *func = NULL;
+ ast_value *fval = NULL;
+ ast_block *body = NULL;
+
+ fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
+ if (!fval) {
+ parseerror(parser, "failed to create accessor function value");
+ return false;
+ }
+
+ func = ast_function_new(ast_ctx(array), funcname, fval);
+ if (!func) {
+ ast_delete(fval);
+ parseerror(parser, "failed to create accessor function node");
+ return false;
+ }
+
+ body = ast_block_new(ast_ctx(array));
+ if (!body) {
+ parseerror(parser, "failed to create block for array accessor");
+ ast_delete(fval);
+ ast_delete(func);
+ return false;
+ }
+
+ vec_push(func->blocks, body);
+ *out = fval;
+
+ vec_push(parser->accessors, fval);
+
+ return true;
+}
+
+static bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
+{
+ ast_expression *root = NULL;
+ ast_value *index = NULL;
+ ast_value *value = NULL;
+ ast_function *func;
+ ast_value *fval;
+
+ if (!ast_istype(array->expression.next, ast_value)) {
+ parseerror(parser, "internal error: array accessor needs to build an ast_value with a copy of the element type");
+ return false;
+ }
+
+ if (!parser_create_array_accessor(parser, array, funcname, &fval))
+ return false;
+ func = fval->constval.vfunc;
+ fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
+
+ index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
+ value = ast_value_copy((ast_value*)array->expression.next);
+
+ if (!index || !value) {
+ parseerror(parser, "failed to create locals for array accessor");
+ goto cleanup;
+ }
+ (void)!ast_value_set_name(value, "value"); /* not important */
+ vec_push(fval->expression.params, index);
+ vec_push(fval->expression.params, value);
+
+ root = array_setter_node(parser, array, index, value, 0, array->expression.count);
+ if (!root) {
+ parseerror(parser, "failed to build accessor search tree");
+ goto cleanup;
+ }
+
+ array->setter = fval;
+ return ast_block_add_expr(func->blocks[0], root);
+cleanup:
+ if (index) ast_delete(index);
+ if (value) ast_delete(value);
+ if (root) ast_delete(root);
+ ast_delete(func);
+ ast_delete(fval);
+ return false;
+}
+
+static bool parser_create_array_field_setter(parser_t *parser, ast_value *array, const char *funcname)
+{
+ ast_expression *root = NULL;
+ ast_value *entity = NULL;
+ ast_value *index = NULL;
+ ast_value *value = NULL;
+ ast_function *func;
+ ast_value *fval;