Skip to content

Meta4 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
b0ff8c7
Starts meta4. Flags.
teresalazar13 May 24, 2018
97f05a5
Creates generate_code main function.
teresalazar13 May 24, 2018
acd7036
Func Declaration code generator.
teresalazar13 May 24, 2018
3e0542a
Merge branch 'master' of https://github.com/Kabombom/compilers into m…
May 24, 2018
20c42bb
Frees symbol tables
May 24, 2018
b6a596b
Scrip now zips at the end
May 24, 2018
d57f6c3
Starts functions for Generate code declaration and Generate Code func…
teresalazar13 May 24, 2018
a84c872
merge.
teresalazar13 May 24, 2018
d68052a
Refactor get_type function to get_llvm_type
May 24, 2018
f3d18ba
ups
May 24, 2018
564e3ae
Better code in generate_code_func_declaration
May 24, 2018
3928d9c
Fixes function declaration code gen
May 25, 2018
2901302
comments declaration
May 25, 2018
e6e70eb
Code generator for unary operator.
teresalazar13 May 26, 2018
b390409
Fixes declarations
May 27, 2018
10475d4
Generate code store.
teresalazar13 May 27, 2018
73916d4
Generate code for local declarations.
teresalazar13 May 27, 2018
69f5330
Declares getchar and putchar functions.
teresalazar13 May 27, 2018
22ea9bc
Generate code for call.
teresalazar13 May 27, 2018
2423ff7
Generate code call, assign and terminal.
teresalazar13 May 27, 2018
a9fc4a2
Corrects local declarations.
teresalazar13 May 27, 2018
c12fe44
Fixes types in declarations
May 27, 2018
55927ae
Fixes chars declaration
May 27, 2018
e838247
Tries to fix runtime
May 27, 2018
4b8d6af
Generates code for return.
teresalazar13 May 28, 2018
b02bda2
20 points
May 28, 2018
b6d4300
Generate code for teminals (quick fix, not sure).
teresalazar13 May 28, 2018
b9a3a6d
Corrects error.
teresalazar13 May 28, 2018
3d7a171
Quick fix Minus.
teresalazar13 May 28, 2018
bb083f6
Corrects store.
teresalazar13 May 29, 2018
50f47ef
Corrects errors in declarations.
teresalazar13 May 29, 2018
5da52ed
corrects store.
teresalazar13 May 29, 2018
f9bd749
Fixes paramdeclaration in funcdefinition and funcdeclaration.
teresalazar13 May 29, 2018
e7ab364
Fixes mistake in param list.
teresalazar13 May 29, 2018
9f84cc9
Fixe bug.
teresalazar13 May 29, 2018
d275b4d
Fix mistake in store.
teresalazar13 May 29, 2018
11cb384
Corrects store and declaration errors.
teresalazar13 May 30, 2018
c9b49ae
Corrects assign and declarations.
teresalazar13 May 30, 2018
7d65080
Corrects sub.
teresalazar13 May 30, 2018
5034040
Corrects assign and declarations.
teresalazar13 May 30, 2018
b6702dd
Corrects type errors.
teresalazar13 May 30, 2018
7e0d119
Fixes returns and register count
May 30, 2018
c1a1129
Fixes reg count in return and call
May 31, 2018
a602a9f
Starts fixing code calls
May 31, 2018
d105c28
Fixes call.
teresalazar13 May 31, 2018
aec1841
Tries call with odd character.
teresalazar13 May 31, 2018
0324a86
Cleans.
teresalazar13 May 31, 2018
cac5905
Fixes calls.
teresalazar13 May 31, 2018
da95d6c
60 -> check.
teresalazar13 Jun 1, 2018
05a0746
100.
teresalazar13 Jun 1, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
516 changes: 516 additions & 0 deletions generate.c

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions generate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include "ast.h"
#include "symbol_table.h"


void generate_code(node_t *ast);
void generate_code_program(node_t *ast);
void generate_code_declaration(node_t *ast);
void generate_code_func_declaration(node_t *ast);
void generate_code_func_definition(node_t *ast);
void generate_code_return(node_t *ast);
void generate_code_assign_operator(node_t *ast);
void generate_code_arithmetic_operator(node_t *ast);
void generate_code_unary_operator(node_t *ast);
void generate_code_call(node_t *ast);
void generate_code_terminal(node_t *ast);

void print_param_types(param_type *params);
void print_param_types_and_ids(node_t *param_list);
void declare_param_declaration(node_t *param_list);

char *get_llvm_type(char *type_name);
6 changes: 3 additions & 3 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
lex $1.l
# o -v ou -t gera um ficheiro output com a tabela de parsing
yacc -d -v $1.y
cc -Wall -Wno-unused-function -o $1 y.tab.c lex.yy.c ast.c symbol_table.c semantics.c
./$1 -s < $2

cc -Wall -Wno-unused-function -o $1 y.tab.c lex.yy.c ast.c symbol_table.c semantics.c generate.c
./$1 < $2
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
5 changes: 2 additions & 3 deletions semantics.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ void check_declaration(node_t *declaration) {
score_aux2 = 3;
}
if (strcmp(aux_name, "short") == 0) {
score_aux = 2;
score_aux = 3;
}
if (strcmp(aux2_name, "short") == 0) {
score_aux2 = 2;
score_aux2 = 3;
}
if (strcmp(aux_name, "char") == 0) {
score_aux = 3;
Expand Down Expand Up @@ -393,7 +393,6 @@ void check_param_list(node_t *func_node, node_t *param_list, symbol *func, int i
new_symbol->is_param = 1;
}
}

else {
insert_type(param_type, func);
}
Expand Down
51 changes: 51 additions & 0 deletions symbol_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,54 @@ void show_tables() {
aux = aux->next;
}
}

void destroy_param_types(param_type *current) {
if (current == NULL) {
return;
}

if (current->name != NULL) {
free(current->name);
}

destroy_param_types(current->next);

free(current);
}

void destroy_symbols(symbol *current) {
if (current == NULL) {
return;
}

if (current->name != NULL) {
free(current->name);
}
if (current->type != NULL) {
free(current->type);
}
if (current->param != NULL) {
destroy_param_types(current->param);
}

destroy_symbols(current->next);

free(current);
}

void destroy_tables(table *current) {
if (current == NULL) {
return;
}

if (current->name != NULL) {
free(current->name);
}
if(current->symbol != NULL) {
destroy_symbols(current->symbol);
}

destroy_tables(current->next);

free(current);
}
4 changes: 4 additions & 0 deletions symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void show_symbol(symbol *symbol);
void show_table(table *table);
void show_tables();

void destroy_param_types(param_type *param);
void destroy_symbols(symbol *symbols);
void destroy_tables();

table *tables;
table *current;

Expand Down
Binary file added uccompiler
Binary file not shown.
29 changes: 29 additions & 0 deletions uccompiler.l
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "ast.h"
#include "symbol_table.h"
#include "semantics.h"
#include "generate.h"
int line = 1;
int col = 1;
int beginning_line;
Expand Down Expand Up @@ -127,6 +128,11 @@ CHRLIT_UNT \'({ANYCHAR}|{ESCAPE})*(\\)*{ANYCHAR}*
%%
int main(int argc, char *argv[]) {
if (argc > 1) {
if (strcmp(argv[1], "-1") == 0) {
print_tokens = 0;
yyparse();
yylex();
}
if (strcmp(argv[1], "-l") == 0) {
print_tokens = 1;
yyparse();
Expand All @@ -146,6 +152,18 @@ int main(int argc, char *argv[]) {
destroy_ast(ast);
}
}
else if (strcmp(argv[1], "-3") == 0) {
print_tokens = 0;
yyparse();

if(errors == 0) {
current = create_table("Global", 1);
current->print = 1;
insert_default_functions(current);
check_program(ast);
destroy_ast(ast);
}
}
else if (strcmp(argv[1], "-s") == 0) {
print_tokens = 0;
yyparse();
Expand All @@ -157,13 +175,23 @@ int main(int argc, char *argv[]) {
check_program(ast);
show_tables();
print_ast(ast, 0);
destroy_tables(tables);
destroy_ast(ast);
}
}
}
else {
print_tokens = 0;
yyparse();
if(errors == 0) {
current = create_table("Global", 1);
current->print = 1;
insert_default_functions(current);
check_program(ast);
generate_code(ast);
destroy_tables(tables);
destroy_ast(ast);
}
}
return 0;
}
Expand All @@ -177,3 +205,4 @@ void yyerror (char *s) {
int aux_col = col - strlen(yytext);
printf("Line %d, col %d: %s: %s\n", line, aux_col, s, yytext);
}