implemented ast printing

This commit is contained in:
Aaro Koivukangas
2024-08-16 13:27:24 +02:00
parent 9452ebfdb1
commit 67992d8b52
13 changed files with 278 additions and 4 deletions
+15
View File
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "comment.h"
@@ -10,3 +11,17 @@ struct comment * comment_create(char *text)
return c;
}
int comment_print(struct comment *c)
{
if (!c || !c->text)
{
printf("\nERROR: Comment does not exist\n");
return 1;
}
printf("%s", c->text);
return 0;
}
+114
View File
@@ -1,6 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include "expr.h"
#define PRINT_COMMENTS 1
struct expr * expr_create(expr_t kind, struct label *l, struct oper *o, struct ttk_register *f_arg, struct value *s_arg, struct comment *c, struct expr *n)
{
struct expr *e = malloc(sizeof(*e));
@@ -16,6 +19,117 @@ struct expr * expr_create(expr_t kind, struct label *l, struct oper *o, struct t
return e;
}
struct expr *expr_create_instruction(struct oper *o, struct ttk_register *ttk_reg, struct value *v)
{
return expr_create(INSTRUCTION, NULL, o, ttk_reg, v, NULL, NULL);
}
struct expr *expr_create_instruction_one_arg(struct oper *o, struct value *v)
{
return expr_create(INSTRUCTION_ONE_ARG, NULL, o, NULL, v, NULL, NULL);
}
struct expr *expr_create_labeled_instruction(struct label *l, struct oper *o, struct ttk_register *ttk_reg, struct value *v)
{
return expr_create(INSTRUCTION, l, o, ttk_reg, v, NULL, NULL);
}
struct expr *expr_create_labeled_instruction_one_arg(struct label *l, struct oper *o, struct value *v)
{
return expr_create(INSTRUCTION_ONE_ARG, l, o, NULL, v, NULL, NULL);
}
struct expr *expr_create_compiler_instruction(struct label *l, struct oper *o, struct value *v)
{
return expr_create(LABEL_DEF, l, o, NULL, v, NULL, NULL);
}
struct expr *expr_create_compiler_instruction_raw_label_oper_value(char* label, char *oper, int value)
{
struct label *l = label_create_const(label);
struct oper *o = oper_create_compiler(oper);
struct pure_value *p_v = pure_value_create_value(value);
struct value *v = value_create_direct_value(p_v);
return expr_create_compiler_instruction(l, o, v);
}
struct expr *expr_create_comment(struct comment *c)
{
return expr_create(COMMENT, NULL, NULL, NULL, NULL, c, NULL);
}
int expr_print(struct expr *e)
{
if (!e)
return 1;
switch (e->kind)
{
case INSTRUCTION:
if (e->label)
{
if (label_print(e->label)!=0)
return 1;;
printf(" ");
}
if (oper_print(e->oper)!=0)
return 1;
printf(" ");
if (ttk_register_print(e->first_arg)!=0)
return 1;
printf(",");
printf(" ");
if (value_print(e->second_arg)!=0)
return 1;
printf("\n");
return 0;
case INSTRUCTION_ONE_ARG:
if (e->label)
{
if (label_print(e->label)!=0)
return 1;
printf(" ");
}
if (oper_print(e->oper)!=0)
return 1;
printf(" ");
if (value_print(e->second_arg)!=0)
return 1;
printf("\n");
return 0;
case LABEL_DEF:
if (label_print(e->label)!=0)
return 1;
printf(" ");
if (oper_print(e->oper)!=0)
return 1;
printf(" ");
if (value_print(e->second_arg)!=0)
return 1;
printf("\n");
return 0;
case COMMENT:
if (PRINT_COMMENTS == 1)
return 0;
if (comment_print(e->comment)!=0)
return 1;
printf("\n");
return 0;
default:
return 1;
}
}
void expr_copy(struct expr *orig, struct expr *copy)
{
orig->kind = copy->kind;
+12
View File
@@ -21,3 +21,15 @@ struct label * label_create(label_t kind, char* name, int value)
struct label * label_create_const(char* name) { return label_create(LABEL_CONST, name, 0); }
struct label * label_create_const_sys(char* name) { return label_create(LABEL_CONST, name, 0); } // FIXME value
struct label * label_create_tag(char* name) { return label_create(LABEL_TAG, name, 0); } // FIXME also this
int label_print(struct label *l)
{
if (!l->name)
{
printf("ERROR: Label name doesn't exist.\n");
return 1;
}
printf("%s", l->name);
return 0;
}
+21
View File
@@ -15,6 +15,23 @@ extern char *yytext;
// parser
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()
{
yyin = fopen(FILENAME, "r");
@@ -39,5 +56,9 @@ int main()
return 1;
}
if (program_print(parse_result)!=0)
return 1;
// TODO handle all frees
return 0;
}
+14
View File
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "oper.h"
@@ -20,3 +21,16 @@ struct oper * oper_create_branch(char* oper_name) { return oper_create(OPE
struct oper * oper_create_stack(char* oper_name) { return oper_create(OPER_STACK, oper_name); }
struct oper * oper_create_sys(char* oper_name) { return oper_create(OPER_SYS, oper_name); }
struct oper * oper_create_compiler(char* oper_name) { return oper_create(OPER_COMPILER, oper_name); }
int oper_print(struct oper *o)
{
if (!o)
{
printf("\nERROR: Null oper given.\n");
return 1;
}
printf("%s", o->oper_name);
return 0;
}
+22
View File
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ttk_register.h"
@@ -46,3 +47,24 @@ int parse_register_num(char* name, ttk_register_t kind)
return -1; // error, shouldn't end up here.
}
int ttk_register_print(struct ttk_register *ttk_reg)
{
if (!ttk_reg || !ttk_reg->id)
{
printf("ERROR: No ttk_register exists.\n");
return 1;
}
switch (ttk_reg->id)
{
case 6:
printf("SP");
return 0;
case 7:
printf("FP");
return 0;
}
printf("R%d", ttk_reg->id);
return 0;
}
+70 -4
View File
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include "value.h"
@@ -71,10 +72,75 @@ addr_mode_t parse_addr_mode(char addr_mode)
return DIRECT;
}
int pure_value_print(struct pure_value *p_v)
{
if (!p_v)
{
return INDIRECT;
}
else {
return DIRECT;
printf("\nERROR: Pure_value does not exist\n");
return 1;
}
switch (p_v->kind)
{
case REGISTER:
ttk_register_print(p_v->ttk_register);
return 0;
case NUMBER:
printf("%d", p_v->number);
return 0;
case LABEL:
label_print(p_v->label);
return 0;
}
return 1;
}
int value_print(struct value *v)
{
if (!v)
{
printf("ERROR: Value not defined.\n");
return 1;
}
// add = or @ to the beginnig of value
switch (v->addr_mode)
{
case IMMEDIATE:
printf("=");
break;
case INDIRECT:
printf("@");
break;
case DIRECT:
break;
}
switch (v->indexing_mode)
{
case NONE:
if (pure_value_print(v->value)!=0)
return 1;
return 0;
case INDEXED:
if (pure_value_print(v->value)!=0)
return 1;
printf("(");
if (ttk_register_print(v->index_register)!=0)
return 1;
printf(")");
return 0;
case INDEXED_ONLY_IREG:
printf("(");
if (ttk_register_print(v->index_register)!=0)
return 1;
printf(")");
return 0;
}
return 1;
}