aboutsummaryrefslogtreecommitdiffstats
path: root/src/backend/targets/C
diff options
context:
space:
mode:
authorGalin Simeonov <gts@volconst.com>2021-06-04 16:42:52 +0300
committerGalin Simeonov <gts@volconst.com>2021-07-15 18:04:59 +0300
commita26684a417729699e95b335a3d00798237ffba9b (patch)
tree6cd73bc90fbe5fd435b8cc721455d0a70420a4ac /src/backend/targets/C
parentb845e4754be86d2216733d9bea75cb301f38739d (diff)
downloadMEGATRON-a26684a417729699e95b335a3d00798237ffba9b.tar.gz
added the if statement
Diffstat (limited to 'src/backend/targets/C')
-rw-r--r--src/backend/targets/C/ast_to_c.c76
-rw-r--r--src/backend/targets/C/ast_to_c.h4
2 files changed, 79 insertions, 1 deletions
diff --git a/src/backend/targets/C/ast_to_c.c b/src/backend/targets/C/ast_to_c.c
index 3ce923d..9c8b38d 100644
--- a/src/backend/targets/C/ast_to_c.c
+++ b/src/backend/targets/C/ast_to_c.c
@@ -189,7 +189,8 @@ void ast_transitions_of_state_to_c(FILE *out,struct AST_Machine *machine,struct
ast_event_to_c_enum(out,machine,vector->transitions[i]->event);
fprintf(out,":\n");
- ast_pipeline_to_c(out,3,vector->transitions[i]->pipeline);
+ //ast_pipeline_to_c(out,3,vector->transitions[i]->pipeline);
+ ast_statement_to_c(out,3,vector->transitions[i]->statement);
fprintf(out,"\t\t\tmachine_states[");
ast_token_to_c(out,machine->id);
fprintf(out,"]=");
@@ -370,4 +371,77 @@ void ast_to_c_print_comment(FILE *out,char *comment)
fprintf(out,"%s\n",comment);
fprintf(out,"*/\n");
}
+void ast_statement_to_c(FILE *out,size_t indentation,struct AST *statement)
+{
+ if(statement==NULL)return;
+ if(statement->type==AST_TYPE_PIPELINE)
+ ast_pipeline_to_c(out,indentation,(struct AST_Pipeline*)statement);
+ else if(statement->type==AST_TYPE_IF)
+ ast_if_to_c(out,indentation,(struct AST_If_Statement*)statement);
+ else
+ assert(0);
+
+}
+void ast_if_to_c(FILE *out,size_t indentation,struct AST_If_Statement *statement)
+{
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"if");
+ ast_expression_to_c(out,statement->condition);
+ fprintf(out,"\n");
+
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"{\n");
+
+ ast_statement_to_c(out,indentation+1,statement->body);
+
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"}");
+ if(statement->else_statement)
+ {
+ fprintf(out,"else{\n");
+
+ ast_statement_to_c(out,indentation+1,statement->else_statement);
+
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"}");
+ }
+ fprintf(out,"\n");
+}
+void ast_expression_to_c(FILE *out,struct AST *expression)
+{
+ fprintf(out,"(");
+ switch(expression->type)
+ {
+ case AST_TYPE_OP_OR:
+ ast_expression_to_c(out,AS_BIN_EXPR_PTR(expression)->left);
+ fprintf(out,"||");
+ ast_expression_to_c(out,AS_BIN_EXPR_PTR(expression)->right);
+ break;
+ case AST_TYPE_OP_NOT:
+ fprintf(out,"!");
+ ast_expression_to_c(out,AS_UN_EXPR_PTR(expression)->operand);
+ break;
+ case AST_TYPE_OP_AND:
+ ast_expression_to_c(out,AS_BIN_EXPR_PTR(expression)->left);
+ fprintf(out,"&&");
+ ast_expression_to_c(out,AS_BIN_EXPR_PTR(expression)->right);
+ break;
+ case AST_TYPE_STATE:
+ ast_expression_state_to_c(out,(struct AST_State*)expression);
+ break;
+ case AST_TYPE_OP_SELECTOR:
+ assert(!"selector in final product!\n");
+ default:
+ assert(0);
+ }
+ fprintf(out,")");
+}
+void ast_expression_state_to_c(FILE *out,struct AST_State *state)
+{
+ fprintf(out,"machine_states[");
+ ast_token_to_c(out,state->parent->id);
+ fprintf(out,"]==");
+ ast_state_to_c_function_name(out,state->parent,state);
+
+}
#endif
diff --git a/src/backend/targets/C/ast_to_c.h b/src/backend/targets/C/ast_to_c.h
index fb3e729..299d6db 100644
--- a/src/backend/targets/C/ast_to_c.h
+++ b/src/backend/targets/C/ast_to_c.h
@@ -44,6 +44,10 @@ void ast_machines_to_c_array(FILE *out,struct AST_Translation_Unit *translation_
void ast_machines_to_c_enum(FILE *out,struct AST_Translation_Unit *translation_unit);
void ast_event_to_c_enum(FILE *out,struct AST_Machine *machine,struct AST_Event *event);
+void ast_statement_to_c(FILE *out,size_t indentation,struct AST *statement);
+void ast_if_to_c(FILE *out,size_t indentation,struct AST_If_Statement *statement);
+void ast_expression_to_c(FILE *out,struct AST *expression);
+void ast_expression_state_to_c(FILE *out,struct AST_State *state);
void ast_to_c_print_internal_stuff_for_header(FILE *out,struct AST_Translation_Unit *translation_unit);
void ast_to_c_print_internal_stuff_for_body(FILE *out);