mirror of
https://github.com/iritmaximus/ttk91-compiler.git
synced 2026-10-12 19:13:40 +03:00
added ast creation to parsing
This commit is contained in:
+12
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
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));
|
||||
struct expr *e = malloc(sizeof(*e));
|
||||
|
||||
e->kind = kind;
|
||||
e->label = l;
|
||||
@@ -15,3 +15,14 @@ struct expr * expr_create(expr_t kind, struct label *l, struct oper *o, struct t
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
void expr_copy(struct expr *orig, struct expr *copy)
|
||||
{
|
||||
orig->kind = copy->kind;
|
||||
orig->label = copy->label;
|
||||
orig->oper = copy->oper;
|
||||
orig->first_arg = copy->first_arg;
|
||||
orig->second_arg = copy->second_arg;
|
||||
orig->comment = copy->comment;
|
||||
orig->next = copy->next;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
|
||||
struct label * label_create(label_t kind, char *name, int value)
|
||||
{
|
||||
if (!name)
|
||||
return NULL;
|
||||
|
||||
struct label *l = malloc(sizeof(*l));
|
||||
l->kind = kind;
|
||||
l->name = name;
|
||||
|
||||
@@ -23,6 +23,12 @@ int main()
|
||||
}
|
||||
|
||||
struct expr *parse_result = malloc(sizeof(*parse_result));
|
||||
if (!parse_result)
|
||||
{
|
||||
printf("ERROR: Failed to allocate memory for parse_result.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if (yyparse(parse_result) == 0) {
|
||||
printf("Parse succesful :)\n");
|
||||
|
||||
+5
-1
@@ -1,4 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "oper.h"
|
||||
|
||||
|
||||
@@ -6,8 +7,11 @@ struct oper * oper_create(oper_t kind, char* oper_name)
|
||||
{
|
||||
struct oper *o = malloc(sizeof(*o));
|
||||
|
||||
char *name_copy = malloc(sizeof(char) * (strlen(oper_name)+1));
|
||||
strcpy(name_copy, oper_name);
|
||||
|
||||
o->kind = kind;
|
||||
o->oper_name = oper_name;
|
||||
o->oper_name = name_copy;
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
+11
-10
@@ -13,6 +13,7 @@ extern int yylineno;
|
||||
|
||||
|
||||
%define parse.error verbose
|
||||
|
||||
%parse-param { struct expr *parse_result }
|
||||
|
||||
%token TOKEN_REGISTER
|
||||
@@ -66,15 +67,15 @@ extern int yylineno;
|
||||
|
||||
%%
|
||||
|
||||
program : expr_list { parse_result = $1; }
|
||||
program : expr_list { expr_copy(parse_result, $1); }
|
||||
;
|
||||
|
||||
expr_list : expr expr_list { $$ = $1; $1->next = $2; }
|
||||
expr_list : expr_list expr { $$ = $2; $2->next = $1; }
|
||||
| { $$ = NULL; }
|
||||
;
|
||||
|
||||
expr : oper ttk_register TOKEN_COMMA value { $$ = expr_create(INSTRUCTION, NULL, $1, $2, $4, NULL, NULL); }
|
||||
| oper value { $$ = expr_create(INSTRUCTION, NULL, $1, NULL, $2, NULL, NULL); }
|
||||
| oper value { $$ = expr_create(INSTRUCTION_ONE_ARG, NULL, $1, NULL, $2, NULL, NULL); }
|
||||
| TOKEN_LABEL TOKEN_COMP_OPER num_value { $$ = expr_create(LABEL_DEF, label_create(LABEL_CONST, $1, 0), NULL, NULL, NULL, NULL, NULL); /* TODO implement this properly */ }
|
||||
| TOKEN_LABEL oper ttk_register TOKEN_COMMA value { $$ = expr_create(INSTRUCTION, label_create(LABEL_TAG,$1, 0), $2, $3, $5, NULL, NULL); /* TODO FIXME labels have always value 0 */ }
|
||||
| TOKEN_LABEL oper value { $$ = expr_create(INSTRUCTION, label_create(LABEL_TAG,$1, 0), $2, NULL, $3, NULL, NULL); }
|
||||
@@ -89,16 +90,16 @@ oper : TOKEN_OPER_DATA { $$ = oper_create(OPER_DATA, $1
|
||||
| TOKEN_OPER_SYS { $$ = oper_create(OPER_SYS, $1); }
|
||||
;
|
||||
|
||||
ttk_register: TOKEN_REGISTER { $$ = ttk_register_create($1, 0); }
|
||||
| TOKEN_STACK_REGISTER { $$ = ttk_register_create($1, 0); }
|
||||
ttk_register: TOKEN_REGISTER { $$ = ttk_register_create(TTK_REGISTER, $1, 0); }
|
||||
| TOKEN_STACK_REGISTER { $$ = ttk_register_create(STACK_TTK_REGISTER, $1, 0); }
|
||||
;
|
||||
|
||||
|
||||
value : pure_value TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { value_create(DIRECT, INDEXED, $1, $3); }
|
||||
| pure_value { value_create(DIRECT, NONE, $1, NULL); }
|
||||
| TOKEN_ADDR_M pure_value TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { value_create(parse_addr_mode($1), INDEXED, $2, $4); }
|
||||
| TOKEN_ADDR_M pure_value { value_create(parse_addr_mode($1), NONE, $2, NULL); }
|
||||
| TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { value_create(DIRECT, NONE, NULL, $2); }
|
||||
value : pure_value TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { $$ = value_create(DIRECT, INDEXED, $1, $3); }
|
||||
| pure_value { $$ = value_create(DIRECT, NONE, $1, NULL); }
|
||||
| TOKEN_ADDR_M pure_value TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { $$ = value_create(parse_addr_mode($1), INDEXED, $2, $4); }
|
||||
| TOKEN_ADDR_M pure_value { $$ = value_create(parse_addr_mode($1), NONE, $2, NULL); }
|
||||
| TOKEN_PAREN_L ttk_register TOKEN_PAREN_R { $$ = value_create(DIRECT, NONE, NULL, $2); }
|
||||
;
|
||||
|
||||
pure_value : ttk_register { $$ = pure_value_create(REGISTER, $1, 0, NULL); }
|
||||
|
||||
+20
-5
@@ -1,22 +1,37 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "ttk_register.h"
|
||||
|
||||
struct ttk_register * ttk_register_create(char* name, int value)
|
||||
struct ttk_register * ttk_register_create(ttk_register_t kind, char* name, int value)
|
||||
{
|
||||
struct ttk_register * r = malloc(sizeof(*r));
|
||||
|
||||
r->id = parse_register_num(name);
|
||||
r->kind = kind;
|
||||
r->id = parse_register_num(name, kind);
|
||||
r->value = value;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int parse_register_num(char* name)
|
||||
int parse_register_num(char* name, ttk_register_t kind)
|
||||
{
|
||||
if (!name) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char num = name[1]; // registers have the format RX, 'R' + X 0-7
|
||||
return num - '0';
|
||||
switch (kind)
|
||||
{
|
||||
case TTK_REGISTER:
|
||||
char num = name[1]; // registers have the format RX, 'R' + X 0-7
|
||||
return num - '0';
|
||||
case STACK_TTK_REGISTER:
|
||||
if (strcasecmp("sp", name) == 0)
|
||||
return 6;
|
||||
if (strcasecmp("fp", name) == 0)
|
||||
return 7;
|
||||
|
||||
}
|
||||
|
||||
return -1; // error, shouldn't end up here.
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user