-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_c.wax
751 lines (703 loc) · 22.7 KB
/
target_c.wax
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
; ----------------------------------------------------------------------------
; - C language target -
; ----------------------------------------------------------------------------
(func node_to_c_preprocess (param node (struct node)) (result str)
(let value str (get node value))
(if (= (get node type) @NODE_PREPROCESS) (then
(set value "")
(for i 1 (< i (# (get node value))) 1 (do
(<< value (get (get node value) i))
))
))
(return value)
)
(func node_to_c_type (param name str) (param node (struct node)) (param element_only int) (result str)
(let type str (get node value))
(let suffix str "")
(if (= (get node value) "str") (then
(set type "char*")
))
(if (= (get node value) "vec") (then
(set type (call node_to_c_type "" (get node child 1) 0))
(if (<> name "") (then
(<< suffix "[")
(<< suffix (call node_to_c_preprocess (get node child 0)))
(<< suffix "]")
) (else
(<< type "*")
))
))
(if (= (get node value) "arr") (then
(set type (call node_to_c_type "" (get node child 0) 0))
(if (! element_only) (then
(<< type "*")
))
))
(if (= (get node value) "struct") (then
(set type "struct ")
(<< type (get node child 0 value))
(<< type "*")
))
(if (= type "map") (then
(set type "std::map<")
(<< type (call node_to_c_type "" (get node child 0) 0))
(<< type ",")
(<< type (call node_to_c_type "" (get node child 1) 0))
(<< type ">")
))
(if (= type "func") (then
(let return_type str "void")
(let params str "void")
(let i int 0)
(while (< i (# (get node child))) (do
(let child_node (struct node) (get node child i))
(if (= (get child_node value) "param") (then
(if (= params "void") (then
(set params "")
) (else
(<< params ", ")
))
(let c_var str "")
(set c_var (call node_to_c_type (get child_node child 0 value) (get child_node child 1) 0))
(<< params c_var)
(remove (get node child) i 1)
(set i (- i 1))
))
(if (= (get child_node value) "result") (then
(set return_type (call node_to_c_type "" (get child_node child 0) 0))
(remove (get node child) i 1)
(set i (- i 1))
))
(set i (+ i 1))
))
(let c_function str "")
(if (> (# (get node child)) 0) (then
(if (= (get node child 0 type) @NODE_STR) (then
(let func_name str (get node child 0 value))
(if (= func_name "main") (then
(set func_name "main_args")
(set params "char **args")
(set return_type "int")
))
(<< c_function return_type)
(<< c_function " ")
(<< c_function func_name)
(<< c_function "(")
(<< c_function params)
(<< c_function ")")
) (else
(<< c_function "[=](")
(<< c_function params)
(<< c_function ")")
))
) (else
(<< c_function return_type)
(<< c_function " (*")
(<< c_function name)
(<< c_function ")(")
(<< c_function params)
(<< c_function ")")
(set name "")
))
(set type c_function)
))
(let out str type)
(if (<> name "") (then
(<< out " ")
(<< out name)
(<< out suffix)
))
(return out)
)
(func node_to_c_tostring (param symbol (arr (struct node_symbol))) (param node (struct node)) (param castint int) (result str)
(let node_type int (call node_symbol_node_type symbol node))
(if (= node_type @NODE_TYPE_STR) (then
(return "tostring_char_p")
))
(if (= node_type @NODE_TYPE_INT) (then
(return (? castint "tostring_int" "tostring_char"))
))
(if (= node_type @NODE_TYPE_FLOAT) (then
(return "tostring_float")
))
(return "tostring_unknown")
)
(func node_to_c_length (param symbol (arr (struct node_symbol))) (param node (struct node)) (result str)
(let node_type int (call node_symbol_node_type symbol node))
(if (= node_type @NODE_TYPE_STR) (then
(return "strlen")
))
(return "ARRAY_LENGTH")
)
(func node_to_c_has_keyword (param node (struct node)) (param keyword str) (result int)
(if (= (get node value) keyword) (then (return 1)))
(for i 0 (< i (# (get node child))) 1 (do
(if (<> (call node_to_c_has_keyword (get node child i) keyword) 0) (then
(return 1)
))
))
(return 0)
)
(func node_to_c (param depth int) (param parent_symbol (arr (struct node_symbol))) (param node (struct node)) (result str)
(let s str (alloc str ""))
(let symbol (arr (struct node_symbol)) (call node_symbol_scope_node parent_symbol node))
(set depth (+ depth 1))
(set node (call node_copy node))
(if (= (get node type) @NODE_ROOT) (then
(let has_main int 0)
(for i 0 (< i (# (get node child))) 1 (do
(if (&&
(= (get node child i value) "func")
(= (get node child i child 0 value) "main")
) (then
(set has_main 1)
))
))
(if (<> (call node_to_c_has_keyword node "print") 0) (then
(<< s "#include <stdio.h>\n")
(<< s "#include <stdlib.h>\n")
(<< s "#include <string.h>\n")
(<< s "\n")
(<< s "char *tostring_char_p(char *value) { return value; }\n")
(<< s "char *tostring_char(int value) { static char buf[256]; sprintf(buf, \"%c\", value); return buf; }\n")
(<< s "char *tostring_int(int value) { static char buf[256]; sprintf(buf, \"%d\", value); return buf; }\n")
(<< s "char *tostring_float(float value) { static char buf[256]; sprintf(buf, \"%g\", value); return buf; }\n")
(<< s "char *tostring_double(double value) { static char buf[256]; sprintf(buf, \"%g\", value); return buf; }\n")
(<< s "void *STRING_ALLOC(char *str) {")
(<< s " char *data = malloc(strlen(str) + 1);")
(<< s " memcpy(data, str, strlen(str) + 1);")
(<< s " return data;")
(<< s "}\n")
(<< s "char *STRING_APPEND(char *str, char *append) {")
(<< s " char *data = realloc(str, strlen(str) + strlen(append) + 1);")
(<< s " strcat(data, append);")
(<< s " return data;")
(<< s "}\n")
))
(if has_main (then
(<< s "struct ARRAY_HEADER {")
(<< s " int element_size;")
(<< s " int element_count;")
(<< s "};\n")
(<< s "void *ARRAY_ALLOC(int element_size, int element_count) {")
(<< s " struct ARRAY_HEADER *header = malloc(sizeof(struct ARRAY_HEADER) + (element_size * element_count));")
(<< s " header->element_size = element_size;")
(<< s " header->element_count = element_count;")
(<< s " void *data = ((char *)header) + sizeof(struct ARRAY_HEADER);")
(<< s " return data;")
(<< s "}\n")
(<< s "void *ARRAY_INSERT(void *data, int index) {")
(<< s " struct ARRAY_HEADER *header = (void *)((char *)data - sizeof(struct ARRAY_HEADER));")
(<< s " header = realloc(header, sizeof(struct ARRAY_HEADER) + (header->element_size * (header->element_count + 1)));")
(<< s " data = ((char *)header) + sizeof(struct ARRAY_HEADER);")
(<< s " memmove((char *)data + ((index + 1) * header->element_size), (char *)data + (index * header->element_size), (header->element_count - index) * header->element_size);")
(<< s " header->element_count = header->element_count + 1;")
(<< s " return data;")
(<< s "}\n")
(<< s "void *ARRAY_REMOVE(void *data, int index, int count) {")
(<< s " struct ARRAY_HEADER *header = (void *)((char *)data - sizeof(struct ARRAY_HEADER));")
(<< s " memmove((char *)data + (index * header->element_size), (char *)data + ((index + count) * header->element_size), (header->element_count - (index + count)) * header->element_size);")
(<< s " header->element_count = header->element_count - count;")
(<< s " header = realloc(header, sizeof(struct ARRAY_HEADER) + (header->element_size * header->element_count));")
(<< s " data = (void *)((char *)header + sizeof(struct ARRAY_HEADER));")
(<< s " return data;")
(<< s "}\n")
(<< s "void ARRAY_FREE(void *data) {")
(<< s " struct ARRAY_HEADER *header = (void *)((char *)data - sizeof(struct ARRAY_HEADER));")
(<< s " free(header);")
(<< s "}\n")
(<< s "int ARRAY_LENGTH(void *data) {")
(<< s " struct ARRAY_HEADER *header = (void *)((char *)data - sizeof(struct ARRAY_HEADER));")
(<< s " return header->element_count;")
(<< s "}\n")
))
(<< s "\n\n")
(for i 0 (< i (# (get node child))) 1 (do
(let child str (call node_to_c 0 symbol (get node child i)))
(<< s child)
(if (= (get node child i value) "let") (then
(<< s ";")
))
(<< s "\n")
))
(if has_main (then
(<< s "int main(int argc, char** argv) {\n")
(<< s " char **args = ARRAY_ALLOC(sizeof(char *),argc);\n")
(<< s " for (int i = 0; i < argc; i++) { args[i] = argv[i]; }\n")
(<< s " return main_args(args);\n")
(<< s "};\n")
))
))
(if (||
(= (get node value) "+")
(= (get node value) "&&")
(= (get node value) "||")
) (then
(set node (call node_expand node))
))
(if (= (get node type) @NODE_EXPR) (then
(let block_start str (alloc str "("))
(<< block_start (get node value))
(let block_end str (alloc str ")"))
(let block_seperator str (alloc str " "))
(if (= (get node value) "") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "@define") (then
(set block_start "#define ")
(<< block_start (get node child 0 value))
(<< block_start " ")
(<< block_start (get node child 1 value))
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "@if") (then
(set block_start "")
(set block_end "")
(if (= (get node child 0 value) "TARGET_C") (then
(remove (get node child) 0 1)
(remove (get node child) 0 1)
) (else
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
))
(if (= (get node value) "extern") (then
(set block_start "")
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "asm") (then
(let quotedline str (get node child 0 value))
(let line str "")
(for j 1 (< j (- (# quotedline) 1)) 1 (do
(if (<> (get quotedline j) 92) (then
(<< line (get quotedline j))
))
))
(set block_start line)
(set block_end "\n")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "struct") (then
(set block_start "struct ")
(<< block_start (get node child 0 value))
(<< block_start " {")
(set block_end "};")
(remove (get node child) 0 1)
))
(if (= (get node value) "func") (then
(set block_start (call node_to_c_type "" node 0))
(<< block_start " {")
(set block_end "}")
(if (= (get node child 0 type) @NODE_STR) (then
(remove (get node child) 0 1)
))
))
(if (= (get node value) "call") (then
(let fname_node (struct node) (get node child 0))
(set block_start (get fname_node value))
(<< block_start "(")
(set block_end ")")
(remove (get node child) 0 1)
(set block_seperator ", ")
))
(if (||
(= (get node value) "let")
(= (get node value) "local")
) (then
(let name str (get node child 0 value))
(let type str (call node_to_c_type name (get node child 1) 0))
(set block_start type)
(<< block_start " ")
(set block_end "")
(remove (get node child) 0 1)
(remove (get node child) 0 1)
(if (= (# (get node child)) 1) (then
(insert (get node child) 0 (call node_new @NODE_STR "="))
(if (&& (= (get node child 1 value) "alloc") (= (get node child 1 child 0 value) "arr")) (then
(for i 1 (< i (# (get node child 1 child))) 1 (do
(<< block_end ";")
(<< block_end name)
(<< block_end "[")
(<< block_end (cast (- i 1) str))
(<< block_end "]=")
(<< block_end (get node child 1 child i value))
))
))
))
))
(if (= (get node value) "alloc") (then
(set block_start "")
(set block_end "")
(if (= (get node child 0 value) "str") (then
(let alloc_string str "\"\"")
(if (> (# (get node child)) 1) (then
(set alloc_string (get node child 1 value))
))
(set block_start "STRING_ALLOC(")
(<< block_start alloc_string)
(<< block_start ")")
))
(if (= (get node child 0 type) @NODE_EXPR) (then
(if (= (get node child 0 value) "arr") (then
(set block_start "ARRAY_ALLOC(sizeof(")
(<< block_start (call node_to_c_type "" (get node child 0) 1))
(<< block_start "),")
(<< block_start (cast (- (# (get node child)) 1) str))
(<< block_start ")")
))
(if (= (get node child 0 value) "vec") (then
(set block_start "{")
(for i 1 (< i (# (get node child))) 1 (do
(<< block_start (call node_to_c depth symbol (get node child i)))
(if (< i (- (# (get node child)) 1)) (then
(<< block_start ",")
))
))
(<< block_start "}")
))
(if (= (get node child 0 value) "struct") (then
(set block_start "ARRAY_ALLOC(sizeof(struct ")
(<< block_start (get node child 0 child 0 value))
(<< block_start "),1)")
))
(if (= (get node child 0 value) "map") (then
(set block_start "{}")
))
))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "free") (then
(let array str (call node_to_c depth symbol (get node child 0)))
(set block_start "ARRAY_FREE(")
(<< block_start array)
(set block_end ")")
(remove (get node child) 0 1)
))
(if (= (get node value) "#") (then
(set block_start (call node_to_c_length symbol (get node child 0)))
(<< block_start "(")
; (set block_start "ARRAY_LENGTH(")
(set block_end ")")
))
(if (= (get node value) "insert") (then
(let init str "")
(if (&& (= (get node child 2 value) "alloc") (= (get node child 2 child 0 value) "arr")) (then
(for i 1 (< i (# (get node child 2 child))) 1 (do
(<< init ";")
(<< init (get node child 0 value))
(<< init "[")
(<< init (get node child 1 value))
(<< init "]")
(<< init "[")
(<< init (cast (- i 1) str))
(<< init "]=")
(<< init (get node child 2 child i value))
))
))
(let array str (call node_to_c depth symbol (get node child 0)))
(let position str (call node_to_c depth symbol (get node child 1)))
(let value str (call node_to_c depth symbol (get node child 2)))
(set block_start "{int __index__=")
(<< block_start position)
(<< block_start ";")
(<< block_start array)
(<< block_start "=ARRAY_INSERT(")
(<< block_start array)
(<< block_start ",__index__);")
(<< block_start array)
(<< block_start "[__index__]=")
(<< block_start value)
(<< block_start init)
(<< block_start ";}")
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "remove") (then
(let array str (call node_to_c depth symbol (get node child 0)))
(let first str (call node_to_c depth symbol (get node child 1)))
(let last str (call node_to_c depth symbol (get node child 2)))
(set block_start array)
(<< block_start "=ARRAY_REMOVE(")
(<< block_start array)
(<< block_start ",")
(<< block_start first)
(<< block_start ",")
(<< block_start last)
(set block_end ")")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "slice") (then
(set block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start ".substr(")
(<< block_start (call node_to_c depth symbol (get node child 1)))
(<< block_start ",")
(<< block_start (call node_to_c depth symbol (get node child 2)))
(set block_end ")")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "get") (then
(set block_start (call node_to_c depth symbol (get node child 0)))
(set block_end "")
(for i 1 (< i (# (get node child))) 1 (do
(let child (struct node) (get node child i))
(if (&& (= (get child type) @NODE_STR) (> (# (get child value)) 1)) (then
(<< block_end "->")
(<< block_end (call node_to_c depth symbol (get node child i)))
(<< block_end "")
) (else
(<< block_end "[")
(<< block_end (call node_to_c depth symbol (get node child i)))
(<< block_end "]")
))
))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "set") (then
(set block_start (call node_to_c depth symbol (get node child 0)))
(set block_end "")
(let n int (- (# (get node child)) 1))
(for i 1 (< i n) 1 (do
(let child (struct node) (get node child i))
(if (&& (= (get child type) @NODE_STR) (> (# (get child value)) 1)) (then
(<< block_end "->")
(<< block_end (call node_to_c depth symbol (get node child i)))
(<< block_end "")
) (else
(<< block_end "[")
(<< block_end (call node_to_c depth symbol (get node child i)))
(<< block_end "]")
))
))
(<< block_end " = ")
(let node_type int (call node_symbol_node_type symbol (get node child n)))
(if (= node_type @NODE_TYPE_STR) (then (<< block_end "STRING_ALLOC(")))
(<< block_end (call node_to_c depth symbol (get node child n)))
(if (= node_type @NODE_TYPE_STR) (then (<< block_end ")")))
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "cast") (then
(if (= (get node child 1 value) "str") (then
(set block_start "tostring_int(")
; (set block_start (call node_to_c_tostring symbol (get node child 0)))
; (<< block_start "(")
(<< block_start (call node_to_c depth symbol (get node child 0)))
(set block_end ")")
))
(if (= (get node child 1 value) "int") (then
(set block_start "(int)")
(set block_end (call node_to_c depth symbol (get node child 0)))
))
(remove (get node child) 0 1)
(remove (get node child) 0 1)
))
(if (= (get node value) "then") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "else") (then
(set block_start "else {")
(set block_end "}")
))
(if (= (get node value) "for") (then
(if (= (# (get node child)) 5) (then
(set block_start "for (int ")
(<< block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start " = ")
(<< block_start (call node_to_c depth symbol (get node child 1)))
(<< block_start "; ")
(<< block_start (call node_to_c depth symbol (get node child 2)))
(<< block_start "; ")
(<< block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start " += ")
(<< block_start (call node_to_c depth symbol (get node child 3)))
(<< block_start ") ")
(remove (get node child) 0 4)
(set block_end "")
))
(if (= (# (get node child)) 4) (then
(set block_start "for (const auto &[")
(<< block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start ",")
(<< block_start (call node_to_c depth symbol (get node child 1)))
(<< block_start "] : ")
(<< block_start (call node_to_c depth symbol (get node child 2)))
(<< block_start ") ")
(remove (get node child) 0 3)
(set block_end "")
))
))
(if (||
(= (get node value) "if")
(= (get node value) "while")
) (then
(set block_start (get node value))
(<< block_start " ")
(let block_simple int (<> (get node child 0 type) @NODE_EXPR))
(if block_simple (then
(<< block_start "(")
))
(<< block_start (call node_to_c depth symbol (get node child 0)))
(if block_simple (then
(<< block_start ")")
))
(<< block_start " ")
(set block_end "")
(remove (get node child) 0 1)
))
(if (= (get node value) "do") (then
(set block_start "{")
(set block_end "}")
))
(if (= (get node value) "break") (then
(set block_start "break")
(set block_end "")
))
(if (= (get node value) "?") (then
(set block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start " ? ")
(<< block_start (call node_to_c depth symbol (get node child 1)))
(<< block_start " : ")
(<< block_start (call node_to_c depth symbol (get node child 2)))
(set block_end "")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
))
(if (= (get node value) "print") (then
(set block_start "puts(")
(<< block_start (call node_to_c_tostring symbol (get node child 0) 1))
(<< block_start "(")
(set block_end "))")
))
(if (||
(= (get node value) ">>")
(= (get node value) "<<")
(= (get node value) "=")
(= (get node value) "&&")
(= (get node value) "||")
(= (get node value) ">=")
(= (get node value) "<=")
(= (get node value) "<>")
(= (get node value) "+")
(= (get node value) "-")
(= (get node value) "*")
(= (get node value) "/")
(= (get node value) "^")
(= (get node value) "%")
(= (get node value) "&")
(= (get node value) "|")
(= (get node value) "~")
(= (get node value) "<")
(= (get node value) ">")
) (then
(set block_start "(")
(set block_end ")")
(if (= (get node value) "=") (then
(if (|| (= (get node child 0 type) @NODE_STR_QUOTE) (= (get node child 1 type) @NODE_STR_QUOTE)) (then
(set block_start "(strcmp(")
(set block_end ") == 0 ? 1 : 0)")
(set node value ",")
) (else
(set node value "==")
))
))
(if (= (get node value) "<>") (then
(set node value "!=")
))
(if (&&
(= (get node value) "<<")
(= (get node child 0 type) @NODE_STR)
) (then
(set block_start (get node child 0 value))
(<< block_start "=")
(<< block_start "STRING_APPEND(")
(<< block_start (call node_to_c depth symbol (get node child 0)))
(<< block_start ",")
(<< block_start (call node_to_c_tostring symbol (get node child 1) 0))
(<< block_start "(")
(<< block_start (call node_to_c depth symbol (get node child 1)))
(set block_end "))")
(while (> (# (get node child)) 0) (do
(remove (get node child) 0 1)
))
) (else
(insert (get node child) 1 (call node_new @NODE_STR (get node value)))
))
))
(if (= (get node value) "return") (then
(set block_start "return ")
(set block_end "")
))
(<< s block_start)
(for i 0 (< i (# (get node child))) 1 (do
(let node_child (struct node) (get node child i))
(let indent_needed int (call node_indent_needed node node_child))
(let indent_depth int (? indent_needed depth (- depth 1)))
(let indent str " ")
(if indent_needed (then
(<< s "\n")
(for i 0 (< i indent_depth) 1 (do
(<< s indent)
))
) (else (if (> i 0) (then
(<< s block_seperator)
))))
(let child str (call node_to_c indent_depth symbol node_child))
(<< s child)
(if (||
(= (get node value) "")
(= (get node value) "func")
(= (get node value) "struct")
(= (get node value) "do")
(= (get node value) "then")
(= (get node value) "else")
) (then
(<< s ";")
))
(if (&&
indent_needed
(= i (- (# (get node child)) 1))
) (then
(<< s "\n")
(for i 0 (< i (- indent_depth 1)) 1 (do
(<< s indent)
))
))
))
(<< s block_end)
))
(if (<> (get node type) @NODE_EXPR) (then
(let value str (call node_to_c_preprocess node))
(if (= (get node type) @NODE_COMMENT) (then
(set value "// ")
(<< value (get node value))
))
; (if (= (get node type) @NODE_CHAR) (then
; (<< s "(int)")
; ))
(<< s value)
))
(return s)
)