aboutsummaryrefslogtreecommitdiffstats
path: root/src/backend/backend.c
diff options
context:
space:
mode:
authorGalin Simeonov <gts@volconst.com>2021-06-03 12:38:37 +0300
committerGalin Simeonov <gts@volconst.com>2021-07-15 18:04:02 +0300
commit76fc38b3cdfded2911b464baa7b182b5102318d1 (patch)
tree389223f76086bff07a3cd762683e5a16d34d12d2 /src/backend/backend.c
parent85b23fbee717f047af5a89eac6f4dba8e7812524 (diff)
downloadMEGATRON-76fc38b3cdfded2911b464baa7b182b5102318d1.tar.gz
work on generated C code
Diffstat (limited to 'src/backend/backend.c')
-rw-r--r--src/backend/backend.c116
1 files changed, 115 insertions, 1 deletions
diff --git a/src/backend/backend.c b/src/backend/backend.c
index 122e84c..6646e6c 100644
--- a/src/backend/backend.c
+++ b/src/backend/backend.c
@@ -6,6 +6,7 @@
struct State_And_Transitions** extract_transition_table(struct AST_States *states,struct AST_Transitions *transitions)
{
struct State_And_Transitions **ret;
+ struct AST_State *hold_state;
struct Queue *qs;
size_t i;
@@ -18,7 +19,13 @@ struct State_And_Transitions** extract_transition_table(struct AST_States *state
/*traverse transitions and push them into the queue of their coresponding state*/
for(i=0;i<transitions->size;++i)
- Queue_Push(qs+transitions->transitions[i]->from->number,transitions->transitions[i]);
+ {
+ hold_state=extract_state(transitions->transitions[i]->from,states);
+
+ assert(hold_state!=NULL);
+
+ Queue_Push(qs+hold_state->number,transitions->transitions[i]);
+ }
/*condense the queues into the arrays*/
for(i=0;i<states->number_of_states;++i)
@@ -34,4 +41,111 @@ struct State_And_Transitions** extract_transition_table(struct AST_States *state
return ret;
}
+_Bool expression_is_binary(struct AST* expression)
+{
+ return (expression->type==AST_TYPE_OP_OR || expression->type==AST_TYPE_OP_AND);
+}
+_Bool expression_is_unary(struct AST* expression)
+{
+ return (expression->type==AST_TYPE_OP_NOT);
+}
+_Bool state_is_in_states(struct AST_State *state,struct AST_States *states)
+{
+ return state==Map_Check(states->states_map,state->name->data,state->name->size);
+}
+
+struct AST_State* extract_state(struct AST *expression,struct AST_States *states)
+{
+ struct AST_State *hold_state;
+
+ assert(expression->type==AST_TYPE_OP_NOT || expression->type==AST_TYPE_OP_AND || expression->type==AST_TYPE_OP_OR
+ || expression->type==AST_TYPE_STATE );
+
+ if(expression->type==AST_TYPE_STATE)
+ {
+ if(state_is_in_states((struct AST_State*)expression,states))
+ return (struct AST_State*)expression;
+ else
+ return NULL;
+ }else if(expression_is_binary(expression))
+ {
+ hold_state=extract_state(((struct AST_Binary_Expression*)expression)->left,states);
+ if(hold_state)
+ return hold_state;
+ else
+ return extract_state(((struct AST_Binary_Expression*)expression)->right,states);
+ }else
+ {
+ return extract_state(((struct AST_Unary_Expression*)expression)->operand,states);
+ }
+}
+
+_Bool check_expression(struct AST *state,struct AST_Translation_Unit *unit)
+{
+
+}
+_Bool check_transition(struct AST *state,struct AST_Translation_Unit *unit)
+{
+
+}
+void anotate_unchecked_states(struct AST_Translation_Unit *unit,struct Translation_Data *translation_data)
+{
+ size_t i;
+ for(i=0;i<unit->number_of_machines;++i)
+ {
+ anotate_machine(unit->machines[i],unit);
+ }
+ if(has_new_errors(
+
+}
+void anotate_machine(struct AST_Machine *machine,struct AST_Translation_Unit *unit,struct Translation_Data *translation_data)
+{
+ size_t i;
+ for(i=0;i<machine->transitions->size;++i)
+ {
+ machine->transitions->transitions[i]->from=anotate_expression(machine->transitions->transitions[i]->from,machine,unit);
+ }
+ if(has_new_errors(translation_data))
+ {
+ push_error_with_token("in machine",translation_data,machine->id);
+ delete_ast_machine(machine);
+ }
+}
+struct AST* anotate_expression(struct AST *expression,struct AST_Translation_Unit *unit,struct AST_Machine *current_machine,struct Translation_Data *translation_data);
+{
+ struct AST_Machine *hold_machine;
+ struct AST *hold_left;
+ struct AST *hold_right;
+
+ if(expression_is_binary(expression))
+ {
+ left=((struct AST_Binary_Expression*)expression)->left;
+ right=((struct AST_Binary_Expression*)expression)->right;
+
+ if(expression->type==AST_TYPE_OP_SELECTOR)
+ {
+ hold_machine=Map_Check(unit->machines_map,((struct AST_Unchecked_State*)hold_left)->name->data,((struct AST_Unchecked_State*)hold_left)->name->size);
+ if(hold_machine==NULL)
+ {
+ push_error_with_token("machine id is undefined",((struct AST_Unchecked_State*)hold_left)->name,translation_data);
+ delete_ast(expression);
+ return NULL;
+ }else
+ {
+ return ast_check_state(hold_right,hold_machine->states,translation_data);
+ }
+ }else
+ {
+ AS_BIN_EXPR_PTR(expression)->right=anotate_expression(AS_BIN_EXPR_PTR(expression)->right,machine,unit);
+ AS_BIN_EXPR_PTR(expression)->left=anotate_expression(AS_BIN_EXPR_PTR(expression)->left,machine,unit);
+ return expression;
+ }
+ }else if(expression_is_unary(expression))
+ {
+ AS_UN_EXPR_PTR(expression)->operand=anotate_expression(AS_UN_EXPR_PTR(expression)->operand,machine,unit);
+ }else if(expression->type==AST_TYPE_UNFINISHED_STATE)
+ {
+ return ast_check_state(AS_UNCK_EXPR_PTR(expression),machine->states,translation_data);
+ }
+}
#endif