Skip to content

Commit

Permalink
fix pile of ast related leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 12, 2025
1 parent 313e75b commit 4596a4d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
50 changes: 39 additions & 11 deletions compiler/ast.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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++:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
# ...
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions compiler/tokenizer.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4596a4d

Please sign in to comment.