Skip to content

Commit

Permalink
Added semantics tests and caller program to help tests tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbechlin committed Jun 15, 2017
1 parent c575217 commit 9651c9c
Show file tree
Hide file tree
Showing 39 changed files with 882 additions and 78 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC := gcc

etapa3: hashmap.o astree.o symbol_table.o parser.o scanner.o main.o
etapa4: hashmap.o astree.o symbol_table.o parser.o scanner.o main.o
$(CC) -g -o $@ $^

scanner.c: scanner.l
Expand All @@ -12,9 +12,9 @@ parser.c: parser.y
%.o: %.c
$(CC) -c $<

etapa3.tgz: clean
tar cvfz etapa3.tgz *
etapa4.tgz: clean
tar cvfz etapa4.tgz *

.PHONY: clean
clean:
rm -rf parser.c parser.h scanner.c etapa3 etapa3.tgz *.o *~
rm -rf parser.c parser.h scanner.c etapa* etapa*.tgz *.o *~
29 changes: 25 additions & 4 deletions astree.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,17 @@ static int resolve_expr_type(struct astree *tree){

switch (tree->type) {
case AST_SYM:
switch(((struct symtab_item *)tree->symbol->value)->code){
case SYMBOL_LIT_INT:
case SYMBOL_LIT_REAL:
case SYMBOL_LIT_CHAR:
return ((struct symtab_item *)tree->symbol->value)->data_type;
case SYMBOL_IDENTIFIER:
if(((struct symtab_item *)tree->symbol->value)->id_type == ID_VAR)
return ((struct symtab_item *)tree->symbol->value)->data_type;
case SYMBOL_LIT_STRING:
return TP_INCOMP;
}
return ((struct symtab_item *)tree->symbol->value)->data_type;

/* Identifiers and Symbols
Expand Down Expand Up @@ -662,7 +673,7 @@ static void second_pass(struct astree *tree, struct hashmap *declared_variables)

/* Iterating in arguments*/
while((call_args != NULL) && (func_args != NULL)){
func_type = ast_keyword_to_data_type(func_args->children[1]->type);
func_type = ((struct symtab_item*)func_args->children[2]->symbol->value)->data_type;
call_type = resolve_expr_type(call_args->children[1]);

if((func_type & call_type) == TP_INCOMP){
Expand Down Expand Up @@ -732,7 +743,7 @@ static void second_pass(struct astree *tree, struct hashmap *declared_variables)
exit(4);
}

if(vec_type & exp_type == TP_INCOMP){
if((vec_type & exp_type) == TP_INCOMP){
fprintf(stderr,
"SEMANTIC ERROR: Attribution to vector '%s' with a incompatible type.\n",
tree->children[0]->symbol->key);
Expand All @@ -756,7 +767,7 @@ static void second_pass(struct astree *tree, struct hashmap *declared_variables)
var_type = info->data_type;
exp_type = resolve_expr_type(tree->children[1]);

if(var_type & exp_type == TP_INCOMP){
if((var_type & exp_type) == TP_INCOMP){
fprintf(stderr,
"SEMANTIC ERROR: Attribution to variable '%s' with a incompatible type.\n",
tree->children[0]->symbol->key);
Expand All @@ -771,7 +782,7 @@ static void second_pass(struct astree *tree, struct hashmap *declared_variables)
int func_type, ret_type;
struct astree* func = (struct astree*)tree->symbol->value;

func_type = ast_keyword_to_data_type(func->children[0]->children[0]->type);
func_type = ((struct symtab_item*)func->children[0]->children[1]->symbol->value)->data_type;
ret_type = resolve_expr_type(tree->children[0]);

if((func_type & ret_type) == TP_INCOMP){
Expand All @@ -780,7 +791,17 @@ static void second_pass(struct astree *tree, struct hashmap *declared_variables)
func->children[0]->children[1]->symbol->key);
exit(4);
}
}

if (tree->type == AST_READ){
/* TODO: check if tree->children[0] is a variable*/
struct symtab_item* info = (struct symtab_item *)tree->children[0]->symbol->value;

if (info->id_type != ID_VAR){
fprintf(stderr,
"SEMANTIC ERROR: Identifier '%s' isn't a variable to be user in read statement.\n",
tree->children[0]->symbol->key);
exit(4);
}
}
}
Loading

0 comments on commit 9651c9c

Please sign in to comment.