-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.y
216 lines (192 loc) · 4.22 KB
/
grammar.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "internals_backend.h"
#include "libs/arg_table.h"
#include "internals.h"
#include "debug.h"
//extern void init_arg_table(ARG_TABLE* arg_table);
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
ARG_TABLE* arg_table = NULL;
extern char* yytext;
extern int yylineno;
extern int yylex();
void yyerror(const char *str)
{
fprintf(stderr,"error: on line %d, %s before token: %s\n",yylineno ,str, yytext);
}
extern char* identifier_name;
extern SYMBOL_TABLE symbol_table;
char* current_scope;
char* ins;
extern bool dry_run;
int struct_size_cnt;
%}
%union
{
int val;
char* id;
}
%token OPERATION SEMICOLON DEF ALLOC INS STRUCT BYTE
%token <val> NUMBER
%token <id> IDENTIFIER
%type <id> struct_type_def argument
%%
commands: /* empty */
| commands command
;
command:
instruction
|
defenition
|
label
|
struct
;
argument:
IDENTIFIER '.' IDENTIFIER
{
if(!dry_run)
{
#ifdef _DEBUG_
printf("struct access %s.%s\n", $3, $1);
#endif
SYMBOL_NODE* symb_node = search_symbol(&symbol_table, $3, $1);
if(symb_node)
{
ARG_NODE_TEMPLATE node;
node.value = symb_node->addr;
node.domain = "numeric";
append_arg(arg_table, node);
}
else
{
printf("error: no %s.%s was found", $3, $1);
}
}
}
|
IDENTIFIER
{
if(!dry_run)
{
append_arg(arg_table, match_args($1));
#ifdef _DEBUG_
printf("arguments detected: name: %s \t value %d\n", $1, match_args($1));
#endif
}
}
|
NUMBER
{
if(!dry_run)
{
ARG_NODE_TEMPLATE node;
node.value = $1;
node.domain = "numeric";
append_arg(arg_table, node);
}
}
|
'[' NUMBER ']'
{
if(!dry_run)
{
ARG_NODE_TEMPLATE node;
node.value = $2;
node.domain = "address";
append_arg(arg_table, node);
}
}
;
arguments:
|
argument
|
argument ',' arguments
;
instruction:
IDENTIFIER
{
if(!dry_run)
{
#ifdef _DEBUG_
printf("Instructions %s\n", $1);
#endif
arg_table = malloc(sizeof(ARG_TABLE));
init_arg_table(arg_table);
}
} arguments ';'
{
if(!dry_run)
{
assemble_ins($1, arg_table);
free(arg_table);
}
increment_addr();
reset_identifiers();
}
;
defenition_exp:
instruction
|
label
|
defenition_exp instruction
|
defenition_exp label
;
defenition:
DEF '{' defenition_exp '}'
{
printf("DEF\n");
}
;
label:
IDENTIFIER ':'
{
if(dry_run)
{
append_symbol(&symbol_table, $1, TYPE_LABEL, addr, "none");
}
}
;
struct_type_def:
BYTE NUMBER ',' IDENTIFIER ';'
{
if(dry_run)
{
append_symbol(&symbol_table, strdup($4), TYPE_STRUCT_MEMBER, struct_size_cnt, current_scope);
struct_size_cnt += $2;
}
}
;
recursive_struct_types:
|
recursive_struct_types struct_type_def
;
struct:
STRUCT IDENTIFIER '{'
{
if(dry_run)
{
//Check if the struct already exists
struct_size_cnt = 0;
current_scope = $2;
append_symbol(&symbol_table, $2, TYPE_STRUCT, 0x00, "struct");
}
} recursive_struct_types '}'
;
%%