Skip to content

Commit dcd77fb

Browse files
author
Miguel Machado
authoredOct 9, 2018
Merge pull request #12 from mmachado95/meta4
Meta4
2 parents 736c98c + 05a0746 commit dcd77fb

8 files changed

+627
-6
lines changed
 

‎generate.c

+516
Large diffs are not rendered by default.

‎generate.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include "ast.h"
3+
#include "symbol_table.h"
4+
5+
6+
void generate_code(node_t *ast);
7+
void generate_code_program(node_t *ast);
8+
void generate_code_declaration(node_t *ast);
9+
void generate_code_func_declaration(node_t *ast);
10+
void generate_code_func_definition(node_t *ast);
11+
void generate_code_return(node_t *ast);
12+
void generate_code_assign_operator(node_t *ast);
13+
void generate_code_arithmetic_operator(node_t *ast);
14+
void generate_code_unary_operator(node_t *ast);
15+
void generate_code_call(node_t *ast);
16+
void generate_code_terminal(node_t *ast);
17+
18+
void print_param_types(param_type *params);
19+
void print_param_types_and_ids(node_t *param_list);
20+
void declare_param_declaration(node_t *param_list);
21+
22+
char *get_llvm_type(char *type_name);

‎run.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
lex $1.l
77
# o -v ou -t gera um ficheiro output com a tabela de parsing
88
yacc -d -v $1.y
9-
cc -Wall -Wno-unused-function -o $1 y.tab.c lex.yy.c ast.c symbol_table.c semantics.c
10-
./$1 -s < $2
11-
9+
cc -Wall -Wno-unused-function -o $1 y.tab.c lex.yy.c ast.c symbol_table.c semantics.c generate.c
10+
./$1 < $2
11+
zip uccompiler.zip uccompiler.l uccompiler.y y.tab.c y.tab.h ast.c ast.h semantics.c semantics.h symbol_table.c symbol_table.h generate.c generate.h

‎semantics.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ void check_declaration(node_t *declaration) {
135135
score_aux2 = 3;
136136
}
137137
if (strcmp(aux_name, "short") == 0) {
138-
score_aux = 2;
138+
score_aux = 3;
139139
}
140140
if (strcmp(aux2_name, "short") == 0) {
141-
score_aux2 = 2;
141+
score_aux2 = 3;
142142
}
143143
if (strcmp(aux_name, "char") == 0) {
144144
score_aux = 3;
@@ -393,7 +393,6 @@ void check_param_list(node_t *func_node, node_t *param_list, symbol *func, int i
393393
new_symbol->is_param = 1;
394394
}
395395
}
396-
397396
else {
398397
insert_type(param_type, func);
399398
}

‎symbol_table.c

+51
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,54 @@ void show_tables() {
182182
aux = aux->next;
183183
}
184184
}
185+
186+
void destroy_param_types(param_type *current) {
187+
if (current == NULL) {
188+
return;
189+
}
190+
191+
if (current->name != NULL) {
192+
free(current->name);
193+
}
194+
195+
destroy_param_types(current->next);
196+
197+
free(current);
198+
}
199+
200+
void destroy_symbols(symbol *current) {
201+
if (current == NULL) {
202+
return;
203+
}
204+
205+
if (current->name != NULL) {
206+
free(current->name);
207+
}
208+
if (current->type != NULL) {
209+
free(current->type);
210+
}
211+
if (current->param != NULL) {
212+
destroy_param_types(current->param);
213+
}
214+
215+
destroy_symbols(current->next);
216+
217+
free(current);
218+
}
219+
220+
void destroy_tables(table *current) {
221+
if (current == NULL) {
222+
return;
223+
}
224+
225+
if (current->name != NULL) {
226+
free(current->name);
227+
}
228+
if(current->symbol != NULL) {
229+
destroy_symbols(current->symbol);
230+
}
231+
232+
destroy_tables(current->next);
233+
234+
free(current);
235+
}

‎symbol_table.h

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ void show_symbol(symbol *symbol);
4949
void show_table(table *table);
5050
void show_tables();
5151

52+
void destroy_param_types(param_type *param);
53+
void destroy_symbols(symbol *symbols);
54+
void destroy_tables();
55+
5256
table *tables;
5357
table *current;
5458

‎uccompiler

54.6 KB
Binary file not shown.

‎uccompiler.l

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "ast.h"
77
#include "symbol_table.h"
88
#include "semantics.h"
9+
#include "generate.h"
910
int line = 1;
1011
int col = 1;
1112
int beginning_line;
@@ -127,6 +128,11 @@ CHRLIT_UNT \'({ANYCHAR}|{ESCAPE})*(\\)*{ANYCHAR}*
127128
%%
128129
int main(int argc, char *argv[]) {
129130
if (argc > 1) {
131+
if (strcmp(argv[1], "-1") == 0) {
132+
print_tokens = 0;
133+
yyparse();
134+
yylex();
135+
}
130136
if (strcmp(argv[1], "-l") == 0) {
131137
print_tokens = 1;
132138
yyparse();
@@ -146,6 +152,18 @@ int main(int argc, char *argv[]) {
146152
destroy_ast(ast);
147153
}
148154
}
155+
else if (strcmp(argv[1], "-3") == 0) {
156+
print_tokens = 0;
157+
yyparse();
158+
159+
if(errors == 0) {
160+
current = create_table("Global", 1);
161+
current->print = 1;
162+
insert_default_functions(current);
163+
check_program(ast);
164+
destroy_ast(ast);
165+
}
166+
}
149167
else if (strcmp(argv[1], "-s") == 0) {
150168
print_tokens = 0;
151169
yyparse();
@@ -157,13 +175,23 @@ int main(int argc, char *argv[]) {
157175
check_program(ast);
158176
show_tables();
159177
print_ast(ast, 0);
178+
destroy_tables(tables);
160179
destroy_ast(ast);
161180
}
162181
}
163182
}
164183
else {
165184
print_tokens = 0;
166185
yyparse();
186+
if(errors == 0) {
187+
current = create_table("Global", 1);
188+
current->print = 1;
189+
insert_default_functions(current);
190+
check_program(ast);
191+
generate_code(ast);
192+
destroy_tables(tables);
193+
destroy_ast(ast);
194+
}
167195
}
168196
return 0;
169197
}
@@ -177,3 +205,4 @@ void yyerror (char *s) {
177205
int aux_col = col - strlen(yytext);
178206
printf("Line %d, col %d: %s: %s\n", line, aux_col, s, yytext);
179207
}
208+

0 commit comments

Comments
 (0)
Please sign in to comment.