Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Luís Möllmann committed Jun 5, 2017
1 parent 4aedfa0 commit 8c183d3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 54 deletions.
40 changes: 20 additions & 20 deletions astree.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void print_identation(FILE* stream, int level){
fprintf(stream, " ");
}

static void ast_annotate(struct astree* tree, struct hashmap *declared_variables);
static void first_pass(struct astree* tree, struct hashmap *declared_variables);

/* `yes` is a dummy variable. Our hashmap implementation is being used here as
* a hashset. I.e., we only care about the keys. So `yes` is just a constant
Expand All @@ -369,8 +369,8 @@ static void annotate_declaration(struct astree *tree, struct hashmap *declared_v
case AST_VEC:
id_node = tree->children[0];
type_node = tree->children[1];
ast_annotate(tree->children[2], declared_variables);
ast_annotate(tree->children[3], declared_variables);
first_pass(tree->children[2], declared_variables);
first_pass(tree->children[3], declared_variables);
break;
case AST_FHEADER:
id_node = tree->children[1];
Expand All @@ -381,7 +381,7 @@ static void annotate_declaration(struct astree *tree, struct hashmap *declared_v
* Right now, this accomplishes nothing, since this node is of type
* AST_IDENTIFIER. Test program with the argument of `tests/fun_dec.txt`
* to see. */
ast_annotate(tree->children[2], declared_variables);
first_pass(tree->children[2], declared_variables);
break;
}

Expand Down Expand Up @@ -449,34 +449,34 @@ void ast_semantic_check(struct astree *tree) {
struct hashmap declared_variables;
hm_initialize(20, 0.6, sizeof(char), &declared_variables);

ast_annotate(tree, &declared_variables);
hm_fprint(stderr, &declared_variables, 0);
first_pass(tree, &declared_variables);

hm_terminate(&declared_variables);
}

/* Traverses the tree annotating the type of the symbols in the symbol table.
* Annotates `data_type` for all symbols and `id_type` for identifiers. */
static void ast_annotate(struct astree* tree, struct hashmap *declared_variables) {
/* Traverses tree annotating:
* 1. Redeclarations
* 2. data_type in symbol table
* 3. id_type in symbol table */
static void first_pass(struct astree *tree, struct hashmap *declared_variables) {
if (tree == NULL) {
return;
}

/* Annotating children before the root seems more intuitive, but I don't think
* it makes any difference. */
for (int i = 0; i < AST_MAXCHILDREN; i++) {
ast_annotate(tree->children[i], declared_variables);
first_pass(tree->children[i], declared_variables);
}

int node_type = tree->type;
int sym = node_type == AST_SYM;
int decl = (node_type == AST_VAR)
|| (node_type == AST_VEC)
|| (node_type == AST_FHEADER);

if (sym) {
annotate_symbol(tree);
} else if (decl) {
annotate_declaration(tree, declared_variables);
switch (tree->type) {
case AST_SYM:
annotate_symbol(tree);
break;
case AST_VAR:
case AST_VEC:
case AST_FHEADER:
annotate_declaration(tree, declared_variables);
break;
}
}
66 changes: 37 additions & 29 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,53 @@
#include "symbol_table.h"
#include "astree.h"

extern int set_input_file(char* file_name);
extern FILE *set_input_file(char* file_name);
extern int yyparse(void);
extern struct astree *program;

void main(int argc, char* argv[]){
int i;
FILE *out = stdout;
symtab_init();
if(argc > 3){
fprintf(stderr,
"ERRO:\n\t Arguments not suported.\nUSAGE:\
\n\t [input file name]: name of file to be analysed.\
\n\t [output file name]: name of file to write source code.\n");
exit(1);
}
if(argc > 2) {
if(!set_input_file(argv[1])){
fprintf(stderr, "ERRO:\n\t File [%s] not found.\n", argv[1]);
exit(2);
}
if(!(out = fopen(argv[2], "w"))){
fprintf(stderr, "ERRO:\n\t File [%s] can't to be open.\n", argv[2]);
exit(2);
}
FILE *out, *in;
switch (argc) {
case 1:
in = stdin;
out = stdout;
break;
case 2:
in = set_input_file(argv[1]);
out = stdout;
break;
case 3:
in = set_input_file(argv[1]);
out = fopen(argv[2], "w");
break;
default:
fprintf(stderr, "Usage: %s <input file> <output file>.\n", argv[0]);
exit(1);
break;
}
if(argc > 1) {
if(!set_input_file(argv[1])){
fprintf(stderr, "ERRO:\n\t File [%s] not found.\n", argv[1]);
exit(2);
}

if (!out || !in) {
fprintf(stderr, "Input or output file not found");
exit(2);
}
yyparse();
printf("SUCESS:\n\t Program was accepted.\n");

symtab_init();
int status = yyparse();
fprintf(stderr, "------------- Parsing successful\n");

ast_semantic_check(program);
fprintf(stderr, "------------- Semantic check successful\n");

fprintf(stderr, "------------- Symbol table:\n");
symtab_print();
printf("SOURCE:\n");
ast_make_source(out, program, 0);

fprintf(stderr, "------------- Abstract syntax tree\n");
ast_fprint(stdout, 0, program);

fprintf(stderr, "------------- Source code (check output file, if this is empty):\n");
ast_make_source(out, program, 0);

fprintf(stderr, "------------- Exiting...\n");
symtab_destroy();
ast_terminate(program);
exit(0);
Expand Down
8 changes: 4 additions & 4 deletions scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "symbol_table.h"
#include "parser.h"

int set_input_file(char* file_name);
FILE *set_input_file(char* file_name);

int getLineNumber(void);
int isRunning(void);
Expand Down Expand Up @@ -83,10 +83,10 @@ int yywrap(void){
return 1;
}

int set_input_file(char* file_name){
FILE *set_input_file(char* file_name){
if(yyin = fopen(file_name, "r"))
return 1;
return 0;
return yyin;
return NULL;
}

int getLineNumber(void){
Expand Down
1 change: 0 additions & 1 deletion symbol_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ struct hm_item *symtab_insert(char* symbol, int code){
}

void symtab_print(void){
printf("\nSYMBOLS TABLE:\n");
hm_fprint(stdout, &hash, 0);
}

Expand Down

0 comments on commit 8c183d3

Please sign in to comment.