aboutsummaryrefslogtreecommitdiffstats
path: root/src/backend/targets
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/targets')
-rw-r--r--src/backend/targets/C/ast_to_c.c155
-rw-r--r--src/backend/targets/C/ast_to_c.h29
-rw-r--r--src/backend/targets/print/print.c295
-rw-r--r--src/backend/targets/print/print.h27
4 files changed, 506 insertions, 0 deletions
diff --git a/src/backend/targets/C/ast_to_c.c b/src/backend/targets/C/ast_to_c.c
new file mode 100644
index 0000000..346801c
--- /dev/null
+++ b/src/backend/targets/C/ast_to_c.c
@@ -0,0 +1,155 @@
+#ifndef AST_TO_C_C
+#define AST_TO_C_C AST_TO_C_C
+#include <ast_to_c.h>
+#include <print.h>
+
+void ast_to_c(FILE *out,struct AST *tree)
+{
+ assert(tree->type==AST_TYPE_MACHINE);
+ ast_machine_to_c(out,(struct AST_Machine*)tree);
+}
+void ast_machine_to_c(FILE *out,struct AST_Machine *machine)
+{
+
+ assert(out!=NULL && machine!=NULL && machine->type==AST_TYPE_MACHINE);
+
+
+ ast_machine_to_c_make_header_part(out,machine);
+ ast_machine_to_c_make_body_part(out,machine);
+
+
+}
+void ast_machine_to_c_make_body_part(FILE *out,struct AST_Machine *machine)
+{
+ size_t i;
+ struct State_And_Transitions **table;
+
+ table=extract_transition_table(machine->states,machine->transitions);
+
+ for(i=0;i<machine->states->number_of_states;++i)
+ {
+ ast_state_to_c_signature(out,machine,machine->states->states[i]);
+ fprintf(out,"\n{\n");
+ ast_transitions_of_state_to_c(out,machine,table[i]);
+ fprintf(out,"\n}\n");
+ }
+}
+void ast_machine_to_c_make_header_part(FILE *out,struct AST_Machine *machine)
+{
+ fprintf(out,"typedef struct machine_buffer_t \n{"
+ "\n\tsize_t size;\n\tunsigned char buffer[];\n} machine_buffer_t;\n\n");
+ ast_events_to_c(out,machine);
+ ast_states_to_c(out,machine);
+}
+void ast_events_to_c(FILE *out,struct AST_Machine *machine)
+{
+ size_t i;
+
+ assert(out!=NULL && machine!=NULL && machine->type==AST_TYPE_MACHINE);
+
+ fprintf(out,"enum ");
+ ast_event_name_to_c(out,machine);
+ fprintf(out,"\n{\n");
+
+ for(i=0;i<machine->events->number_of_events;++i)
+ {
+ fprintf(out,"\t");
+ ast_token_to_c(out,machine->id);
+ fprintf(out,"_EVENT_");
+ ast_token_to_c(out,machine->events->events[i]->name);
+ fprintf(out,",\n");
+ }
+ fprintf(out,"};\n");
+}
+void ast_states_to_c(FILE *out,struct AST_Machine *machine)
+{
+ size_t i;
+ assert(out!=NULL && machine!=NULL && machine->type==AST_TYPE_MACHINE);
+ for(i=0;i<machine->states->number_of_states;++i)
+ {
+ fprintf(out,"extern ");
+ ast_state_to_c_signature(out,machine,machine->states->states[i]);
+ fprintf(out,";\n");
+ }
+}
+void ast_transitions_of_state_to_c(FILE *out,struct AST_Machine *machine,struct State_And_Transitions *vector)
+{
+ size_t i;
+ ast_state_to_c_signature(out,machine,vector->state);
+ fprintf(out,"\n{\n\n\tswitch(event)\n\t{\n");
+ for(i=0;i<vector->number_of_transitions;++i)
+ {
+ fprintf(out,"\t\tcase ");
+ ast_token_to_c(out,vector->transitions[i]->event->name);
+ fprintf(out,":\n");
+ ast_pipeline_to_c(out,3,vector->transitions[i]->pipeline);
+ fprintf(out,"\n\t\t\tbreak;\n");
+ }
+
+}
+void ast_event_name_to_c(FILE *out,struct AST_Machine *machine)
+{
+ fprintf(out,"MACHINE_");
+ ast_token_to_c(out,machine->id);
+ fprintf(out,"_EVENTS_ENUM");
+}
+void ast_state_to_c_signature(FILE *out,struct AST_Machine *machine,struct AST_State *state)
+{
+ fprintf(out,"void machine_");
+ ast_token_to_c(out,machine->id);
+ fprintf(out,"_state_");
+ ast_token_to_c(out,state->name);
+ fprintf(out,"(enum ");
+ ast_event_name_to_c(out,machine);
+ fprintf(out," event)\n");
+}
+void ast_token_to_c(FILE *out,struct token *token)
+{
+ size_t i;
+
+ assert(out!=NULL && token!=NULL);
+
+ for(i=0;i<token->size;++i)
+ fprintf(out,"%c",token->data[i]);
+}
+void ast_pipeline_to_c(FILE *out,size_t indentation,struct AST_Pipeline *pipeline)
+{
+ size_t i;
+
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"machine_buffer_t *hold_buffer;\n");
+
+ for(i=0;i<pipeline->size;++i)
+ {
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"hold_buffer=");
+ ast_command_to_c(out,pipeline->pipeline[i],"hold_buffer");
+ fprintf(out,";\n");
+ }
+ ast_to_c_print_tabs(out,indentation);
+ fprintf(out,"delete_machine_buffer(hold_buffer);\n");
+}
+void ast_command_to_c(FILE *out,struct AST_Command *command,char *hold_buffer)
+{
+ ast_token_to_c(out,command->function_name);
+ fprintf(out,"(");
+ if(command->argument==NULL)
+ fprintf(out,"NULL");
+ else
+ ast_token_to_c(out,command->argument);
+ fprintf(out,",%s)",hold_buffer);
+}
+
+void ast_command_to_c_signature(FILE *out,struct AST_Command *command)
+{
+ fprintf(out,"machine_buffer_t* ");
+ ast_token_to_c(out,command->function_name);
+ fprintf(out,"machine_buffer_t *arguments,machine_buffer_t *input)");
+}
+void ast_to_c_print_tabs(FILE *out,size_t number_of_tabs)
+{
+ size_t i;
+ for(i=0;i<number_of_tabs;++i)
+ fprintf(out,"\t");
+}
+#endif
diff --git a/src/backend/targets/C/ast_to_c.h b/src/backend/targets/C/ast_to_c.h
new file mode 100644
index 0000000..8c86687
--- /dev/null
+++ b/src/backend/targets/C/ast_to_c.h
@@ -0,0 +1,29 @@
+#ifndef AST_TO_C_H
+#define AST_TO_C_H AST_TO_C_H
+#include <stdio.h>
+#include <parser.h>
+#include <backend.h>
+#include <ctype.h>
+
+struct State_And_Transitions;
+
+void ast_to_c(FILE *out,struct AST *tree);
+void ast_machine_to_c(FILE *out,struct AST_Machine *machine);
+void ast_machine_to_c_make_header_part(FILE *out,struct AST_Machine *machine);
+void ast_machine_to_c_make_body_part(FILE *out,struct AST_Machine *machine);
+
+void ast_events_to_c(FILE *out,struct AST_Machine *machine);
+void ast_states_to_c(FILE *out,struct AST_Machine *machine);
+
+void ast_pipeline_to_c(FILE *out,size_t indentation,struct AST_Pipeline *pipeline);
+void ast_command_to_c(FILE *out,struct AST_Command *command,char *hold_buffer);
+void ast_command_to_c_signature(FILE *out,struct AST_Command *command);
+
+void ast_transitions_of_state_to_c(FILE *out,struct AST_Machine *machine,struct State_And_Transitions *vector);
+void ast_token_to_c(FILE *out,struct token *token);
+void ast_event_name_to_c(FILE *out,struct AST_Machine *machine);
+void ast_state_to_c_signature(FILE *out,struct AST_Machine *machine,struct AST_State *state);
+/* :X */
+void ast_to_c_print_tabs(FILE *out,size_t number_of_tabs);
+
+#endif
diff --git a/src/backend/targets/print/print.c b/src/backend/targets/print/print.c
new file mode 100644
index 0000000..8eae3dd
--- /dev/null
+++ b/src/backend/targets/print/print.c
@@ -0,0 +1,295 @@
+#ifndef PRINT_C
+#define PRINT_C PRINT_C
+#include<print.h>
+
+void print_keyword_enum(enum Keyword code)
+{
+ switch(code)
+ {
+ case KW_MACHINE:
+ printf("KW_MACHINE");
+ break;
+ case KW_FROM:
+ printf("KW_FROM");
+ break;
+ case KW_TO:
+ printf("KW_TO");
+ break;
+ case KW_ON:
+ printf("KW_ON");
+ break;
+ case KW_ID:
+ printf("KW_ID");
+ break;
+ case KW_STRING:
+ printf("KW_STRING");
+ break;
+ case KW_NOP:
+ printf("KW_NOP");
+ break;
+ case KW_EOF:
+ printf("KW_EOF");
+ break;
+ case KW_OPEN_SQUARE:
+ printf("KW_OPEN_SQUARE");
+ break;
+ case KW_CLOSE_SQUARE:
+ printf("KW_CLOSE_SQUARE");
+ break;
+ case KW_PIPE:
+ printf("KW_PIPE");
+ break;
+ case KW_SEMI_COLUMN:
+ printf("KW_SEMI_COLUMN");
+ break;
+ case KW_STARTING:
+ printf("KW_STARTING");
+ break;
+ case KW_STATES:
+ printf("KW_STATES");
+ break;
+ case KW_EVENTS:
+ printf("KW_EVENTS");
+ break;
+ case KW_EVENT:
+ printf("KW_EVENT");
+ break;
+ case KW_TRANSITIONS:
+ printf("KW__TRANSITIONS");
+ break;
+ case KW_EXECUTE:
+ printf("KW_EXECUTE");
+ break;
+ case KW_COMMA:
+ printf("KW_COMMA");
+ break;
+ default:
+ printf("LEXERROR");
+ }
+}
+void print_token(struct token *token)
+{
+ size_t i;
+ assert(token);
+
+ printf("[ ");
+ print_keyword_enum(token->type);
+ printf(" ");
+ for(i=0;i<token->size;++i)
+ printf("%c",token->data[i]);
+ printf(" ] ");
+
+}
+void print_tokens(struct Queue *tokens)
+{
+ struct Queue_Node *it;
+ assert(tokens);
+
+ for(it=tokens->first;it!=NULL;it=it->prev)
+ {
+ print_token( (struct token*)(it->data));
+ printf(" ");
+ }
+}
+void print_ast_enum(enum AST_Type type)
+{
+ switch(type)
+ {
+ case AST_TYPE_MACHINE:
+ printf("AST_TYPE_MACHINE");
+ break;
+ case AST_TYPE_STATE:
+ printf("AST_TYPE_STATE");
+ break;
+ case AST_TYPE_STATES:
+ printf("AST_TYPE_STATES");
+ break;
+ case AST_TYPE_EVENT:
+ printf("AST_TYPE_EVENT");
+ break;
+ case AST_TYPE_EVENTS:
+ printf("AST_TYPE_EVENTS");
+ break;
+ case AST_TYPE_TRANSITION:
+ printf("AST_TYPE_TRANSITION");
+ break;
+ case AST_TYPE_TRANSITIONS:
+ printf("AST_TYPE_TRANSITIONS");
+ break;
+ case AST_TYPE_COMMAND:
+ printf("AST_TYPE_COMMAND");
+ break;
+ case AST_TYPE_PIPELINE:
+ printf("AST_TYPE_PIPELINE");
+ break;
+ default:
+ printf("AST_NOP");
+ }
+
+}
+void print_ast(struct AST *tree)
+{
+ assert(tree);
+
+ switch(tree->type)
+ {
+ case AST_TYPE_MACHINE:
+ print_ast_machine((struct AST_Machine*)tree);
+ break;
+ case AST_TYPE_STATE:
+ print_ast_state((struct AST_State*)tree);
+ break;
+ case AST_TYPE_STATES:
+ print_ast_states((struct AST_States*)tree);
+ break;
+ case AST_TYPE_EVENT:
+ print_ast_event((struct AST_Event*)tree);
+ break;
+ case AST_TYPE_EVENTS:
+ print_ast_events((struct AST_Events*)tree);
+ break;
+ case AST_TYPE_TRANSITION:
+ print_ast_transition((struct AST_Transition*)tree);
+ break;
+ case AST_TYPE_TRANSITIONS:
+ print_ast_transitions((struct AST_Transitions*)tree);
+ break;
+ case AST_TYPE_COMMAND:
+ print_ast_command((struct AST_Command*)tree);
+ break;
+ case AST_TYPE_PIPELINE:
+ print_ast_pipeline((struct AST_Pipeline*)tree);
+ break;
+ default:
+ printf("noast");
+ }
+}
+void print_ast_state(struct AST_State* tree)
+{
+ assert(tree);
+
+ printf("[ STATE: ");
+ print_token(tree->name);
+ printf(" %ld",tree->number);
+ printf("]");
+}
+void print_ast_event(struct AST_Event* tree)
+{
+ assert(tree);
+
+ printf("[ EVENT: ");
+ print_token(tree->name);
+ printf("]");
+}
+void print_ast_states(struct AST_States* tree)
+{
+ size_t i;
+ assert(tree);
+
+ printf("STATES [\n");
+ for(i=0;i<tree->number_of_states;++i)
+ {
+ print_ast_state(tree->states[i]);
+ printf(" ");
+ }
+ printf("\n ] END STATES \n");
+}
+void print_ast_events(struct AST_Events* tree)
+{
+ size_t i;
+ assert(tree);
+
+ printf("EVENTS [\n");
+ for(i=0;i<tree->number_of_events;++i)
+ {
+ print_ast_event(tree->events[i]);
+ printf(" ");
+ }
+ printf("\n ] END EVENTS \n");
+}
+void print_ast_transition(struct AST_Transition* tree)
+{
+ assert(tree);
+
+ printf("TRANSITION [\nFROM");
+ print_ast_state(tree->from);
+ printf(" TO ");
+ print_ast_state(tree->to);
+ printf(" COMMAND {");
+ if(tree->pipeline==NULL)
+ {
+ printf("NULL");
+ }else
+ {
+ print_ast_pipeline(tree->pipeline);
+ }
+
+}
+void print_ast_command(struct AST_Command* tree)
+{
+ assert(tree);
+
+ printf("( command ");
+ print_token(tree->function_name);
+ if(tree->argument==NULL)
+ {
+ printf(" NOARGUMENTS ");
+ }else
+ {
+ printf(" \"");
+ print_token(tree->argument);
+ printf("\" ");
+ }
+ printf(")");
+}
+void print_ast_pipeline(struct AST_Pipeline* tree)
+{
+ size_t i;
+ assert(tree);
+ printf("PIPELINE <");
+ for(i=0;i<tree->size;++i)
+ {
+ print_ast_command(tree->pipeline[i]);
+ printf(" | ");
+ }
+ printf("> PIPELINE_END");
+}
+void print_ast_machine(struct AST_Machine* tree)
+{
+ assert(tree);
+ printf("MACHINE ");
+ print_token(tree->id);
+ printf(" [\n");
+ print_ast_states(tree->states);
+ print_ast_events(tree->events);
+ print_ast_transitions(tree->transitions);
+ printf("] MACHINE_END\n");
+}
+void print_ast_transitions(struct AST_Transitions* tree)
+{
+ size_t i;
+ assert(tree);
+ printf("TRANSITIONS [\n");
+ for(i=0;i<tree->size;++i)
+ {
+ print_ast_transition(tree->transitions[i]);
+ printf("\n");
+ }
+ printf("] TRANSITIONS_END\n");
+}
+void print_error(struct Error *error)
+{
+ assert(error);
+ printf("Error: %s, line %ld row %ld\n",error->message,error->row,error->column);
+}
+void print_errors(struct Translation_Data *translation_data)
+{
+ struct Queue_Node *it;
+ assert(translation_data);
+
+ for(it=translation_data->errors->first;it!=NULL;it=it->prev)
+ {
+ print_error(it->data);
+ }
+}
+#endif
diff --git a/src/backend/targets/print/print.h b/src/backend/targets/print/print.h
new file mode 100644
index 0000000..54d47ce
--- /dev/null
+++ b/src/backend/targets/print/print.h
@@ -0,0 +1,27 @@
+#ifndef PRINT_H
+#define PRINT_H PRINT_H
+#include <stdio.h>
+#include <lexer.h>
+#include <parser.h>
+#include <queue.h>
+
+
+void print_keyword_enum(enum Keyword code);
+void print_token(struct token *token);
+void print_tokens(struct Queue *tokens);
+
+void print_ast_enum(enum AST_Type type);
+void print_ast(struct AST *tree);
+void print_ast_state(struct AST_State* tree);
+void print_ast_event(struct AST_Event* tree);
+void print_ast_states(struct AST_States* tree);
+void print_ast_events(struct AST_Events* tree);
+void print_ast_transition(struct AST_Transition* tree);
+void print_ast_command(struct AST_Command* tree);
+void print_ast_pipeline(struct AST_Pipeline* tree);
+void print_ast_machine(struct AST_Machine* tree);
+void print_ast_transitions(struct AST_Transitions* tree);
+
+void print_error(struct Error *error);
+void print_errors(struct Translation_Data *translation_data);
+#endif