ast_instantiate(ast_binary, ctx, ast_binary_delete, ast_binary_next_child);
ast_expression_init((ast_expression*)self, (ast_expression_codegen*)&ast_binary_codegen);
+ if (ast_istype(right, ast_unary) && OPTS_OPTIMIZATION(OPTIM_PEEPHOLE)) {
+ ast_expression *normal = ((ast_unary*)right)->operand;
+
+ /* make a-(-b) => a + b */
+ if (op == INSTR_SUB_F) {
+ op = INSTR_ADD_F;
+ right = normal;
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ } else if (op == INSTR_SUB_V) {
+ op = INSTR_ADD_V;
+ right = normal;
+ ++opts_optimizationcount[OPTIM_PEEPHOLE];
+ }
+ }
+
self->op = op;
self->left = left;
self->right = right;
if (!vtype) {
compile_error(ast_ctx(self), "internal error: ast_function_new condition 0");
goto cleanup;
- } else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
} else if (vtype->hasvalue || vtype->expression.vtype != TYPE_FUNCTION) {
compile_error(ast_ctx(self), "internal error: ast_function_new condition %i %i type=%i (probably 2 bodies?)",
(int)!vtype,
self->fixedparams = NULL;
self->return_value = NULL;
+ self->static_names = NULL;
+ self->static_count = 0;
+
return self;
cleanup:
*/
ast_unref(self->vtype);
}
+ for (i = 0; i < vec_size(self->static_names); ++i)
+ mem_d(self->static_names[i]);
+ vec_free(self->static_names);
for (i = 0; i < vec_size(self->blocks); ++i)
ast_delete(self->blocks[i]);
vec_free(self->blocks);
return false;
}
-/* iterator functions */
+/* 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;