implemented free's for all ast types

This commit is contained in:
Aaro Koivukangas
2024-08-16 14:48:53 +02:00
parent c7795bb0e4
commit dc37278588
18 changed files with 170 additions and 25 deletions
+6
View File
@@ -25,3 +25,9 @@ int comment_print(struct comment *c)
return 0;
}
void comment_free(struct comment *c)
{
free(c->text);
free(c);
}
+37
View File
@@ -140,3 +140,40 @@ void expr_copy(struct expr *orig, struct expr *copy)
orig->comment = copy->comment;
orig->next = copy->next;
}
int expr_free(struct expr *e)
{
switch (e->kind)
{
case INSTRUCTION:
if (e->label != NULL)
label_free(e->label);
oper_free(e->oper);
ttk_register_free(e->first_arg);
value_free(e->second_arg);
break;
case INSTRUCTION_ONE_ARG:
if (e->label != NULL)
label_free(e->label);
oper_free(e->oper);
value_free(e->second_arg);
break;
case LABEL_DEF:
label_free(e->label);
oper_free(e->oper);
value_free(e->second_arg);
break;
case COMMENT:
comment_free(e->comment);
break;
default:
printf("ERROR: Expr kind not matched while freeing\n");
return 1;
}
return 0;
}
+6
View File
@@ -33,3 +33,9 @@ int label_print(struct label *l)
printf("%s", l->name);
return 0;
}
void label_free(struct label *l)
{
free(l->name);
free(l);
}
+5 -17
View File
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "token.h"
#include "expr.h"
#include "program.h"
#define FILENAME "../docs/example-file-harder.k91"
//#define FILENAME "../docs/example-file.k91"
@@ -16,21 +16,6 @@ extern char *yytext;
extern int yyparse();
int program_print(struct expr *program)
{
struct expr *current = program;
do
{
if (expr_print(current)!=0)
return 1;
current = current->next;
}
while (current->next != NULL);
return 0;
}
int main()
{
@@ -53,12 +38,15 @@ int main()
printf("Parse succesful :)\n");
} else {
printf("Parse failed\n");
program_free(parse_result);
return 1;
}
if (program_print(parse_result)!=0)
return 1;
// TODO handle all frees
program_free(parse_result);
// TODO handle errors with assertions
return 0;
}
+6
View File
@@ -34,3 +34,9 @@ int oper_print(struct oper *o)
printf("%s", o->oper_name);
return 0;
}
void oper_free(struct oper *o)
{
free(o->oper_name);
free(o);
}
+8 -1
View File
@@ -1,6 +1,7 @@
/* TODO support tags to jump to different sections */
%{
#include <stdio.h>
#include "program.h"
#include "comment.h"
#include "expr.h"
#include "label.h"
@@ -8,6 +9,12 @@
#include "value.h"
extern int yylineno;
// implicit declaration of function ‘yylex’
int yylex();
// implicit declaration of function ‘yyerror’
int yyerror(struct expr *e, const char*);
%}
@@ -118,7 +125,7 @@ num_value : TOKEN_SIGN TOKEN_NUMBER { $$ = -$2; }
%%
int yyerror ( const char *s )
int yyerror ( struct expr *e, const char *s )
{
printf("ERROR: Parser error: %s at line %d\n", s, yylineno);
return 1;
+29
View File
@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <stdio.h>
#include "expr.h"
int program_print(struct expr *program)
{
struct expr *current = program;
while (current != NULL)
{
if (expr_print(current)!=0)
return 1;
current = current->next;
}
return 0;
}
void program_free(struct expr *program)
{
struct expr *current = program;
while (current != NULL)
{
expr_free(current);
current = current->next;
}
}
+19 -6
View File
@@ -31,21 +31,29 @@ int parse_register_num(char* name, ttk_register_t kind)
return -1;
}
int value;
switch (kind)
{
case TTK_REGISTER:
char num = name[1]; // registers have the format RX, 'R' + X 0-7
return num - '0';
value = num - '0';
break;
case STACK_TTK_REGISTER:
if (strcasecmp("sp", name) == 0)
return 6;
if (strcasecmp("fp", name) == 0)
return 7;
value = 6;
if (strcasecmp("fp", name) == 0)
value = 7;
break;
default:
free(name);
return -1;
}
return -1; // error, shouldn't end up here.
free(name);
return value;
}
int ttk_register_print(struct ttk_register *ttk_reg)
@@ -68,3 +76,8 @@ int ttk_register_print(struct ttk_register *ttk_reg)
printf("R%d", ttk_reg->id);
return 0;
}
void ttk_register_free(struct ttk_register *ttk_reg)
{
free(ttk_reg);
}
+34
View File
@@ -144,3 +144,37 @@ int value_print(struct value *v)
return 1;
}
void pure_value_free(struct pure_value *p_v)
{
switch (p_v->kind)
{
case REGISTER:
ttk_register_free(p_v->ttk_register);
break;
case NUMBER:
break;
case LABEL:
label_free(p_v->label);
break;
}
free(p_v);
}
void value_free(struct value *v)
{
switch (v->indexing_mode)
{
case NONE:
pure_value_free(v->value);
break;
case INDEXED:
pure_value_free(v->value);
ttk_register_free(v->index_register);
break;
default:
printf("ERROR: Value indexing mode not matched while freeing\n");
break;;
}
free(v);
}