aboutsummaryrefslogtreecommitdiffstats
path: root/src/backend/targets
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
parentb845e4754be86d2216733d9bea75cb301f38739d (diff)
downloadMEGATRON-a26684a417729699e95b335a3d00798237ffba9b.tar.gz
added the if statement
Diffstat (limited to 'src/backend/targets')
-rw-r--r--src/backend/targets/C/ast_to_c.c76
-rw-r--r--src/backend/targets/C/ast_to_c.h4
-rw-r--r--src/backend/targets/print/print.c27
-rw-r--r--src/backend/targets/print/print.h1
4 files changed, 104 insertions, 4 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);
diff --git a/src/backend/targets/print/print.c b/src/backend/targets/print/print.c
index 7649b91..3c33438 100644
--- a/src/backend/targets/print/print.c
+++ b/src/backend/targets/print/print.c
@@ -81,6 +81,12 @@ void print_keyword_enum(enum Keyword code)
case KW_NOT:
printf("KW_NOT");
break;
+ case KW_IF:
+ printf("KW_IF");
+ break;
+ case KW_ELSE:
+ printf("KW_ELSE");
+ break;
default:
printf("LEXERROR");
}
@@ -158,6 +164,9 @@ void print_ast_enum(enum AST_Type type)
case AST_TYPE_UNFINISHED_STATE:
printf("AST_TYPE_UNFINISHED_STATE");
break;
+ case AST_TYPE_IF:
+ printf("AST_TYPE_IF");
+ break;
default:
printf("AST_NOP");
}
@@ -210,6 +219,9 @@ void print_ast(struct AST *tree)
case AST_TYPE_UNFINISHED_STATE:
print_ast_unchecked_state((struct AST_Unchecked_State*)tree);
break;
+ case AST_TYPE_IF:
+ print_ast_if_statement((struct AST_If_Statement*)tree);
+ break;
default:
printf("noast");
}
@@ -262,16 +274,16 @@ void print_ast_transition(struct AST_Transition* tree)
assert(tree);
printf("TRANSITION [\nFROM");
- print_ast(tree->from);
+ print_ast_state(tree->from);
printf(" TO ");
print_ast_state(tree->to);
printf(" COMMAND {");
- if(tree->pipeline==NULL)
+ if(tree->statement==NULL)
{
printf("NULL");
}else
{
- print_ast_pipeline(tree->pipeline);
+ print_ast(tree->statement);
}
}
@@ -381,4 +393,13 @@ void print_ast_unchecked_state(struct AST_Unchecked_State *tree)
{
print_token(tree->name);
}
+void print_ast_if_statement(struct AST_If_Statement *tree)
+{
+ printf("if <");
+ print_ast(tree->condition);
+ printf(">\n");
+ print_ast(tree->body);
+ if(tree->else_statement)
+ print_ast(tree->else_statement);
+}
#endif
diff --git a/src/backend/targets/print/print.h b/src/backend/targets/print/print.h
index d318792..b21a00c 100644
--- a/src/backend/targets/print/print.h
+++ b/src/backend/targets/print/print.h
@@ -25,6 +25,7 @@ void print_ast_translation_unit(struct AST_Translation_Unit *tree);
void print_ast_binary_expression(struct AST_Binary_Expression *tree);
void print_ast_unary_expression(struct AST_Unary_Expression *tree);
void print_ast_unchecked_state(struct AST_Unchecked_State *tree);
+void print_ast_if_statement(struct AST_If_Statement *tree);
void print_error(struct Error *error);
void print_errors(struct Translation_Data *translation_data);