Skip to content
Permalink
Browse files
Free the parse tree once we're done with it.
--HG--
branch : calculator-experiment
  • Loading branch information
icculus committed Feb 8, 2010
1 parent 74bd066 commit 8e12f4a4ac15287c953f846ca00d7e15be37f7bc
Showing with 34 additions and 2 deletions.
  1. +33 −1 calculator.c
  2. +1 −1 calculator.lemon
@@ -497,10 +497,42 @@ static double run_expr(const Expression *expr)
return 0.0; // oh well.
} // run_expr

static void parse_complete(const Expression *expr)
static void free_expr(Context *ctx, Expression *expr)
{
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
free_expr(ctx, unary->operand);
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
free_expr(ctx, binary->left);
free_expr(ctx, binary->right);
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
free_expr(ctx, ternary->left);
free_expr(ctx, ternary->center);
free_expr(ctx, ternary->right);
} // else if
else if (expr->op == OP_STRING_LITERAL)
{
Free(ctx, (void *) ((ExpressionStringLiteral *)expr)->string);
} // else if
else if (expr->op == OP_IDENTIFIER)
{
Free(ctx, (void *) ((ExpressionIdentifier *)expr)->identifier);
} // else if
Free(ctx, expr);
} // free_expr

static void parse_complete(Context *ctx, Expression *expr)
{
print_expr(expr, 0);
printf("Result: %lf\n\n", run_expr(expr));
free_expr(ctx, expr);
} // parse_complete


@@ -72,7 +72,7 @@

// The rules...

calculator ::= expression(B). { parse_complete(B); }
calculator ::= expression(B). { parse_complete(ctx, B); }

%type identifier { const char * }
%destructor identifier { (void) ctx; } // !!! FIXME: remove this later, it's just to shut up the compiler for now.

0 comments on commit 8e12f4a

Please sign in to comment.