Skip to content

Commit

Permalink
ETAPA 4 PRONTA AEEEEE
Browse files Browse the repository at this point in the history
  • Loading branch information
fkeglevich committed Jun 20, 2017
1 parent ad730fc commit 523e19a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
2 changes: 1 addition & 1 deletion hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void hash_print()
node = table[i];
while (node != NULL)
{
printf("Endereco: %d\t%s\t(%d)\tTexto: %s\n", i, getTokenName(node->type), node->type, node->text);
printf("Endereco: %d\t%s\t(%d)\tDatatype: %d\tTexto: %s\n", i, getTokenName(node->type), node->type, node->dataType, node->text);
node = node->next;
}
}
Expand Down
10 changes: 6 additions & 4 deletions saida.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
v : long [ 4 ] 0 0 0 0 ;
a2 : short 0 ;
long incn2 ( long x , float n ) return 2.2 ;
long incn2 ( long x2 , float n ) return main();
short eno ( ) {
a2 = 2 ;
a2 = main();
a2 = eno();
};
long incn3 ( ) return main();
long incn ( long x , long n ) {
long incn ( long x , long lastpar ) {
a2 = eno();
print "a didi ua" "asdaaar" ;
return a2 + n ;
return a2 + lastpar * (lastpar + lastpar - 2 )+ incn2(v[2 ]+ 1 , 2.2 );
};
long main ( ) {
a2 = 2 ;
a2 = incn(2 , 2.3 );
};
short voidfunc ( ) ;
12 changes: 7 additions & 5 deletions sample2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ v: long[4] 0 0 0 0;

a2: short 0;

long incn2 (long x, float n)
return 2.2;
long incn2 (long x2, float n)
return main();

short eno()
{
a2 = 2;
a2 = main();
a2 = eno();
};

long incn3 ()
return main();

long incn (long x, long n)
long incn (long x, long lastpar)
{
a2 = eno();
print "a didi ua" "asdaaar";
return a2+n;
return a2 + lastpar * (lastpar + lastpar - 2) + incn2(v[2] + 1, 2.2);
};


Expand All @@ -31,6 +32,7 @@ long main()
a2 = 2;
//v#3.4 = 2;
//v#2 = v[10-1];
a2 = incn(2,2.3);
};

short voidfunc() ;
Expand Down
81 changes: 74 additions & 7 deletions semantic.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ void semanticError(const char * msg, int lineNumber)
void checkIdentifierExists(HASH_NODE *node, int lineNumber)
{
if (node->dataNature == NATURE_UNKNOWN)
{
printf("Identificador: %s\n", node->text);
semanticError("Identificador nao-declarado!", lineNumber);
}
}

void checkNature(HASH_NODE *node, int expectedNature, int lineNumber)
Expand Down Expand Up @@ -200,6 +203,35 @@ int getExpressionDataType(AST_NODE *node)
}
}

/*
lista_parametros
: declara_parametro {$$ = $1;}
| declara_parametro ',' lista_parametros {$$ = ast_insert(AST_ARGS_LIST, NULL, $1, $3, NULL, NULL);}
| {$$ = NULL;}
;
declara_parametro: tipo_variavel TK_IDENTIFIER {$$ = ast_insert(AST_DEC_ARGS, $2, $1, NULL, NULL, NULL);};
*/
void updateParamsDatatypes(AST_NODE *node)
{
if (node == NULL)
return;

if (node->type == AST_DEC_ARGS)
{
if (node->symbol->dataNature != NATURE_UNKNOWN)
semanticError("Parametro com nome ambiguo!", node->lineNumber);

node->symbol->dataType = getDataTypeFromVarType(node->children[0]);
node->symbol->dataNature = NATURE_VAR;
}
else if (node->type == AST_ARGS_LIST)
{
updateParamsDatatypes(node->children[0]);
updateParamsDatatypes(node->children[1]);
}
}

void checkDeclr(AST_NODE *node)
{
if (node->symbol->dataNature != NATURE_UNKNOWN)
Expand All @@ -219,26 +251,34 @@ void checkDeclr(AST_NODE *node)
case AST_FUNC_DEC:
node->symbol->dataNature = NATURE_FUNC;
node->symbol->funcParam = node->children[1];
checkProgram(node->children[2]);
updateParamsDatatypes(node->children[1]);
//fullCheck(node->children[2]);
break;
}
}

void fullCheck(AST_NODE *node);

void checkDeclrFull(AST_NODE *node)
{
//checkDeclr(node);
if (node->type == AST_FUNC_DEC)
fullCheck(node->children[2]);
}

void checkChildren(AST_NODE *node)
{
int i;
for (i = 0; i < NUM_CHILDREN; i++)
{
checkProgram(node->children[i]);
}
fullCheck(node->children[i]);
}

//TK_IDENTIFIER '=' expressao
//tem que ser var
//tem que existir
//tem que ter tipos compativeis, number/float <--> number/float ou bool <-> bool

void checkProgram(AST_NODE *node)
void fullCheck(AST_NODE *node)
{
if (node == NULL) return;

Expand All @@ -251,7 +291,7 @@ void checkProgram(AST_NODE *node)
case AST_VAR_DEC:
case AST_VEC_DEC:
case AST_FUNC_DEC:
checkDeclr(node);
checkDeclrFull(node);
break;

//: TK_IDENTIFIER '=' expressao {$$ = ast_insert(AST_ATRIB, $1, $3, NULL, NULL, NULL);}
Expand Down Expand Up @@ -287,4 +327,31 @@ void checkProgram(AST_NODE *node)
checkChildren(node);
break;
}
}
}

void firstTreePass(AST_NODE *node)
{
int i;

if (node == NULL) return;

switch(node->type)
{
case AST_PROGRAM:
for (i = 0; i < NUM_CHILDREN; i++)
firstTreePass(node->children[i]);
break;

case AST_VAR_DEC:
case AST_VEC_DEC:
case AST_FUNC_DEC:
checkDeclr(node);
break;
}
}

void checkProgram(AST_NODE *node)
{
firstTreePass(node);
fullCheck(node);
}
5 changes: 3 additions & 2 deletions verificacoes
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PS3: Temos 3 literais, inteiros, caracteres e strings (OK)

Mais veriicacoes chatas:

Percorrer a AST duas vezes (a primeira só com as declarações básicas)
(OK) Percorrer a AST duas vezes (a primeira só com as declarações básicas)

(OK) Literais strings so podem ser usadas no comando print

Expand All @@ -36,4 +36,5 @@ Percorrer a AST duas vezes (a primeira só com as declarações básicas)
(OK) A unica incompatibilidadeque existe nos literais numericos e com os valores booleanos q NAO PODEM se misturar
Nota: checar atribuição e expressão

Verificar o retorno da funcao (só checar que o datatype da expressao do "return {expr}" coiseia com o da função que ele tá retornando)
(OK) Verificar o retorno da funcao (só checar que o datatype da expressao do "return {expr}" coiseia com o da função que ele tá retornando)
Nota: o retorno pode ser qqlr coisa exceto bool ;)

0 comments on commit 523e19a

Please sign in to comment.