+
+/* AST iterator + iterator functions */
+void ast_iterator_begin(ast_iterator *iter, ast_node *start) {
+ iter->head = start;
+ iter->at = &iter->head;
+ iter->path = NULL;
+ vec_push(iter->path, iter->at);
+}
+
+void ast_iterator_delete(ast_iterator *iter) {
+ if (iter->path)
+ vec_free(iter->path);
+}
+
+ast_node *ast_iterator_next(ast_iterator *iter) {
+ size_t depth = vec_size(iter->path);
+ while (depth) {
+ ast_node **last = vec_last(iter->path);
+ ast_node **next = ast_next_child(*last, iter->at);
+ if (next) {
+ vec_push(iter->path, next);
+ iter->at = next;
+ return *next;
+ }
+ iter->at = last;
+ vec_pop(iter->path);
+ --depth;
+ }
+ return NULL;
+}
+
+static ast_node** ast_member_next_child(ast_member *self, ast_node **cur) {
+ (void)self; (void)cur;
+ return NULL;
+}
+
+static ast_node** ast_value_next_child(ast_value *self, ast_node **cur) {
+ (void)self; (void)cur;
+ return NULL;
+}
+
+static ast_node** ast_array_index_next_child(ast_array_index *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->array;
+ if (cur == (ast_node**)&self->array)
+ return (ast_node**)&self->index;
+ return NULL;
+}
+
+static ast_node** ast_argpipe_next_child(ast_argpipe *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->index;
+ return NULL;
+}
+
+static ast_node** ast_store_next_child(ast_store *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->dest;
+ if (cur == (ast_node**)&self->dest)
+ return (ast_node**)&self->source;
+ return NULL;
+}
+
+static ast_node** ast_ifthen_next_child(ast_ifthen *self, ast_node **cur) {
+ if (*cur == (ast_node*)self) {
+ if (self->cond) return (ast_node**)&self->cond;
+ if (self->on_true) return (ast_node**)&self->on_true;
+ return (ast_node**)&self->on_false;
+ }
+ if (self->cond && cur == (ast_node**)&self->cond) {
+ if (self->on_true) return (ast_node**)&self->on_true;
+ return (ast_node**)&self->on_false;
+ }
+ if (self->on_true && cur == (ast_node**)&self->on_true)
+ return (ast_node**)&self->on_false;
+ return NULL;
+}
+
+static ast_node** ast_ternary_next_child(ast_ternary *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->cond;
+ if (self->cond && cur == (ast_node**)&self->cond)
+ return (ast_node**)&self->on_true;
+ if (self->on_true && cur == (ast_node**)&self->on_true)
+ return (ast_node**)&self->on_false;
+ return NULL;
+}
+
+static ast_node** ast_loop_next_child(ast_loop *self, ast_node **cur) {
+ if (*cur == (ast_node*)self) {
+ /* If only we'd use ?: then this would be: return a ?: b ?: c ?: d; */
+ if (self->initexpr) return (ast_node**)&self->initexpr;
+ if (self->precond) return (ast_node**)&self->precond;
+ if (self->body) return (ast_node**)&self->body;
+ if (self->postcond) return (ast_node**)&self->postcond;
+ return (ast_node**)&self->increment;
+ }
+ if (self->initexpr && cur == (ast_node**)&self->initexpr) {
+ if (self->precond) return (ast_node**)&self->precond;
+ if (self->body) return (ast_node**)&self->body;
+ if (self->postcond) return (ast_node**)&self->postcond;
+ return (ast_node**)&self->increment;
+ }
+ if (self->precond && cur == (ast_node**)&self->precond) {
+ if (self->body) return (ast_node**)&self->body;
+ if (self->postcond) return (ast_node**)&self->postcond;
+ return (ast_node**)&self->increment;
+ }
+ if (self->body && cur == (ast_node**)&self->body) {
+ if (self->postcond) return (ast_node**)&self->postcond;
+ return (ast_node**)&self->increment;
+ }
+ return NULL;
+}
+
+static ast_node** ast_breakcont_next_child(ast_breakcont *self, ast_node **cur) {
+ (void)self; (void)cur;
+ return NULL;
+}
+
+static ast_node** ast_switch_next_child(ast_switch *self, ast_node **cur) {
+ size_t i, cases;
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->operand;
+ cases = vec_size(self->cases);
+ if (!cases)
+ return NULL;
+ if (cur == (ast_node**)&self->operand)
+ return (ast_node**)&self->cases[0].value;
+ for (i = 0; i != cases; ++i) {
+ if (cur == (ast_node**)&self->cases[i].value)
+ return (ast_node**)&self->cases[i].code;
+ if (cur == (ast_node**)&self->cases[i].code) {
+ return (i+1 != cases) ? (ast_node**)&self->cases[i+1].value
+ : NULL;
+ }
+ }
+ return NULL;
+}
+
+static ast_node** ast_label_next_child(ast_label *self, ast_node **cur) {
+ (void)self; (void)cur;
+ return NULL;
+}
+
+static ast_node** ast_goto_next_child(ast_goto *self, ast_node **cur) {
+ (void)self; (void)cur;
+ return NULL;
+}
+
+static ast_node** ast_call_next_child(ast_call *self, ast_node **cur) {
+ size_t i, params;
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->func;
+ params = vec_size(self->params);
+ if (!params)
+ return NULL;
+ if (cur == (ast_node**)&self->func)
+ return (ast_node**)&self->params[0];
+ for (i = 1; i != params; ++i) {
+ if (cur == (ast_node**)&self->params[i-1])
+ return (ast_node**)&self->params[i];
+ }
+ return NULL;
+}
+
+static ast_node** ast_block_next_child(ast_block *self, ast_node **cur) {
+ size_t i, exprs = vec_size(self->exprs);
+ if (!exprs)
+ return NULL;
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->exprs[0];
+ for (i = 1; i != exprs; ++i) {
+ if (cur == (ast_node**)&self->exprs[i-1])
+ return (ast_node**)&self->exprs[i];
+ }
+ return NULL;
+}
+
+static ast_node** ast_unary_next_child(ast_unary *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->operand;
+ return NULL;
+}
+
+static ast_node** ast_entfield_next_child(ast_entfield *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->entity;
+ if (cur == (ast_node**)&self->entity)
+ return (ast_node**)&self->field;
+ return NULL;
+}
+
+static ast_node** ast_return_next_child(ast_return *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->operand;
+ return NULL;
+}
+
+static ast_node** ast_binstore_next_child(ast_binstore *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->dest;
+ if (cur == (ast_node**)&self->dest)
+ return (ast_node**)&self->source;
+ return NULL;
+}
+
+static ast_node** ast_binary_next_child(ast_binary *self, ast_node **cur) {
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->left;
+ if (cur == (ast_node**)&self->left)
+ return (ast_node**)&self->right;
+ return NULL;
+}
+
+static ast_node** ast_function_next_child(ast_function *self, ast_node **cur) {
+ size_t i, blocks = vec_size(self->blocks);
+ if (!blocks)
+ return NULL;
+ if (*cur == (ast_node*)self)
+ return (ast_node**)&self->blocks[0];
+ for (i = 1; i != blocks; ++i) {
+ if (cur == (ast_node**)&self->blocks[i-1])
+ return (ast_node**)&self->blocks[i];
+ }
+ return NULL;
+}