diff --git a/compiler/ast.jou b/compiler/ast.jou index a176b3f4..7dbfedaa 100644 --- a/compiler/ast.jou +++ b/compiler/ast.jou @@ -328,19 +328,19 @@ class AstExpression: self->operands[i].print_with_tree_printer(tp.print_prefix(i == self->get_arity()-1)) def free(self) -> None: - if self->kind == AstExpressionKind.Call: - self->call.free() - elif self->kind == AstExpressionKind.As: - self->as_->free() - free(self->as_) - elif self->kind == AstExpressionKind.String: + if self->kind == AstExpressionKind.String: free(self->string) elif self->kind == AstExpressionKind.Array: self->array.free() + elif self->kind == AstExpressionKind.Call: + self->call.free() elif self->kind == AstExpressionKind.Instantiate: self->instantiation.free() elif self->kind == AstExpressionKind.GetClassField: self->class_field.free() + elif self->kind == AstExpressionKind.As: + self->as_->free() + free(self->as_) if self->get_arity() != 0: for i = 0; i < self->get_arity(); i++: @@ -469,6 +469,10 @@ class AstAssertion: condition: AstExpression condition_str: byte* + def free(self) -> None: + self->condition.free() + free(self->condition_str) + enum AstStatementKind: ExpressionStatement # Evaluate an expression. Discard the result. @@ -502,8 +506,8 @@ class AstStatement: if_statement: AstIfStatement while_loop: AstConditionAndBody for_loop: AstForLoop - return_value: AstExpression* # can be NULL - assignment: AstAssignment + return_value: AstExpression* # can be NULL + assignment: AstAssignment # also used for +=, -= etc var_declaration: AstNameTypeValue # DeclareLocalVar function: AstFunctionOrMethod classdef: AstClassDef @@ -582,19 +586,38 @@ class AstStatement: printf("??????\n") def free(self) -> None: - if self->kind == AstStatementKind.Enum: - self->enumdef.free() if self->kind == AstStatementKind.ExpressionStatement: self->expression.free() + if self->kind == AstStatementKind.Assert: + self->assertion.free() if self->kind == AstStatementKind.Return and self->return_value != NULL: self->return_value->free() free(self->return_value) if self->kind == AstStatementKind.If: self->if_statement.free() + if self->kind == AstStatementKind.WhileLoop: + self->while_loop.free() if self->kind == AstStatementKind.ForLoop: self->for_loop.free() + if self->kind == AstStatementKind.DeclareLocalVar: + self->var_declaration.free() + if ( + self->kind == AstStatementKind.Assign + or self->kind == AstStatementKind.InPlaceAdd + or self->kind == AstStatementKind.InPlaceSub + or self->kind == AstStatementKind.InPlaceMul + or self->kind == AstStatementKind.InPlaceDiv + or self->kind == AstStatementKind.InPlaceMod + ): + self->assignment.free() + if self->kind == AstStatementKind.Function: + self->function.free() if self->kind == AstStatementKind.Class: self->classdef.free() + if self->kind == AstStatementKind.Enum: + self->enumdef.free() + # TODO: GlobalVariableDeclaration + # TODO: GlobalVariableDefinition # Useful for e.g. "while condition: body", "if condition: body" @@ -631,6 +654,10 @@ class AstAssignment: self->target.print_with_tree_printer(tp.print_prefix(False)) self->value.print_with_tree_printer(tp.print_prefix(True)) + def free(self) -> None: + self->target.free() + self->value.free() + # if foo: # ... @@ -730,9 +757,10 @@ class AstNameTypeValue: self->value->print_with_tree_printer(sub) def free(self) -> None: + self->type.free() if self->value != NULL: self->value->free() - free(self->value) + free(self->value) # typically multiple indented lines after a ":" at end of line diff --git a/compiler/tokenizer.jou b/compiler/tokenizer.jou index 3412ad56..6f103bd3 100644 --- a/compiler/tokenizer.jou +++ b/compiler/tokenizer.jou @@ -600,6 +600,8 @@ def tokenize(path: byte*, import_location: Location*) -> Token*: fail(*import_location, message) raw_tokens = tokenize_without_indent_dedent_tokens(file, path) + fclose(file) + better_tokens = handle_indentations(raw_tokens) free(raw_tokens) return better_tokens