+ if (!ast_block_exprs_add(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_exprs_add(block, (ast_expression*)ret)) {
+ ast_delete(block);
+ return NULL;
+ }
+
+ return (ast_expression*)block;
+ } else {
+ ast_ifthen *ifthen;
+ ast_expression *left, *right;
+ ast_binary *cmp;
+
+ 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);
+ 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, from + diff/2));
+ 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_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_ifthen *ifthen;
+ ast_expression *left, *right;
+ ast_binary *cmp;
+
+ 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);
+ 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, from + diff/2));
+ 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 bool parser_create_array_setter(parser_t *parser, ast_value *array, const char *funcname)
+{
+ ast_expression *root = NULL;
+ ast_function *func = NULL;
+ ast_value *fval = NULL;
+ ast_block *body;
+ ast_value *index, *value;
+
+ 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;
+ }
+
+ body = ast_block_new(ast_ctx(array));
+ if (!body) {
+ parseerror(parser, "failed to create block for array accessor");
+ return false;
+ }
+
+ index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
+ value = ast_value_copy((ast_value*)array->expression.next);
+
+ if (!index || !value) {
+ ast_delete(body);
+ if (index) ast_delete(index);
+ if (value) ast_delete(value);
+ parseerror(parser, "failed to create locals for array accessor");
+ return false;
+ }
+
+ root = array_setter_node(parser, array, index, value, 0, array->expression.count);
+ if (!root) {
+ parseerror(parser, "failed to build accessor search tree");
+ goto cleanup;
+ }
+
+ if (!ast_block_exprs_add(body, root)) {
+ parseerror(parser, "failed to build accessor search block");
+ goto cleanup;
+ }
+
+ fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
+ if (!fval) {
+ parseerror(parser, "failed to create accessor function value");
+ goto cleanup;
+ }
+ fval->expression.next = (ast_expression*)ast_value_new(ast_ctx(array), "<void>", TYPE_VOID);
+
+ (void)!ast_value_set_name(value, "value"); /* not important */
+ if (!ast_expression_common_params_add(&fval->expression, index)) {
+ parseerror(parser, "failed to build array setter");
+ goto cleanup;
+ }
+ if (!ast_expression_common_params_add(&fval->expression, value)) {
+ ast_delete(index);
+ parseerror(parser, "failed to build array setter");
+ goto cleanup2;
+ }
+
+ func = ast_function_new(ast_ctx(array), funcname, fval);
+ if (!func) {
+ parseerror(parser, "failed to create accessor function node");
+ goto cleanup2;
+ }
+
+ if (!ast_function_blocks_add(func, body))
+ goto cleanup2;
+
+ array->setter = fval;
+
+ return true;
+cleanup:
+ ast_delete(index);
+ ast_delete(value);
+cleanup2:
+ ast_delete(body);
+ if (root) ast_delete(root);
+ if (func) ast_delete(func);
+ if (fval) ast_delete(fval);
+ return false;
+}
+
+static bool parser_create_array_getter(parser_t *parser, ast_value *array, const char *funcname)
+{
+ ast_expression *root = NULL;
+ ast_function *func = NULL;
+ ast_value *fval = NULL;
+ ast_block *body;
+ ast_value *index;
+
+ 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;
+ }
+
+ body = ast_block_new(ast_ctx(array));
+ if (!body) {
+ parseerror(parser, "failed to create block for array accessor");
+ return false;
+ }
+
+ index = ast_value_new(ast_ctx(array), "index", TYPE_FLOAT);
+
+ if (!index) {
+ ast_delete(body);
+ if (index) ast_delete(index);
+ parseerror(parser, "failed to create locals for array accessor");
+ return false;
+ }
+
+ root = array_getter_node(parser, array, index, 0, array->expression.count);
+ if (!root) {
+ parseerror(parser, "failed to build accessor search tree");
+ goto cleanup;
+ }
+
+ if (!ast_block_exprs_add(body, root)) {
+ parseerror(parser, "failed to build accessor search block");
+ goto cleanup;
+ }
+
+ fval = ast_value_new(ast_ctx(array), funcname, TYPE_FUNCTION);
+ if (!fval) {
+ parseerror(parser, "failed to create accessor function value");
+ goto cleanup;
+ }
+ fval->expression.next = ast_type_copy(ast_ctx(array), array->expression.next);
+
+ if (!ast_expression_common_params_add(&fval->expression, index)) {
+ parseerror(parser, "failed to build array setter");
+ goto cleanup;
+ }
+
+ func = ast_function_new(ast_ctx(array), funcname, fval);
+ if (!func) {
+ parseerror(parser, "failed to create accessor function node");
+ goto cleanup2;
+ }
+
+ if (!ast_function_blocks_add(func, body))
+ goto cleanup2;
+
+ array->getter = fval;
+
+ return true;
+cleanup:
+ ast_delete(index);
+cleanup2:
+ ast_delete(body);
+ if (root) ast_delete(root);
+ if (func) ast_delete(func);
+ if (fval) ast_delete(fval);
+ return false;
+}
+
+typedef struct {
+ MEM_VECTOR_MAKE(ast_value*, p);
+} paramlist_t;
+MEM_VEC_FUNCTIONS(paramlist_t, ast_value*, p)
+
+static ast_value *parse_typename(parser_t *parser, ast_value **storebase);
+static ast_value *parse_parameter_list(parser_t *parser, ast_value *var)
+{
+ lex_ctx ctx;
+ size_t i;
+ paramlist_t params;
+ ast_value *param;
+ ast_value *fval;
+ bool first = true;
+ bool variadic = false;
+
+ ctx = parser_ctx(parser);
+
+ /* for the sake of less code we parse-in in this function */
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected parameter list");
+ return NULL;
+ }
+
+ MEM_VECTOR_INIT(¶ms, p);
+
+ /* parse variables until we hit a closing paren */
+ while (parser->tok != ')') {
+ if (!first) {
+ /* there must be commas between them */
+ if (parser->tok != ',') {
+ parseerror(parser, "expected comma or end of parameter list");
+ goto on_error;