-
Notifications
You must be signed in to change notification settings - Fork 0
/
wlc.c
1036 lines (870 loc) · 22.5 KB
/
wlc.c
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Simple wavelang translator
*/
#include <stdio.h>
#include <stdlib.h>
#include "lib/mpc/mpc.h"
//#define DEBUG
#define SYM_NAME_LEN 128
#define CAT_NAME_LEN SYM_NAME_LEN
#define SYS_NAME_LEN SYM_NAME_LEN
#define MAX_SYMS_PER_TABLE 128
#define MAX_EQNS_PER_CAT 64
#ifdef DEBUG
#define DEBUG_PRINT(...) do {fprintf(stderr, __VA_ARGS__ );} while(0)
#else
#define DEBUG_PRINT(...) do {} while (0)
#endif
#define ERROR_PRINT(...) do {fprintf(stderr, "[ERROR] " __VA_ARGS__);} while(0)
enum symbol_type {
SYM_VAR = 0,
SYM_PAR,
SYM_LOC_VAR,
SYM_STORAGE,
SYM_INT_VAR,
SYM_INT_VEC,
SYM_EQN_VEC,
SYM_VEC,
SYM_FUN
};
struct symbol {
enum symbol_type type;
char name[SYM_NAME_LEN];
unsigned int capacity;
};
struct symbol_table {
struct symbol symbols[MAX_SYMS_PER_TABLE];
unsigned int num_symbols;
};
int add_symbol(struct symbol_table *table, char *name,
enum symbol_type type, unsigned int capacity)
{
unsigned int i = table->num_symbols;
if (strlen(name) >= SYM_NAME_LEN) {
ERROR_PRINT("Too long symbol name!\n");
return -1;
}
strcpy(table->symbols[i].name, name);
table->symbols[i].type = type;
table->symbols[i].capacity = capacity;
table->num_symbols++;
return 0;
}
struct symbol *find_symbol(struct symbol_table *table, char *name)
{
int i;
for (i = 0; i < table->num_symbols; i++) {
if (0 == strcmp(table->symbols[i].name, name))
return &table->symbols[i];
}
return NULL;
}
typedef void (*symbol_fun_t)(struct symbol *symbol);
void for_each_symbol_type(struct symbol_table *table, enum symbol_type type,
symbol_fun_t fun)
{
int i;
for (i = 0; i < table->num_symbols; i++) {
if (table->symbols[i].type == type)
fun(&table->symbols[i]);
}
}
int count_symbol_type(struct symbol_table *table, enum symbol_type type)
{
int i, c = 0;
for (i = 0; i < table->num_symbols; i++) {
if (table->symbols[i].type == type)
c++;
}
return c;
}
struct system {
char name[SYS_NAME_LEN];
unsigned int num_equations;
struct symbol_table sym_table;
};
struct catastrophe {
char name[CAT_NAME_LEN];
struct symbol_table sym_table;
struct system systems[MAX_EQNS_PER_CAT];
unsigned int num_systems;
};
enum parser_state{
PSTATE_INITIAL = 0,
PSTATE_CATASTROPHE,
PSTATE_CATASTROPHE_NAME,
PSTATE_LEVEL0_DECL,
PSTATE_LEVEL0_AFTER_DECL,
PSTATE_LEVEL0_SYSTEM,
PSTATE_LEVEL0_AFTER_MAIN_BLOCK,
};
struct parse_table_entry;
typedef int (*walk_func_t)(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[]);
struct parse_table_entry {
char *tag;
walk_func_t func;
struct parse_table_entry *(*parse_table[]);
};
struct catastrophe catastrophe;
enum parser_state parser_state = PSTATE_INITIAL;
struct system *current_system = NULL;
/*
* Most of the analyzer's code is data-driven. The following declarations are
* the set of semantic analysis and generation rules.
*/
extern struct parse_table_entry *parse_leaf_table[];
extern struct parse_table_entry *parse_value_table[];
extern struct parse_table_entry *parse_product_table[];
extern struct parse_table_entry *parse_function_table[];
extern struct parse_table_entry *parse_expression_table[];
void gen_headers(void)
{
char *str =
"/* Generated by WaveLang Compiler (WLC). */\n"
"#include <wavecat/catastrophe.h>\n"
"#include <wavecat/point_array.h>\n"
"#include <lib/integration/cmplx_runge_kutta.h>\n"
"#include <math.h>\n\n"
"#define RungeKutta(step, func) \\\n"
"\t0;{\\\n"
"\t\tcmplx_runge_kutta(0.0, 1.0, (step), catastrophe);\\\n"
"\t\tequation_set_function(equation, (func));\\\n"
"\t\t0;\\\n"
"\t} while(0)\n\n";
printf("%s\n", str);
}
void gen_glob_sym_id(struct symbol *symbol)
{
printf("\t%s,\n", symbol->name);
}
void gen_glob_sym_name(struct symbol *symbol)
{
printf("\"%s\",", symbol->name);
}
void gen_glob_parameters(void)
{
printf("\nenum parameters {\n");
for_each_symbol_type(&catastrophe.sym_table, SYM_PAR,
gen_glob_sym_id);
printf("\tlastpar\n");
printf("};\n\n");
printf("static char *par_names[] = {");
for_each_symbol_type(&catastrophe.sym_table, SYM_PAR,
gen_glob_sym_name);
printf("NULL};\n");
}
void gen_glob_variables(void)
{
printf("\nenum variables {\n");
for_each_symbol_type(&catastrophe.sym_table, SYM_VAR,
gen_glob_sym_id);
printf("\tlastvar\n");
printf("};\n\n");
printf("static char *var_names[] = {");
for_each_symbol_type(&catastrophe.sym_table, SYM_VAR,
gen_glob_sym_name);
printf("NULL};\n");
}
void gen_glob_storage(void)
{
printf("\nenum storage_entries {\n");
for_each_symbol_type(&catastrophe.sym_table, SYM_STORAGE,
gen_glob_sym_id);
printf("};\n\n");
}
void gen_loc_sym_name(struct symbol *symbol)
{
printf("%s, ", symbol->name);
}
void gen_system(void)
{
printf("\n\nvoid catastrophe_%s_function(\n"
"\t\tconst catastrophe_t *const catastrophe,\n"
"\t\tconst double t, const double complex *input,\n"
"\t\tdouble complex *const result)\n", current_system->name);
}
void gen_system_vardecls(void)
{
printf("\tdouble complex ");
for_each_symbol_type(¤t_system->sym_table, SYM_LOC_VAR,
gen_loc_sym_name);
printf("unused;\n\n");
}
void gen_main_block(void)
{
printf("\n\nstatic void calculate(catastrophe_t *const catastrophe,\n"
"\t\tconst unsigned int i, const unsigned int j)\n");
}
void gen_main_block_prologue(void)
{
printf(
"\tcmplx_equation_t *equation;\n"
"\tpoint_array_t *point_array;\n"
"\tdouble module, phase;\n"
"\tint nope;\n\n"
"\tequation = catastrophe->equation;\n"
"\tpoint_array = catastrophe->point_array;\n\n"
);
}
void gen_main_block_epilogue(void)
{
printf(
"\n\n\tmodule = cabs(equation->resulting_vector[0]);\n"
"\tphase = (180.0 / M_PI) * carg(equation->resulting_vector[0]);\n\n"
"\tpoint_array->array[i][j].module = module;\n"
"\tpoint_array->array[i][j].phase = phase;\n");
}
void gen_desc(void)
{
printf(
"\n\nstatic catastrophe_desc_t catastrophe_%s_desc = {\n"
"\t.type = CT_COMPLEX,\n"
"\t.sym_name = \"%s\",\n"
"\t.fabric = catastrophe_fabric,\n"
"\t.num_parameters = %d,\n"
"\t.num_variables = %d,\n"
"\t.par_names = par_names,\n"
"\t.var_names = var_names,\n"
"\t.equation.cmplx = catastrophe_%s_function,\n"
"\t.num_equations = %d,\n"
"\t.calculate = calculate,\n"
"};\n\n",
catastrophe.name, catastrophe.name,
count_symbol_type(&catastrophe.sym_table, SYM_PAR),
count_symbol_type(&catastrophe.sym_table, SYM_VAR),
current_system->name,
current_system->num_equations);
}
void gen_init(void)
{
printf(
"static int cmplx_catastrophe_%s_init(void) __attribute__ ((constructor));\n"
"static int cmplx_catastrophe_%s_init(void)\n"
"{\n"
"\tfprintf(stderr, \"Catastrophe %s (complex) initialization.\\n\");\n"
"\tregister_catastrophe_desc(&catastrophe_%s_desc);\n"
"}\n",
catastrophe.name, catastrophe.name, catastrophe.name,
catastrophe.name);
}
void gen_rest(void)
{
gen_desc();
gen_init();
}
int apply_parse_rule(mpc_ast_t *ast, mpc_ast_t *par_ast,
struct symbol_table *sym_table,
struct parse_table_entry *parse_table[], int strict)
{
int i, ret;
for (i = 0; parse_table[i]; i++) {
int match = 0;
switch (strict) {
case 1:
if (ast->tag == strstr(ast->tag, parse_table[i]->tag))
match = 1;
break;
default:
if (strstr(ast->tag, parse_table[i]->tag))
match = 1;
};
if (match) {
ret = parse_table[i]->func(ast, sym_table,
*parse_table[i]->parse_table);
if (ret)
return -1;
return 0;
}
}
ERROR_PRINT("Cannot find suitable rule %s!\n",
(strict)? "strictly" : "relaxed");
ERROR_PRINT("for tag: %s in %s.\n",
ast->tag, par_ast->tag);
return -1;
}
int walk_something(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
int i, ret;
if (strlen(ast->contents)) {
ret = apply_parse_rule(ast, ast, sym_table,
parse_leaf_table, 0);
if (ret)
return -1;
}
for (i = 0; i < ast->children_num; i++) {
if (strstr(ast->children[i]->tag, "char")) {
printf("%s", ast->children[i]->contents);
} else {
ret = apply_parse_rule(ast->children[i], ast,
sym_table, parse_table, 1);
if (ret)
return -1;
}
}
return 0;
}
int walk_variable(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
if (strlen(ast->contents)) {
struct symbol *sym = NULL;
if (sym_table)
sym = find_symbol(sym_table, ast->contents);
if (!sym)
sym = find_symbol(&catastrophe.sym_table, ast->contents);
if (!sym) {
ERROR_PRINT("Unknown symbol: %s!\n", ast->contents);
return -1;
}
switch (sym->type) {
case SYM_PAR:
printf("PARAM(%s)", ast->contents);
break;
case SYM_VAR:
printf("VAR(%s)", ast->contents);
break;
case SYM_INT_VAR:
case SYM_LOC_VAR:
printf("%s", ast->contents);
break;
case SYM_STORAGE:
printf("STORAGE_COMPLEX(%s)", ast->contents);
break;
case SYM_FUN:
printf("catastrophe_%s_function", ast->contents);
break;
default:
ERROR_PRINT("Unexpected symbol type!\n");
return -1;
}
return 0;
}
ERROR_PRINT("Cannot walk variable (AST issue)!\n");
return -1;
}
int walk_array(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
struct symbol *sym = NULL;
if (sym_table)
sym = find_symbol(sym_table, ast->children[0]->contents);
if (!sym)
sym = find_symbol(&catastrophe.sym_table,
ast->children[0]->contents);
if (!sym) {
ERROR_PRINT("Unknown symbol: %s!\n",
ast->children[0]->contents);
return -1;
}
if (atoi(ast->children[2]->contents) >= sym->capacity) {
ERROR_PRINT("Array out of bounds access!\n");
return -1;
}
switch (sym->type) {
case SYM_EQN_VEC:
/* Such kind of symbols cannot be used locally. */
if (sym_table) {
ERROR_PRINT(
"Unable to use such kind of symbols here!\n");
return -1;
}
if (0 == strcmp(ast->children[0]->contents, "sysinput")) {
printf("equation->initial_vector[%s]",
ast->children[2]->contents);
} else if (0 == strcmp(ast->children[0]->contents,
"sysresult")) {
printf("equation->resulting_vector[%s]",
ast->children[2]->contents);
} else {
ERROR_PRINT("Unexpected EQN symbol!\n");
return -1;
}
break;
case SYM_INT_VEC:
if (!sym_table) {
ERROR_PRINT(
"Unable to use such kind of symbols here!\n");
return -1;
}
default:
printf("%s[%s]", ast->children[0]->contents,
ast->children[2]->contents);
}
return 0;
}
int walk_ass_regex(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
if (0 == strcmp(ast->contents, "<-")) {
printf(" = ");
return 0;
}
return 0;
}
char *known_functions[] = {
"RungeKutta",
"cos",
"sin",
"cexp",
NULL
};
int is_known_function(char *name)
{
int i;
for (i = 0; known_functions[i]; i++) {
if (0 == strcmp(known_functions[i], name))
return 1;
}
return 0;
}
int walk_funcname(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
if (strlen(ast->contents)) {
if (!is_known_function(ast->contents)) {
ERROR_PRINT("Unknown function!\n");
return -1;
}
printf("%s", ast->contents);
return 0;
}
ERROR_PRINT("Cannot walk function name (AST issue)!\n");
return -1;
}
int walk_echo(mpc_ast_t *ast, struct symbol_table *sym_table,
struct parse_table_entry *parse_table[])
{
printf("%s", ast->contents);
return 0;
}
struct parse_table_entry parse_value =
{"value|", walk_something, parse_value_table};
struct parse_table_entry parse_expression =
{"expression|", walk_something, parse_expression_table};
struct parse_table_entry parse_variable =
{"variable|", walk_variable, NULL};
struct parse_table_entry parse_leftside_variable =
{"leftside|variable|", walk_variable, NULL};
struct parse_table_entry parse_value_variable =
{"value|variable|", walk_variable, NULL};
struct parse_table_entry parse_value_integer =
{"value|integer|", walk_echo, NULL};
struct parse_table_entry parse_value_float =
{"value|float|", walk_echo, NULL};
struct parse_table_entry parse_array =
{"array|", walk_array, NULL};
struct parse_table_entry parse_product =
{"product|", walk_something, parse_product_table};
struct parse_table_entry parse_function =
{"function|", walk_something, parse_function_table};
struct parse_table_entry parse_funcname =
{"funcname|", walk_funcname, NULL};
struct parse_table_entry parse_ass_regex =
{"regex", walk_ass_regex, NULL};
struct parse_table_entry *parse_value_table[] = {
&parse_expression,
NULL
};
struct parse_table_entry *parse_product_table[] = {
&parse_function,
&parse_array,
&parse_value_variable,
&parse_value,
NULL
};
struct parse_table_entry *parse_function_table[] = {
&parse_expression,
&parse_funcname,
&parse_product,
NULL
};
struct parse_table_entry *parse_expression_table[] = {
&parse_array,
&parse_product,
&parse_function,
&parse_expression,
NULL
};
struct parse_table_entry *parse_assignment[] = {
&parse_function,
&parse_leftside_variable,
&parse_ass_regex,
&parse_variable,
&parse_array,
&parse_expression,
&parse_product,
NULL
};
struct parse_table_entry *parse_leaf_table[] = {
&parse_value_variable,
&parse_value_integer,
&parse_value_float,
NULL
};
int add_system(struct catastrophe *catastrophe, char *name,
unsigned int num_equations)
{
unsigned int i = catastrophe->num_systems;
if (strlen(name) >= SYS_NAME_LEN) {
ERROR_PRINT("Too long system name!\n");
return -1;
}
strcpy(catastrophe->systems[i].name, name);
catastrophe->systems[i].num_equations = num_equations;
current_system = &catastrophe->systems[i];
catastrophe->num_systems++;
return 0;
}
int walk_level0_parlist(mpc_ast_t *ast)
{
int i, ret;
for (i = 1; i < ast->children_num - 1; i+=2) {
DEBUG_PRINT("Param found: %s.\n", ast->children[i]->contents);
ret = add_symbol(&catastrophe.sym_table,
ast->children[i]->contents, SYM_PAR, 0);
if (ret)
return -1;
}
return 0;
}
int walk_varlist(mpc_ast_t *ast, struct symbol_table *sym_table,
enum symbol_type type)
{
int i, ret;
for (i = 1; i < ast->children_num - 1; i+=2) {
DEBUG_PRINT("Var found: %s.\n", ast->children[i]->contents);
ret = add_symbol(sym_table, ast->children[i]->contents,
type, 0);
if (ret)
return -1;
}
return 0;
}
int walk_level0_varlist(mpc_ast_t *ast)
{
return walk_varlist(ast, &catastrophe.sym_table, SYM_VAR);
}
int walk_system_varlist(mpc_ast_t *ast, struct system *system)
{
return walk_varlist(ast, &system->sym_table, SYM_LOC_VAR);
}
int walk_level0_strlist(mpc_ast_t *ast)
{
return walk_varlist(ast, &catastrophe.sym_table, SYM_STORAGE);
}
int walk_level0_veclist(mpc_ast_t *ast)
{
int i, ret;
for (i = 1; i < ast->children_num - 1; i++) {
mpc_ast_t *arr = ast->children[i];
DEBUG_PRINT("Vec found: %s[%s].\n",
arr->children[0]->contents,
arr->children[2]->contents);
ret = add_symbol(&catastrophe.sym_table,
arr->children[0]->contents, SYM_VEC,
(unsigned int)atoi(arr->children[2]->contents));
if (ret)
return -1;
}
return 0;
}
int walk_assignment(mpc_ast_t *ast, struct symbol_table *sym_table)
{
int ret;
ret = walk_something(ast, sym_table, parse_assignment);
if (ret)
return -1;
printf(";\n");
return 0;
}
int walk_block(mpc_ast_t *ast, struct symbol_table *sym_table)
{
int i, ret;
for (i = 1; i < ast->children_num - 1; i+=2) {
printf("\t");
ret = walk_assignment(ast->children[i], sym_table);
if (ret)
return -1;
}
return 0;
}
int walk_level0_system(mpc_ast_t *ast)
{
int i, ret;
DEBUG_PRINT("System found: %s(%s).\n", ast->children[1]->contents,
ast->children[3]->contents);
ret = add_system(&catastrophe, ast->children[1]->contents,
(unsigned int)atoi(ast->children[3]->contents));
if (ret)
return -1;
gen_system();
/* Add symbols defined in any SODE by default. */
add_symbol(¤t_system->sym_table, "t", SYM_INT_VAR, 0);
add_symbol(¤t_system->sym_table, "input", SYM_INT_VEC,
current_system->num_equations);
add_symbol(¤t_system->sym_table, "result", SYM_INT_VEC,
current_system->num_equations);
add_symbol(¤t_system->sym_table, "I", SYM_INT_VAR, 0);
add_symbol(¤t_system->sym_table, "M_PI", SYM_INT_VAR, 0);
/*
* Add a symbol with the name of the SODE for usage as functional
* arguments
*/
add_symbol(&catastrophe.sym_table, ast->children[1]->contents,
SYM_FUN, 0);
i = 5;
while (0 == strcmp(ast->children[i]->tag, "varlist|>")) {
ret = walk_system_varlist(ast->children[i], current_system);
if (ret)
return -1;
i++;
}
printf("{\n");
gen_system_vardecls();
ret = walk_block(ast->children[i], ¤t_system->sym_table);
if (ret)
return -1;
printf("}\n");
return 0;
}
int walk_level0_main_block(mpc_ast_t *ast)
{
int i, ret;
DEBUG_PRINT("Main block found.\n");
gen_main_block();
add_symbol(&catastrophe.sym_table, "sysinput", SYM_EQN_VEC,
current_system->num_equations);
add_symbol(&catastrophe.sym_table, "sysresult", SYM_EQN_VEC,
current_system->num_equations);
add_symbol(&catastrophe.sym_table, "I", SYM_INT_VAR, 0);
add_symbol(&catastrophe.sym_table, "M_PI", SYM_INT_VAR, 0);
add_symbol(&catastrophe.sym_table, "nope", SYM_INT_VAR, 0);
printf("{\n");
gen_main_block_prologue();
ret = walk_block(ast, NULL);
if (ret)
return -1;
gen_main_block_epilogue();
printf("}\n");
return 0;
}
int walk_level0(mpc_ast_t *ast)
{
int ret;
switch (parser_state) {
case PSTATE_INITIAL:
parser_state = PSTATE_CATASTROPHE;
break;
case PSTATE_CATASTROPHE:
parser_state = PSTATE_CATASTROPHE_NAME;
break;
case PSTATE_CATASTROPHE_NAME:
if (!strlen(ast->contents) ||
ast->tag != strstr(ast->tag, "variable|regex")) {
ERROR_PRINT("Catastrophe name expected!\n");
return -1;
}
strcpy(catastrophe.name, ast->contents);
parser_state = PSTATE_LEVEL0_DECL;
break;
case PSTATE_LEVEL0_AFTER_DECL:
if (0 == strcmp(ast->tag, "system|>")) {
gen_glob_parameters();
gen_glob_variables();
gen_glob_storage();
parser_state = PSTATE_LEVEL0_SYSTEM;
ret = walk_level0_system(ast);
if (ret)
return -1;
break;
}
case PSTATE_LEVEL0_DECL:
if (0 == strcmp(ast->tag, "parlist|>")) {
ret = walk_level0_parlist(ast);
if (ret)
return -1;
parser_state = PSTATE_LEVEL0_AFTER_DECL;
} else if (0 == strcmp(ast->tag, "varlist|>")) {
ret = walk_level0_varlist(ast);
if (ret)
return -1;
parser_state = PSTATE_LEVEL0_AFTER_DECL;
} else if (0 == strcmp(ast->tag, "veclist|>")) {
ret = walk_level0_veclist(ast);
if (ret)
return -1;
parser_state = PSTATE_LEVEL0_AFTER_DECL;
} else if (0 == strcmp(ast->tag, "strlist|>")) {
ret = walk_level0_strlist(ast);
if (ret)
return -1;
parser_state = PSTATE_LEVEL0_AFTER_DECL;
} else {
ERROR_PRINT("One of declaration lists expected!\n");
return -1;
}
break;
case PSTATE_LEVEL0_SYSTEM:
if (0 == strcmp(ast->tag, "system|>")) {
parser_state = PSTATE_LEVEL0_SYSTEM;
ret = walk_level0_system(ast);
if (ret)
return -1;
break;
}
parser_state = PSTATE_LEVEL0_AFTER_MAIN_BLOCK;
ret = walk_level0_main_block(ast);
if (ret)
return -1;
break;
case PSTATE_LEVEL0_AFTER_MAIN_BLOCK:
break;
default:
ERROR_PRINT("Unpredictable parser state: %d!\n", parser_state);
return -1;
};
return 0;
}
int walk_catastrophe(mpc_ast_t *ast)
{
int i, ret;
if (strlen(ast->contents)) {
ERROR_PRINT("Top-level non-terminal is expected!\n");
return -1;
}
for (i = 0; i < ast->children_num; i++) {
ret = walk_level0(ast->children[i]);
if (ret)
return -1;
}
gen_rest();
return 0;
}
int walk_ast(mpc_ast_t *ast)
{
return walk_catastrophe(ast);
}
int parser(char *input)
{
mpc_parser_t *Integer = mpc_new("integer");
mpc_parser_t *Float = mpc_new("float");
mpc_parser_t *Expression = mpc_new("expression");
mpc_parser_t *Product = mpc_new("product");
mpc_parser_t *Value = mpc_new("value");
mpc_parser_t *Variable = mpc_new("variable");
mpc_parser_t *Array = mpc_new("array");
mpc_parser_t *Leftside = mpc_new("leftside");
mpc_parser_t *Assignment = mpc_new("assignment");
mpc_parser_t *Block = mpc_new("block");
mpc_parser_t *Varlist = mpc_new("varlist");
mpc_parser_t *Parlist = mpc_new("parlist");
mpc_parser_t *Veclist = mpc_new("veclist");
mpc_parser_t *Strlist = mpc_new("strlist");
mpc_parser_t *System = mpc_new("system");
mpc_parser_t *Catastrophe = mpc_new("catastrophe");
mpc_parser_t *Declare = mpc_new("declare");
mpc_parser_t *Function = mpc_new("function");
mpc_parser_t *Funcname = mpc_new("funcname");
mpca_lang(MPCA_LANG_DEFAULT,
"integer \"integer\" : /[0-9]+/ ;"
"float \"float\" : /[0-9]*\\.?[0-9]+/ ;"
"variable \"variable\" : /[A-Za-z'_']+[A-Za-z0-9'_']*/ ;"
"funcname \"funcname\" : /[A-Za-z'_']+[A-Za-z0-9'_']*/ ;"
"array : <variable> '[' <expression> ']' ;"
"expression : <product> (('+' | '-') <product>)* ;"
"product : <value> (('*' | '/') <value>)* ;"
"value : '(' <expression> ')' | <float> | <integer> | <function> | <array> | <variable> ;"
"leftside : <array> | <variable> ;"
"assignment : <leftside> /<-/ <expression> ;"
"block : /BEGIN/ (<assignment> ';')* /END/ ;"
"strlist: /STORAGE/ (<variable> (','|';'))+ ;"
"varlist: /VARIABLES/ (<variable> (','|';'))+ ;"
"parlist: /PARAMETERS/ (<variable> (','|';'))+ ;"
"veclist: /VECTORS/ (<array> (','|';'))+ ;"
"system: /SYSTEM/ <variable> '('<integer>')' <varlist> <block> ;"
"declare: <parlist> | <varlist> | <veclist> | <strlist> ;"
"catastrophe : /^/ /CATASTROPHE/ <variable> (<declare>)+ (<system>)+ <block>'.' /$/ ;"
"function: <funcname> '(' <expression> (',' <expression>)* ')' ;",
Integer,
Float,
Expression,
Product,
Value,
Catastrophe,
Variable,
Array,
Leftside,
Assignment,
Block,
Varlist,
Parlist,
Veclist,
System,
Declare,
Function,
Funcname,
Strlist
);
mpc_result_t result;
if (mpc_parse("stdin>", input, Catastrophe, &result)) {
//mpc_ast_print(result.output);
gen_headers();
walk_ast(result.output);
mpc_ast_delete(result.output);
printf("\n\n");
} else {
mpc_err_print(result.error);
mpc_err_delete(result.error);
}
mpc_cleanup(19,
Integer,
Float,
Expression,
Product,
Value,
Catastrophe,
Variable,
Array,
Leftside,
Assignment,
Block,
Varlist,
Parlist,
Veclist,
System,
Declare,
Function,
Funcname,
Strlist
);
}