mirror of
https://github.com/iritmaximus/ttk91-compiler.git
synced 2026-10-12 19:13:40 +03:00
implemented free's for all ast types
This commit is contained in:
@@ -7,6 +7,7 @@ INCLUDEDIR=include
|
||||
FILES=$(SRCDIR)/main.c \
|
||||
$(SRCDIR)/scanner.c \
|
||||
$(SRCDIR)/parser.c \
|
||||
$(SRCDIR)/program.c \
|
||||
$(SRCDIR)/expr.c \
|
||||
$(SRCDIR)/label.c \
|
||||
$(SRCDIR)/ttk_register.c \
|
||||
|
||||
@@ -9,4 +9,6 @@ struct comment {
|
||||
struct comment * comment_create(char *text);
|
||||
int comment_print(struct comment *c);
|
||||
|
||||
void comment_free(struct comment *c);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -37,4 +37,6 @@ struct expr *expr_create_compiler_instruction(struct label *l, struct oper *o, s
|
||||
struct expr *expr_create_compiler_instruction_raw_label_oper_value(char* label, char *o, int value);
|
||||
struct expr *expr_create_comment(struct comment *c);
|
||||
|
||||
int expr_free(struct expr *e);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -21,4 +21,6 @@ struct label * label_create_const(char* name);
|
||||
struct label * label_create_const_sys(char* name);
|
||||
struct label * label_create_tag(char* name);
|
||||
|
||||
void label_free(struct label *l);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -29,4 +29,6 @@ struct oper * oper_create_stack(char* oper_name);
|
||||
struct oper * oper_create_sys(char* oper_name);
|
||||
struct oper * oper_create_compiler(char* oper_name);
|
||||
|
||||
void oper_free(struct oper *o);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#include "expr.h"
|
||||
|
||||
int program_print(struct expr *program);
|
||||
void program_free(struct expr *program);
|
||||
+1
-1
@@ -81,7 +81,7 @@ extern int yydebug;
|
||||
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
|
||||
union YYSTYPE
|
||||
{
|
||||
#line 40 "src/parser.bison"
|
||||
#line 47 "src/parser.bison"
|
||||
|
||||
struct expr *expr;
|
||||
struct oper *oper;
|
||||
|
||||
@@ -19,4 +19,6 @@ struct ttk_register * ttk_register_create_stack_register(char* name);
|
||||
|
||||
int ttk_register_print(struct ttk_register *ttk_reg);
|
||||
|
||||
void ttk_register_free(struct ttk_register *ttk_reg);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -52,4 +52,8 @@ struct pure_value * pure_value_create(value_t kind, struct ttk_register *ttk_reg
|
||||
addr_mode_t parse_addr_mode(char addr_mode);
|
||||
int value_print(struct value *v);
|
||||
|
||||
|
||||
void value_free(struct value *v);
|
||||
void pure_value_free(struct pure_value *p_v);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,3 +25,9 @@ int comment_print(struct comment *c)
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void comment_free(struct comment *c)
|
||||
{
|
||||
free(c->text);
|
||||
free(c);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user