#include "scirpt/ast.h" #include "common/string.h" #include "common/string_array.h" #include #include void scirpt_expr_delete(ScirptExpr* expr) { switch (expr->type) { case ScirptExprTypeEof: break; case ScirptExprTypeError: break; case ScirptExprTypeId: heapstring_destroy(&expr->id_value); break; case ScirptExprTypeInt: break; case ScirptExprTypeFloat: break; case ScirptExprTypeString: heapstring_destroy(&expr->string_value); break; case ScirptExprTypeBool: break; case ScirptExprTypeNull: break; case ScirptExprTypeArray: scirpt_expr_array_clean_and_destroy(&expr->array_value); break; case ScirptExprTypeObject: for (size_t i = 0; i < expr->object.entries.length; ++i) { heapstring_destroy(&expr->object.entries.data[i].key); scirpt_expr_delete(expr->object.entries.data[i].value); } break; case ScirptExprTypeBlock: scirpt_expr_array_clean_and_destroy(&expr->block.statements); break; case ScirptExprTypeIf: scirpt_expr_delete(expr->if_expr.condition); scirpt_expr_delete(expr->if_expr.truthy); if (expr->if_expr.falsy) scirpt_expr_delete(expr->if_expr.falsy); break; case ScirptExprTypeLoop: scirpt_expr_delete(expr->loop.body); break; case ScirptExprTypeWhile: scirpt_expr_delete(expr->while_expr.condition); scirpt_expr_delete(expr->while_expr.body); break; case ScirptExprTypeFor: heapstring_destroy(&expr->for_expr.subject); scirpt_expr_delete(expr->for_expr.value); scirpt_expr_delete(expr->for_expr.body); break; case ScirptExprTypeMember: scirpt_expr_delete(expr->member.subject); heapstring_destroy(&expr->member.value); break; case ScirptExprTypeCall: scirpt_expr_delete(expr->call.subject); scirpt_expr_array_clean_and_destroy(&expr->call.args); break; case ScirptExprTypeIndex: scirpt_expr_delete(expr->index.subject); scirpt_expr_delete(expr->index.value); break; case ScirptExprTypeUnary: scirpt_expr_delete(expr->unary.subject); break; case ScirptExprTypeBinary: scirpt_expr_delete(expr->binary.left); scirpt_expr_delete(expr->binary.right); break; case ScirptExprTypeAssign: scirpt_expr_delete(expr->assign.subject); scirpt_expr_delete(expr->assign.value); break; case ScirptExprTypeLambda: heapstring_array_clean_and_destroy(&expr->lambda.params); scirpt_expr_delete(expr->lambda.body); break; case ScirptExprTypeFunction: heapstring_array_clean_and_destroy(&expr->function.params); scirpt_expr_delete(expr->function.body); break; case ScirptExprTypeLet: heapstring_destroy(&expr->let.subject); scirpt_expr_delete(expr->let.value); break; case ScirptExprTypeReturn: scirpt_expr_delete(expr->return_statement.value); break; case ScirptExprTypeBreak: scirpt_expr_delete(expr->break_statement.value); break; } } void scirpt_expr_array_clean_and_destroy(ScirptExprArray* array) { for (size_t i = 0; i < array->length; ++i) scirpt_expr_delete(array->data[i]); scirpt_expr_array_destroy(array); } void heapstring_array_clean_and_destroy(HeapStringArray* array) { for (size_t i = 0; i < array->length; ++i) heapstring_destroy(&array->data[i]); heapstring_array_destroy(array); }