Skip to content
Snippets Groups Projects
Commit ed7cad81 authored by krf41037's avatar krf41037
Browse files

Create tac from expression

parent da44e281
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,9 @@ enum mcc_tac_operation{
};
// use quadruples like this : https://github.com/fauzanzaid/Quadruple-Three-Address-Code-in-C ??
struct mcc_tac{
enum mcc_tac_operation tac_op;
......@@ -57,6 +60,12 @@ struct mcc_tac{
};
struct mcc_tac *mcc_create_tac(struct mcc_ast_expression *expr, struct mcc_tac *tac);
struct mcc_tac *mcc_new_tac();
int mcc_tac_add(struct mcc_tac *tac_prev, struct mcc_tac *tac_add);
void mcc_tac_delete(struct mcc_tac *tac);
#endif
\ No newline at end of file
#include "mcc/tac_build.h"
struct mcc_tac *mcc_new_tac(enum mcc_tac_operation op,
struct mcc_tac *prev,
struct mcc_tac *next)
struct mcc_tac *mcc_new_tac()
{
struct mcc_tac *tac = malloc(sizeof(tac));
tac -> tac_op = op;
if (!tac) {
return NULL;
}
tac -> tac_op = NULL;
tac -> arg1 = NULL;
tac -> arg2 = NULL;
tac -> result = NULL;
tac -> prev = prev;
tac -> next = next;
tac -> prev = NULL;
tac -> next = NULL;
return tac;
}
struct mcc_tac *mcc_create_tac(struct mcc_ast_expression *expr, struct mcc_tac *tac)
{
switch (expr->type)
{
case MCC_AST_EXPRESSION_TYPE_PARENTH:
mcc_create_tac(expr->expression,tac);
break;
case MCC_AST_EXPRESSION_TYPE_BINARY_OP:
struct mcc_tac *tac_bin = mcc_new_tac();
if(expr->lhs->type == MCC_AST_EXPRESSION_TYPE_LITERAL ){
tac_bin->arg1 = ast_literal_to_char(expr->lhs,tac);
}
if(expr->rhs->type == MCC_AST_EXPRESSION_TYPE_LITERAL){
// TODO literal to char
tac_bin->arg2 = ast_literal_to_char(expr->rhs,tac);
}
case MCC_AST_EXPRESSION_TYPE_CALL_EXPRESSION:
case MCC_AST_EXPRESSION_TYPE_UNARY_OP:
case MCC_AST_EXPRESSION_TYPE_LITERAL:
//TODO parse MCC_AST types to MCC_TAC_OP types
break;
default:
break;
}
}
char *ast_literal_to_char(struct mcc_ast_expression *expr, struct mcc_tac *tac){
char *string;
switch (expr->literal->type) {
case MCC_AST_DATA_TYPE_INT:
string = malloc(sizeof(char) * (myLog10(expr->literal->i_value, 0) + 2));
sprintf(string, "%ld", expr->literal->i_value);
break;
case MCC_AST_DATA_TYPE_FLOAT:
string = malloc(sizeof(char) * 64);
sprintf(string, "%lf", expr->literal->f_value);
break;
case MCC_AST_DATA_TYPE_STRING:
string = expr->literal->s_value;
break;
case MCC_AST_DATA_TYPE_BOOL:
string = expr->literal->b_value;
break;
case MCC_AST_DATA_TYPE_VOID:
string = "void";
break;
default:
string = "Error ast_literal_to_char";
}
return string;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment