Newer
Older
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
static void parse_expression_identifier(struct mcc_ast_identifier *identifier, struct mcc_tac *tac) {
// Get expression type from symbol table
enum mCc_ast_type expression_type =
mCc_symbol_table_check_get_expression_type(
builder->root_st, builder->current_st, expression);
// Get tac copy operation type
enum mCc_tac_operation_type type = get_copy_type(expression_type);
// Create arg1 from identifier
char *arg1 = id->name;
create_and_add_line_new_temp(builder, type, arg1, NULL);
}
void mcc_tac_parse_expression(struct mcc_ast_expression *expression, struct mcc_tac *tac) {
assert(expression);
assert(tac);
switch (expression -> type) {
case MCC_AST_EXPRESSION_TYPE_IDENTIFIER:
char *temp =
break;
default:
return;
struct mcc_tac *mcc_tac_parse_statement_if(struct mcc_ast_statement *if_st, struct mcc_tac *tac) {
// create new tac
// jumpfalse
// true condition
// false condition
void mcc_tac_parse_statement_list(struct mcc_ast_statement_list *stl, struct mcc_tac *tac) {
assert(stl);
assert(tac);
while (stl != NULL) {
mcc_tac_parse_statement(stl -> statement, tac);
stl = stl -> next;
}
}
struct mcc_tac *mcc_tac_parse_statement(struct mcc_ast_statement *statement, struct mcc_tac *tac) {
switch (statement -> type) {
case MCC_AST_STATEMENT_TYPE_COMPOUND:
mcc_tac_parse_statement_list(statement -> statement_list, tac);
case MCC_AST_STATEMENT_TYPE_IF:
return NULL;
}
// -------------------------------- parse function
int mcc_tac_parse_function(struct mcc_ast_function *f, struct mcc_tac *tac) {
assert(f);
assert(tac);
// TODO: do something with params
if (f->statement != NULL) {
mcc_tac_parse_statement(f -> statement, tac);
// start parsing program
struct mcc_tac *mcc_tac_build(struct mcc_ast_program *program, struct mcc_symbol_table *st) {
struct mcc_tac *tac = mcc_tac_new(NULL);
struct mcc_ast_function *f = NULL;
// look for main an start parsing program
for (int i = 0; i < program -> function_def -> size; i++) {
f = program -> function_def -> arr[i];
if (strcmp(f -> identifier -> i_value, "main") == 0) {
mcc_tac_parse_function(f, tac);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
// for (int i = 0; i < program -> function_def -> size; i++) {
// struct mcc_ast_function *f = program -> function_def -> arr[i];
//
// mcc_tac_parse_function(f, tac);
// }
return tac;
}
//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)); // why log10?
// 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 ? "1" : "0";
// break;
// case MCC_AST_DATA_TYPE_VOID:
// string = "void";
// break;
//
// default:
// string = "Error ast_literal_to_char";
// }
//
// return string;
//}
//
//static struct mcc_tac *new_tac_binary(struct mcc_ast_expression *expr) {
// 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);
// }
//
//}
//
//struct mcc_tac *mcc_create_tac(struct mcc_ast_expression *expr, struct mcc_tac *tac) {
// struct mcc_tac *next_tac = malloc(sizeof(*next_tac));
//
// // add new tac object to linked list
// next_tac -> prev = tac;
// next_tac -> next = NULL;
// tac -> next = next_tac;
//
//
//
//
//// struct mcc_tac *tac_bin;
//// switch (expr->type) {
////
//// case MCC_AST_EXPRESSION_TYPE_PARENTH:
//// mcc_create_tac(expr->expression,tac);
//// break;
//// case MCC_AST_EXPRESSION_TYPE_BINARY_OP:
//// 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;
//// }
//}