Skip to content

Commit

Permalink
Add if/elseif/else structure, while is working
Browse files Browse the repository at this point in the history
  • Loading branch information
jefersonla committed Nov 6, 2016
1 parent 1000e18 commit 806bb1a
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 28 deletions.
2 changes: 1 addition & 1 deletion analisador-lexico.l
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ int main( int argc, char **argv ) {

break;
default:
printf( "Incorrect number of parrameters used!\n\n");
printf( "Incorrect number of parrameters used!\n\n");
showHelpUsage();
return EXIT_FAILURE;
}
Expand Down
253 changes: 237 additions & 16 deletions codegen_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,27 @@ bool cgenCallFunction(TokenNode *call_function_token, SymbolTable *actual_symbol
/**
* Generate code for assign.
*
* @param _token
* @param assign_token Token with the assign operations
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenAssign(TokenNode *assign_token, SymbolTable *actual_symbol_table){
// TODO! Implement codegenerator assign
printTodo("'cgenAssign' - NOT IMPLEMENTED YET!");
return false;
/* Check if command list token is null */
if(assign_token == NULL){
printError("ASSIGN TOKEN IS INVALID!");
return false;
}

/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}

/* */

/* Return success */
return true;
}

/**
Expand Down Expand Up @@ -280,19 +293,19 @@ bool cgenWhile(TokenNode *while_token, SymbolTable *previous_symbol_table){
}

/* Add header message */
addInstructionMainQueue(mips_start_while, loop_while_counter);
addInstructionMainQueueFormated(mips_start_while, loop_while_counter);

/* CGEN(exp) */
cgenExpression(exp_token, previous_symbol_table);

/* Check condition */
addInstructionMainQueue(mips_check_while, loop_while_counter);
addInstructionMainQueueFormated(mips_check_while, loop_while_counter);

/* CGEN(bloco) */
cgenBlock(block_token, newSymbolTable);
cgenBlockCode(block_token, new_symbol_table);

/* Add while end */
addInstructionMainQueue(mips_end_while, loop_while_counter, loop_while_counter);
addInstructionMainQueueFormated(mips_end_while, loop_while_counter, loop_while_counter);

/* Increment while loop counter */
loop_while_counter += 1;
Expand All @@ -304,27 +317,235 @@ bool cgenWhile(TokenNode *while_token, SymbolTable *previous_symbol_table){
/**
* Generate code for for.
*
* @param _token
* @param for_token token for.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table){
// TODO! Implement codegenerator for 'for' loop structure
printTodo("'cgenFor' - NOT IMPLEMENTED YET!");
return false;

/* Check if for token is null */
if(for_token == NULL){
printError("IF COMMAND IS INVALID!");
return false;
}

/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}

printTodo("NOT IMPLEMENTED YET!");

/* Return success */
return false;
}

/**
* Generate code for if.
*
* @param _token
* @param if_token If comand token structure.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
// TODO! Implement codegenerator for 'if' structure
printTodo("'cgenIf' - NOT IMPLEMENTED YET!");
return false;
int i;
TokenNode *exp_token;
TokenNode *block_token;
TokenNode *list_elseif;
TokenNode *token_node;
TokenList *exp_list;
TokenList *block_list;
SymbolTable *new_symbol_table;

/* Check if for token is null */
if(if_token == NULL){
printError("IF COMMAND IS INVALID!");
return false;
}

/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}

/* Get expression */
exp_token = listGetTokenByIndex(if_token->child_list, 2);

/* Check if this expression is null */
if(exp_token == NULL){
printError("EXPRESSION TOKEN IS NULL!");
return false;
}

/* Get bloco */
block_token = listGetTokenByIndex(if_token->child_list, 4);

/* Check if block token is null */
if(block_token == NULL){
printError("BLOCK TOKEN IS NULL!");
return false;
}

/* Get list of elseif conditions */
list_elseif = listGetTokenByIndex(if_token->child_list, 5);

/* Check if block token is null */
if(list_elseif == NULL){
printError("INVALID TOKEN LIST ESLE IF!");
return false;
}

/* Add header message */
addInstructionMainQueue(mips_start_if);

/* CGEN(exp) */
cgenExpression(exp_token, actual_symbol_table);

/* Check condition */
addInstructionMainQueueFormated(mips_check_if, cond_if_counter, 0);

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
printError("FAILED TO CREATE LOCAL SYMBOL TABLE!");
return false;
}

/* CGEN(bloco) */
cgenBlockCode(block_token, new_symbol_table);

/* Delete the temporary escope */
deleteSymbolTable(&new_symbol_table);

/* Add check for next if condition {else if / else} */
addInstructionMainQueueFormated(mips_next_if, cond_if_counter, cond_if_counter, 0);

/* Check if there are a list of else if */
if(list_elseif->token_type == TI_LIST_ELSEIF){

/* Get all expressions */
exp_list = newTokenList();

/* Check if the list of expression list is valid */
if(exp_list == NULL){
printError("EXPRESSION LIST IS INVALID!");
return false;
}

/* Pick all expressions in elseif */
for(i = 1; i <= list_elseif->child_list->length; i++){

/* Get the token node */
token_node = listGetTokenByIndex(list_elseif->child_list, i);

/* If this token is an expression add him to the token list */
if(IS_EXPRESSION(token_node->token_type)){
listAddToken(exp_list, token_node);
}
}

/* Get all blocks */
block_list = listGetTokensByType(list_elseif->child_list, TI_BLOCO);

/* Check if block list is valid */
if(block_list == NULL){
printError("BLOCK LIST IS INVALID!");
return false;
}

/* Add the other 'else if' conditionals */
for(i = 1; i <= exp_list->length; i++){

/* Get expression[i] */
exp_token = listGetTokenByIndex(exp_list, i);

/* Check if this expression is null */
if(exp_token == NULL){
printError("EXPRESSION TOKEN IS NULL!");
return false;
}

/* Get block[i] */
block_token = listGetTokenByIndex(block_list, i);

/* Check if this expression is null */
if(block_token == NULL){
printError("BLOCK TOKEN IS NULL!");
return false;
}

/* Header for elseif */
addInstructionMainQueueFormated(mips_elseif_start, cond_if_counter, i);

/* CGEN(exp) */
cgenExpression(exp_token, actual_symbol_table);

/* Check condition */
addInstructionMainQueueFormated(mips_check_if, cond_if_counter, i);

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
printError("FAILED TO CREATE LOCAL SYMBOL TABLE!");
return false;
}

/* CGEN(bloco) */
cgenBlockCode(block_token, new_symbol_table);

/* Delete the temporary escope */
deleteSymbolTable(&new_symbol_table);

/* Add check for next if condition {else if / else} */
addInstructionMainQueueFormated(mips_next_if, cond_if_counter, cond_if_counter, i);
}
}

/* Check if 'if token' has a else condition */
if(if_token->token_type == TI_IF_ELSE){

/* Get block else */
block_token = listGetTokenByIndex(if_token->child_list, 7);

/* Check if this expression is null */
if(block_token == NULL){
printError("BLOCK TOKEN IS NULL!");
return false;
}

/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table);

/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
printError("FAILED TO CREATE LOCAL SYMBOL TABLE!");
return false;
}

/* Header for else */
addInstructionMainQueueFormated(mips_else_start, cond_if_counter);

/* CGEN(bloco) */
cgenBlockCode(block_token, new_symbol_table);

/* Delete the temporary escope */
deleteSymbolTable(&new_symbol_table);
}

/* Add if footer */
addInstructionMainQueueFormated(mips_end_if, cond_if_counter);

/* Increment if counter */
cond_if_counter += 1;

/* Return success */
return true;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions codegen_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ extern const char mips_next_if[];
/* If end condition */
extern const char mips_end_if[];

/* Header for else if */
extern const char mips_elseif_start[];

/* Header for else */
extern const char mips_else_start[];

/* Loop type while */
extern const char mips_start_while[];

Expand Down
30 changes: 26 additions & 4 deletions codegen_models.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const char mips_footer[] =
"\n"
"end_check_nil:\n"
"\tlw $ra, 0($fp)\n"
"\taddiu $sp, $sp, 8\n"
"\tlw $fp, 0($sp)\n"
"\taddiu $sp, $sp, 4\n"
"\tlw $fp, 4($sp)\n"
"\tjr $ra\n"
"# -- End Check Nil -- #\n"
"\n"
Expand Down Expand Up @@ -115,8 +115,8 @@ const char mips_footer[] =
"\n"
"\t# Close Print Function \n"
"\tlw $ra, 0($fp)\n"
"\taddiu $sp, $sp, 8\n"
"\tlw $fp, 0($sp)\n"
"\taddiu $sp, $sp, 4\n"
"\tlw $fp, 4($sp)\n"
"\tjr $ra\n"
"# -- End Print Function -- #\n"
"\n"
Expand Down Expand Up @@ -370,6 +370,14 @@ const char mips_check_if[] =
const char mips_next_if[] =
"\tj end_if_%d\n"
"end_if_%d_exp_%d:\n";

/* Header for else if */
const char mips_elseif_start[] =
"\t### if - %d -- elseif - %d ###\n";

/* Header for else */
const char mips_else_start[] =
"\t### if - %d -- else ###\n";

/* If end condition */
const char mips_end_if[] =
Expand Down Expand Up @@ -555,6 +563,7 @@ const char mips_end_function_def2[] =

const char mips_end_function_call[] =
"\tjal function_%s\n"
"\taddiu $sp, $sp, 4\n"
"\t# ^----------- End of Call Function ------------^ #\n";

/* ------------------------------------------------------------- */
Expand Down Expand Up @@ -647,6 +656,19 @@ const char mips_or_sc_footer[] =
"\tend_or_%d:\n"
"\t# ^-------- End of 'or' short-circuit ----------^ #\n";

/* ------------------------------------------------------------- */
/* Assign Model */
/* ------------------------------------------------------------- */

/**
* Model for assigns.
*
* Default Model for assign CGEN():
* ...
*/



/* ------------------------------------------------------------- */
/* .......................... */
/* ------------------------------------------------------------- */
Expand Down
Loading

0 comments on commit 806bb1a

Please sign in to comment.