Skip to content

Commit c4fdaad

Browse files
committed
Add %destructor symbols in parser definition
Required to clean up temp nodes/values if a parsing error occurs, as some aren't attached to the root node of the compiled expression tree yet.
1 parent 5be3db3 commit c4fdaad

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

projectm-eval/Compiler.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,11 @@ static const yytype_int8 yytranslate[] =
518518
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
519519
static const yytype_uint8 yyrline[] =
520520
{
521-
0, 77, 77, 78, 88, 92, 93, 97, 101, 102,
522-
103, 107, 108, 114, 115, 118, 121, 122, 123, 130,
523-
131, 132, 133, 134, 135, 136, 137, 140, 141, 142,
524-
143, 144, 145, 148, 149, 152, 155, 158, 161, 162,
525-
163, 164, 165, 166, 167, 168, 171, 172, 173, 176
521+
0, 82, 82, 83, 93, 97, 98, 102, 106, 107,
522+
108, 112, 113, 119, 120, 123, 126, 127, 128, 135,
523+
136, 137, 138, 139, 140, 141, 142, 145, 146, 147,
524+
148, 149, 150, 153, 154, 157, 160, 163, 166, 167,
525+
168, 169, 170, 171, 172, 173, 176, 177, 178, 181
526526
};
527527
#endif
528528

@@ -1569,7 +1569,39 @@ yydestruct (const char *yymsg,
15691569
YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
15701570

15711571
YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
1572-
YY_USE (yykind);
1572+
switch (yykind)
1573+
{
1574+
case YYSYMBOL_VAR: /* VAR */
1575+
{ free(((*yyvaluep).VAR)); }
1576+
break;
1577+
1578+
case YYSYMBOL_FUNC: /* FUNC */
1579+
{ free(((*yyvaluep).FUNC)); }
1580+
break;
1581+
1582+
case YYSYMBOL_function: /* function */
1583+
{ prjm_eval_compiler_destroy_node(((*yyvaluep).function)); }
1584+
break;
1585+
1586+
case YYSYMBOL_46_function_arglist: /* function-arglist */
1587+
{ prjm_eval_compiler_destroy_arglist(((*yyvaluep).yykind_46)); }
1588+
break;
1589+
1590+
case YYSYMBOL_parentheses: /* parentheses */
1591+
{ prjm_eval_compiler_destroy_node(((*yyvaluep).parentheses)); }
1592+
break;
1593+
1594+
case YYSYMBOL_48_instruction_list: /* instruction-list */
1595+
{ prjm_eval_compiler_destroy_node(((*yyvaluep).yykind_48)); }
1596+
break;
1597+
1598+
case YYSYMBOL_expression: /* expression */
1599+
{ prjm_eval_compiler_destroy_node(((*yyvaluep).expression)); }
1600+
break;
1601+
1602+
default:
1603+
break;
1604+
}
15731605
YY_IGNORE_MAYBE_UNINITIALIZED_END
15741606
}
15751607

projectm-eval/Compiler.y

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ typedef void* yyscan_t;
4747
%nterm <prjm_eval_compiler_node_t*> function program instruction-list expression parentheses
4848
%nterm <prjm_eval_compiler_arg_list_t*> function-arglist
4949

50+
/* Cleanup */
51+
%destructor { free($$); } VAR FUNC
52+
%destructor { prjm_eval_compiler_destroy_node($$); } function instruction-list expression parentheses
53+
%destructor { prjm_eval_compiler_destroy_arglist($$); } function-arglist
54+
5055
/* Operator precedence, lowest first, highest last. */
5156
%precedence ','
5257
%right '='

0 commit comments

Comments
 (0)