aboutsummaryrefslogtreecommitdiffstats
path: root/src/backend/targets/print/print.c
blob: 3c334388798437f8bd68007ce8e0b734e70c1909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#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_OPEN_NORMAL:
			printf("KW_OPEN_NORMAL");
			break;
		case KW_CLOSE_NORMAL:
			printf("KW_CLOSE_NORMAL");
			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_EXECUTE:
			printf("KW_EXECUTE");
			break;
		case KW_TRANSITIONS:
			printf("KW_TRANSITIONS");
			break;
		case KW_COMMA:
			printf("KW_COMMA");
			break;
		case KW_DOT:
			printf("KW_DOT");
			break;
		case KW_AND:
			printf("KW_AND");
			break;
		case KW_OR:
			printf("KW_OR");
			break;
		case KW_NOT:
			printf("KW_NOT");
			break;
		case KW_IF:
			printf("KW_IF");
			break;
		case KW_ELSE:
			printf("KW_ELSE");
			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_TRANSLATION_UNIT:
			printf("AST_TYPE_TRANSLATION_UNIT");
			break;
		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;
		case AST_TYPE_OP_AND:
			printf("AST_TYPE_OP_AND");
			break;
		case AST_TYPE_OP_OR:
			printf("AST_TYPE_OP_OR");
			break;
		case AST_TYPE_OP_NOT:
			printf("AST_TYPE_OP_NOT");
			break;
		case AST_TYPE_OP_SELECTOR:
			printf("AST_TYPE_OP_SELECTOR");
			break;
		case AST_TYPE_UNFINISHED_STATE:
			printf("AST_TYPE_UNFINISHED_STATE");
			break;
		case AST_TYPE_IF:
			printf("AST_TYPE_IF");
			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;
		case AST_TYPE_TRANSLATION_UNIT:
			print_ast_translation_unit((struct AST_Translation_Unit*)tree);
			break;
		case AST_TYPE_OP_AND:
		case AST_TYPE_OP_OR:
		case AST_TYPE_OP_SELECTOR:
			print_ast_binary_expression((struct AST_Binary_Expression*)tree);
			break;
		case AST_TYPE_OP_NOT:
			print_ast_unary_expression((struct AST_Unary_Expression*)tree);
			break;
		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");
	}
}
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->statement==NULL)
	{
		printf("NULL");
	}else
	{
		print_ast(tree->statement);
	}

}
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);
	}
}
void print_ast_translation_unit(struct AST_Translation_Unit *tree)
{
	size_t i;
	printf("TRANSLATION UNIT\n[\n");
	for(i=0;i<tree->number_of_machines;++i)
		print_ast_machine(tree->machines[i]);
	printf("\n] TRANSLATION UNIT END \n");
}
void print_ast_binary_expression(struct AST_Binary_Expression *tree)
{
	printf(" (");
	print_ast(tree->left);
	switch(tree->type)
	{
		case AST_TYPE_OP_AND:
			printf("&&");
			break;
		case AST_TYPE_OP_OR:
			printf("||");
			break;
		case AST_TYPE_OP_SELECTOR:
			printf(".");
			break;
		default:
			printf("?!!?!?!?");
	}
	print_ast(tree->right);
	printf(")");
}
void print_ast_unary_expression(struct AST_Unary_Expression *tree)
{
	printf("(!");
	print_ast(tree->operand);
	printf(")");
}
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