|
@@ -152,56 +152,97 @@ ast_node_t *create_bin_exp(ast_op_t operator, ast_node_t *lhs, ast_node_t *rhs)
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
-void destroy_ast(ast_node_t *node)
|
|
|
+void destroy_ast(ast_node_list_t *nl)
|
|
|
{
|
|
|
- switch(node->type) {
|
|
|
+ do {
|
|
|
+ switch(nl->node->type) {
|
|
|
+ case TYPE_VAR:
|
|
|
+ if (nl->node->var.name)
|
|
|
+ free(nl->node->var.name);
|
|
|
+ break;
|
|
|
+ case TYPE_PROTO:
|
|
|
+ if (nl->node->prototype.name)
|
|
|
+ free(nl->node->prototype.name);
|
|
|
+
|
|
|
+ for (size_t i = 0; i < nl->node->prototype.arg_count; ++i)
|
|
|
+ free(nl->node->prototype.args[i]);
|
|
|
+
|
|
|
+ free(nl->node->prototype.args);
|
|
|
+ break;
|
|
|
+ case TYPE_FN:
|
|
|
+ if (nl->node->fn.prototype)
|
|
|
+ free(nl->node->fn.prototype);
|
|
|
+
|
|
|
+ if (nl->node->fn.body)
|
|
|
+ free(nl->node->fn.body);
|
|
|
+ break;
|
|
|
+ case TYPE_CALL:
|
|
|
+ if (nl->node->call.name)
|
|
|
+ free(nl->node->call.name);
|
|
|
+
|
|
|
+ for (size_t i = 0; i < nl->node->call.arg_count; ++i)
|
|
|
+ free(nl->node->call.args[i]);
|
|
|
+
|
|
|
+ free(nl->node->call.args);
|
|
|
+ break;
|
|
|
+ case TYPE_IF:
|
|
|
+ if (nl->node->if_exp.condition)
|
|
|
+ free(nl->node->if_exp.condition);
|
|
|
+
|
|
|
+ if (nl->node->if_exp.true_body)
|
|
|
+ free(nl->node->if_exp.true_body);
|
|
|
+
|
|
|
+ if (nl->node->if_exp.false_body)
|
|
|
+ free(nl->node->if_exp.false_body);
|
|
|
+ break;
|
|
|
+ case TYPE_BIN_EXP:
|
|
|
+ if (nl->node->bin_exp.lhs)
|
|
|
+ free(nl->node->bin_exp.lhs);
|
|
|
+ if (nl->node->bin_exp.rhs)
|
|
|
+ free(nl->node->bin_exp.rhs);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ free(nl->node);
|
|
|
+ nl = nl->next;
|
|
|
+ } while (nl->next != NULL);
|
|
|
+
|
|
|
+ free(nl);
|
|
|
+ verbose_printf("destroyed ast\n");
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void dump_node(ast_node_t *node)
|
|
|
+{
|
|
|
+ switch (node->type) {
|
|
|
case TYPE_VAR:
|
|
|
- if (node->var.name)
|
|
|
- free(node->var.name);
|
|
|
+ printf("variable:\n\tname: %s\n\ttype: %u\n\tmutable:%d\n",
|
|
|
+ node->var.name, node->var.data_type, node->var.mutable);
|
|
|
break;
|
|
|
case TYPE_PROTO:
|
|
|
- if (node->prototype.name)
|
|
|
- free(node->prototype.name);
|
|
|
-
|
|
|
- for (size_t i = 0; i < node->prototype.arg_count; ++i)
|
|
|
- free(node->prototype.args[i]);
|
|
|
-
|
|
|
- free(node->prototype.args);
|
|
|
+ printf("proto:\n\tname: %s\n\ttype: %u\n",
|
|
|
+ node->prototype.name, node->prototype.data_type);
|
|
|
break;
|
|
|
case TYPE_FN:
|
|
|
- if (node->fn.prototype)
|
|
|
- free(node->fn.prototype);
|
|
|
-
|
|
|
- if (node->fn.body)
|
|
|
- free(node->fn.body);
|
|
|
+ printf("function:\n");
|
|
|
+ dump_node(node->fn.prototype);
|
|
|
break;
|
|
|
case TYPE_CALL:
|
|
|
- if (node->call.name)
|
|
|
- free(node->call.name);
|
|
|
-
|
|
|
- for (size_t i = 0; i < node->call.arg_count; ++i)
|
|
|
- free(node->call.args[i]);
|
|
|
-
|
|
|
- free(node->call.args);
|
|
|
+ /* TODO */
|
|
|
break;
|
|
|
case TYPE_IF:
|
|
|
- if (node->if_exp.condition)
|
|
|
- free(node->if_exp.condition);
|
|
|
-
|
|
|
- if (node->if_exp.true_body)
|
|
|
- free(node->if_exp.true_body);
|
|
|
-
|
|
|
- if (node->if_exp.false_body)
|
|
|
- free(node->if_exp.false_body);
|
|
|
+ /* TODO */
|
|
|
break;
|
|
|
case TYPE_BIN_EXP:
|
|
|
- if (node->bin_exp.lhs)
|
|
|
- free(node->bin_exp.lhs);
|
|
|
- if (node->bin_exp.rhs)
|
|
|
- free(node->bin_exp.rhs);
|
|
|
+ /* TODO */
|
|
|
break;
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- free(node);
|
|
|
- verbose_printf("destroyed ast\n");
|
|
|
+void dump_nodes(ast_node_list_t *nl)
|
|
|
+{
|
|
|
+ do {
|
|
|
+ dump_node(nl->node);
|
|
|
+ nl = nl->next;
|
|
|
+ } while (nl->next != NULL);
|
|
|
}
|