-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathsugar.c
1302 lines (1025 loc) · 31.9 KB
/
sugar.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
#include "sugar.h"
#include "../ast/astbuild.h"
#include "../ast/id.h"
#include "../ast/printbuf.h"
#include "../pass/syntax.h"
#include "../pkg/ifdef.h"
#include "../pkg/package.h"
#include "../type/alias.h"
#include "../type/assemble.h"
#include "../type/sanitise.h"
#include "../type/subtype.h"
#include "../ast/stringtab.h"
#include "../ast/token.h"
#include "../../libponyrt/mem/pool.h"
#include "ponyassert.h"
#include <stdio.h>
#include <string.h>
static ast_t* make_runtime_override_defaults(ast_t* ast)
{
pony_assert(ast != NULL);
// set it as a bare function
token_id cap = TK_AT;
BUILD(runtime_override_defaults, ast,
NODE(TK_FUN, AST_SCOPE
NODE(cap)
ID("runtime_override_defaults") // name
NONE // typeparams
NODE(TK_PARAMS, NODE(TK_PARAM, ID("rto") NODE(TK_NOMINAL, ID("$0") ID("RuntimeOptions") NONE NONE NONE) NONE)) // params
NONE // return type
NONE // error
NODE(TK_SEQ, NODE(TK_TRUE))
NONE
));
return runtime_override_defaults;
}
static ast_t* make_create(ast_t* ast)
{
pony_assert(ast != NULL);
// Default constructors on classes can be iso, on actors they must be tag
token_id cap = TK_NONE;
switch(ast_id(ast))
{
case TK_STRUCT:
case TK_CLASS:
cap = TK_ISO;
break;
default: {}
}
BUILD(create, ast,
NODE(TK_NEW, AST_SCOPE
NODE(cap)
ID("create") // name
NONE // typeparams
NONE // params
NONE // return type
NONE // error
NODE(TK_SEQ, NODE(TK_TRUE))
NONE
));
return create;
}
bool has_member(ast_t* members, const char* name)
{
name = stringtab(name);
ast_t* member = ast_child(members);
while(member != NULL)
{
ast_t* id;
switch(ast_id(member))
{
case TK_FVAR:
case TK_FLET:
case TK_EMBED:
id = ast_child(member);
break;
default:
id = ast_childidx(member, 1);
break;
}
if(ast_name(id) == name)
return true;
member = ast_sibling(member);
}
return false;
}
static void add_default_runtime_override_defaults_method(ast_t* ast)
{
pony_assert(ast != NULL);
ast_t* members = ast_childidx(ast, 4);
// If have no @runtime_override_defaults method, add one.
if(has_member(members, "runtime_override_defaults"))
return;
ast_append(members, make_runtime_override_defaults(ast));
}
static void add_default_constructor(ast_t* ast)
{
pony_assert(ast != NULL);
ast_t* members = ast_childidx(ast, 4);
// If we have no constructors and no "create" member, add a "create"
// constructor.
if(has_member(members, "create"))
return;
ast_t* member = ast_child(members);
while(member != NULL)
{
switch(ast_id(member))
{
case TK_NEW:
return;
default: {}
}
member = ast_sibling(member);
}
ast_append(members, make_create(ast));
}
static ast_result_t sugar_module(pass_opt_t* opt, ast_t* ast)
{
ast_t* docstring = ast_child(ast);
ast_t* package = ast_parent(ast);
pony_assert(ast_id(package) == TK_PACKAGE);
if(strcmp(package_name(package), "$0") != 0)
{
// Every module not in builtin has an implicit use builtin command.
// Since builtin is always the first package processed it is $0.
BUILD(builtin, ast,
NODE(TK_USE,
NONE
STRING(stringtab("builtin"))
NONE));
ast_add(ast, builtin);
}
if((docstring == NULL) || (ast_id(docstring) != TK_STRING))
return AST_OK;
ast_t* package_docstring = ast_childlast(package);
if(ast_id(package_docstring) == TK_STRING)
{
ast_error(opt->check.errors, docstring,
"the package already has a docstring");
ast_error_continue(opt->check.errors, package_docstring,
"the existing docstring is here");
return AST_ERROR;
}
ast_append(package, docstring);
ast_remove(docstring);
return AST_OK;
}
static void sugar_docstring(ast_t* ast)
{
pony_assert(ast != NULL);
AST_GET_CHILDREN(ast, cap, id, type_params, params, return_type,
error, body, docstring);
if(ast_id(docstring) == TK_NONE)
{
ast_t* first = ast_child(body);
// First expression in body is a docstring if it is a string literal and
// there are any other expressions in the body sequence
if((first != NULL) &&
(ast_id(first) == TK_STRING) &&
(ast_sibling(first) != NULL))
{
ast_pop(body);
ast_replace(&docstring, first);
}
}
}
static ast_result_t sugar_entity(pass_opt_t* opt, ast_t* ast, bool add_create,
token_id def_def_cap)
{
(void)opt;
AST_GET_CHILDREN(ast, id, typeparams, defcap, traits, members);
if(ast_name(id) == stringtab("Main"))
add_default_runtime_override_defaults_method(ast);
if(add_create)
add_default_constructor(ast);
if(ast_id(defcap) == TK_NONE)
ast_setid(defcap, def_def_cap);
// Build a reverse sequence of all field initialisers.
BUILD(init_seq, members, NODE(TK_SEQ));
ast_t* member = ast_child(members);
while(member != NULL)
{
switch(ast_id(member))
{
case TK_FLET:
case TK_FVAR:
case TK_EMBED:
{
AST_GET_CHILDREN(member, f_id, f_type, f_init);
if(ast_id(f_init) != TK_NONE)
{
// Replace the initialiser with TK_NONE.
ast_swap(f_init, ast_from(f_init, TK_NONE));
// id = init
BUILD(init, member,
NODE(TK_ASSIGN,
NODE(TK_REFERENCE, TREE(f_id))
TREE(f_init)));
ast_add(init_seq, init);
}
break;
}
default: {}
}
member = ast_sibling(member);
}
// Add field initialisers to all constructors.
if(ast_child(init_seq) != NULL)
{
member = ast_child(members);
while(member != NULL)
{
switch(ast_id(member))
{
case TK_NEW:
{
AST_GET_CHILDREN(member, n_cap, n_id, n_typeparam, n_params,
n_result, n_partial, n_body);
pony_assert(ast_id(n_body) == TK_SEQ);
sugar_docstring(member);
ast_t* init = ast_child(init_seq);
while(init != NULL)
{
ast_add(n_body, init);
init = ast_sibling(init);
}
break;
}
default: {}
}
member = ast_sibling(member);
}
}
ast_free_unattached(init_seq);
return AST_OK;
}
static ast_result_t sugar_typeparam(ast_t* ast)
{
AST_GET_CHILDREN(ast, id, constraint);
if(ast_id(constraint) == TK_NONE)
{
REPLACE(&constraint,
NODE(TK_NOMINAL,
NONE
TREE(id)
NONE
NONE
NONE));
}
return AST_OK;
}
static ast_result_t check_params(pass_opt_t* opt, ast_t* params)
{
pony_assert(params != NULL);
ast_result_t result = AST_OK;
// Check each parameter.
for(ast_t* p = ast_child(params); p != NULL; p = ast_sibling(p))
{
if(ast_id(p) == TK_ELLIPSIS)
continue;
AST_GET_CHILDREN(p, id, type, def_arg);
if(ast_id(id) != TK_ID)
{
ast_error(opt->check.errors, p, "expected parameter name");
result = AST_ERROR;
}
else if(!is_name_internal_test(ast_name(id)) && !check_id_param(opt, id))
{
result = AST_ERROR;
}
if(ast_id(type) == TK_NONE)
{
ast_error(opt->check.errors, type, "expected parameter type");
result = AST_ERROR;
}
}
return result;
}
static ast_result_t check_method(pass_opt_t* opt, ast_t* method)
{
pony_assert(method != NULL);
ast_result_t result = AST_OK;
ast_t* params = ast_childidx(method, 3);
result = check_params(opt, params);
return result;
}
static ast_result_t sugar_new(pass_opt_t* opt, ast_t* ast)
{
AST_GET_CHILDREN(ast, cap, id, typeparams, params, result);
// Return type default to ref^ for classes, val^ for primitives, and
// tag^ for actors.
if(ast_id(result) == TK_NONE)
{
token_id tcap = ast_id(cap);
if(tcap == TK_NONE)
{
switch(ast_id(opt->check.frame->type))
{
case TK_PRIMITIVE: tcap = TK_VAL; break;
case TK_ACTOR: tcap = TK_TAG; break;
default: tcap = TK_REF; break;
}
ast_setid(cap, tcap);
}
ast_replace(&result, type_for_this(opt, ast, tcap, TK_EPHEMERAL));
}
sugar_docstring(ast);
return check_method(opt, ast);
}
static ast_result_t sugar_be(pass_opt_t* opt, ast_t* ast)
{
(void)opt;
AST_GET_CHILDREN(ast, cap, id, typeparams, params, result, can_error, body);
ast_setid(cap, TK_TAG);
if(ast_id(result) == TK_NONE)
{
// Return type is None.
ast_t* type = type_sugar(ast, NULL, "None");
ast_replace(&result, type);
}
sugar_docstring(ast);
return check_method(opt, ast);
}
void fun_defaults(ast_t* ast)
{
pony_assert(ast != NULL);
AST_GET_CHILDREN(ast, cap, id, typeparams, params, result, can_error, body,
docstring);
// If the receiver cap is not specified, set it to box.
if(ast_id(cap) == TK_NONE)
ast_setid(cap, TK_BOX);
// If the return value is not specified, set it to None.
if(ast_id(result) == TK_NONE)
{
ast_t* type = type_sugar(ast, NULL, "None");
ast_replace(&result, type);
}
// If the return type is None, add a None at the end of the body, unless it
// already ends with an error or return statement
if(is_none(result) && (ast_id(body) != TK_NONE))
{
ast_t* last_cmd = ast_childlast(body);
if(ast_id(last_cmd) != TK_ERROR && ast_id(last_cmd) != TK_RETURN)
{
BUILD(ref, body, NODE(TK_REFERENCE, ID("None")));
ast_append(body, ref);
}
}
}
static ast_result_t sugar_fun(pass_opt_t* opt, ast_t* ast)
{
fun_defaults(ast);
sugar_docstring(ast);
return check_method(opt, ast);
}
// If the given tree is a TK_NONE expand it to a source None
static void expand_none(ast_t* ast, bool is_scope)
{
if(ast_id(ast) != TK_NONE)
return;
if(is_scope)
ast_scope(ast);
ast_setid(ast, TK_SEQ);
BUILD(ref, ast, NODE(TK_REFERENCE, ID("None")));
ast_add(ast, ref);
}
static ast_result_t sugar_return(pass_opt_t* opt, ast_t* ast)
{
ast_t* return_value = ast_child(ast);
if((ast_id(ast) == TK_RETURN) && (ast_id(opt->check.frame->method) == TK_NEW))
{
pony_assert(ast_id(return_value) == TK_NONE);
ast_setid(return_value, TK_THIS);
} else {
expand_none(return_value, false);
}
return AST_OK;
}
static ast_result_t sugar_else(ast_t* ast, size_t index)
{
ast_t* else_clause = ast_childidx(ast, index);
expand_none(else_clause, true);
return AST_OK;
}
static ast_result_t sugar_try(ast_t* ast)
{
AST_GET_CHILDREN(ast, ignore, else_clause, then_clause);
if(ast_id(else_clause) == TK_NONE && ast_id(then_clause) != TK_NONE)
// Then without else means we don't require a throwable in the try block
ast_setid(ast, TK_TRY_NO_CHECK);
expand_none(else_clause, true);
expand_none(then_clause, true);
return AST_OK;
}
static ast_result_t sugar_for(pass_opt_t* opt, ast_t** astp)
{
AST_EXTRACT_CHILDREN(*astp, for_idseq, for_iter, for_body, for_else);
ast_t* annotation = ast_consumeannotation(*astp);
expand_none(for_else, true);
const char* iter_name = package_hygienic_id(&opt->check);
BUILD(try_next, for_iter,
NODE(TK_TRY_NO_CHECK,
NODE(TK_SEQ, AST_SCOPE
NODE(TK_CALL,
NODE(TK_DOT, NODE(TK_REFERENCE, ID(iter_name)) ID("next"))
NONE
NONE
NODE(TK_QUESTION)))
NODE(TK_SEQ, AST_SCOPE
NODE(TK_BREAK, NONE))
NONE));
sugar_try(try_next);
REPLACE(astp,
NODE(TK_SEQ,
NODE(TK_ASSIGN,
NODE(TK_LET, NICE_ID(iter_name, "for loop iterator") NONE)
TREE(for_iter))
NODE(TK_WHILE, AST_SCOPE
ANNOTATE(annotation)
NODE(TK_SEQ,
NODE_ERROR_AT(TK_CALL, for_iter,
NODE(TK_DOT, NODE(TK_REFERENCE, ID(iter_name)) ID("has_next"))
NONE
NONE
NONE))
NODE(TK_SEQ, AST_SCOPE
NODE_ERROR_AT(TK_ASSIGN, for_idseq,
TREE(for_idseq)
TREE(try_next))
TREE(for_body))
TREE(for_else))));
return AST_OK;
}
static bool build_with_dispose(pass_opt_t* opt, ast_t* dispose_clause, ast_t* idseq)
{
pony_assert(dispose_clause != NULL);
pony_assert(idseq != NULL);
if(ast_id(idseq) == TK_LET)
{
// Just a single variable
ast_t* id = ast_child(idseq);
pony_assert(id != NULL);
// Don't cares aren't allowed
if(is_name_dontcare(ast_name(id))) {
ast_error(opt->check.errors, id, "_ isn't allowed for a variable in a with block");
return false;
}
BUILD(dispose, idseq,
NODE(TK_CALL,
NODE(TK_DOT, NODE(TK_REFERENCE, TREE(id)) ID("dispose"))
NONE
NONE
NONE));
ast_add(dispose_clause, dispose);
return true;
}
// We have a list of variables
pony_assert(ast_id(idseq) == TK_TUPLE);
for(ast_t* p = ast_child(idseq); p != NULL; p = ast_sibling(p))
{
pony_assert(ast_id(p) == TK_SEQ);
ast_t* let = ast_child(p);
return build_with_dispose(opt, dispose_clause, let);
}
return true;
}
static ast_result_t sugar_with(pass_opt_t* opt, ast_t** astp)
{
AST_EXTRACT_CHILDREN(*astp, withexpr, body);
ast_t* main_annotation = ast_consumeannotation(*astp);
// First build a skeleton disposing block without the "with" variables
BUILD(replace, *astp,
NODE(TK_SEQ,
NODE(TK_DISPOSING_BLOCK,
ANNOTATE(main_annotation)
NODE(TK_SEQ, AST_SCOPE
TREE(body))
NODE(TK_SEQ, AST_SCOPE))));
ast_t* dblock = ast_child(replace);
AST_GET_CHILDREN(dblock, dbody, dexit);
// Add the "with" variables from each with element
for(ast_t* p = ast_child(withexpr); p != NULL; p = ast_sibling(p))
{
pony_assert(ast_id(p) == TK_SEQ);
AST_GET_CHILDREN(p, idseq, init);
const char* init_name = package_hygienic_id(&opt->check);
BUILD(assign, idseq,
NODE(TK_ASSIGN,
NODE(TK_LET, ID(init_name) NONE)
TREE(init)));
BUILD(local, idseq,
NODE(TK_ASSIGN,
TREE(idseq)
NODE(TK_REFERENCE, ID(init_name))));
ast_add(replace, assign);
ast_add(dbody, local);
if (!build_with_dispose(opt, dexit, idseq))
return AST_ERROR;
ast_add(dexit, local);
}
ast_replace(astp, replace);
return AST_OK;
}
// Find all captures in the given pattern.
static bool sugar_match_capture(pass_opt_t* opt, ast_t* pattern)
{
switch(ast_id(pattern))
{
case TK_VAR:
ast_error(opt->check.errors, pattern,
"match captures may not be declared with `var`, use `let`");
return false;
case TK_LET:
{
AST_GET_CHILDREN(pattern, id, capture_type);
if(ast_id(capture_type) == TK_NONE)
{
ast_error(opt->check.errors, pattern,
"capture types cannot be inferred, please specify type of %s",
ast_name(id));
return false;
}
// Disallow capturing tuples.
if(ast_id(capture_type) == TK_TUPLETYPE)
{
ast_error(opt->check.errors, capture_type,
"can't capture a tuple, change this into a tuple of capture "
"expressions");
return false;
}
// Change this to a capture.
ast_setid(pattern, TK_MATCH_CAPTURE);
return true;
}
case TK_TUPLE:
{
// Check all tuple elements.
bool r = true;
for(ast_t* p = ast_child(pattern); p != NULL; p = ast_sibling(p))
{
if(!sugar_match_capture(opt, p))
r = false;
}
return r;
}
case TK_SEQ:
// Captures in a sequence must be the only element.
if(ast_childcount(pattern) != 1)
return true;
return sugar_match_capture(opt, ast_child(pattern));
default:
// Anything else isn't a capture.
return true;
}
}
static ast_result_t sugar_case(pass_opt_t* opt, ast_t* ast)
{
ast_result_t r = AST_OK;
AST_GET_CHILDREN(ast, pattern, guard, body);
if(!sugar_match_capture(opt, pattern))
r = AST_ERROR;
if(ast_id(body) != TK_NONE)
return r;
// We have no body, take a copy of the next case with a body
ast_t* next = ast;
ast_t* next_body = body;
while(ast_id(next_body) == TK_NONE)
{
next = ast_sibling(next);
pony_assert(next != NULL);
pony_assert(ast_id(next) == TK_CASE);
next_body = ast_childidx(next, 2);
}
ast_replace(&body, next_body);
return r;
}
static ast_result_t sugar_update(ast_t** astp)
{
ast_t* ast = *astp;
pony_assert(ast_id(ast) == TK_ASSIGN);
AST_GET_CHILDREN(ast, call, value);
if(ast_id(call) != TK_CALL)
return AST_OK;
// We are of the form: x(y) = z
// Replace us with: x.update(y where value = z)
AST_EXTRACT_CHILDREN(call, expr, positional, named, question);
// If there are no named arguments yet, named will be a TK_NONE.
ast_setid(named, TK_NAMEDARGS);
// Build a new namedarg.
BUILD(namedarg, ast,
NODE(TK_UPDATEARG,
ID("value")
NODE(TK_SEQ, TREE(value))));
// Append the named arg to our existing list.
ast_append(named, namedarg);
// Replace with the update call.
REPLACE(astp,
NODE(TK_CALL,
NODE(TK_DOT, TREE(expr) ID("update"))
TREE(positional)
TREE(named)
TREE(question)));
return AST_OK;
}
static ast_result_t sugar_as(pass_opt_t* opt, ast_t** astp)
{
(void)opt;
ast_t* ast = *astp;
pony_assert(ast_id(ast) == TK_AS);
AST_GET_CHILDREN(ast, expr, type);
if(ast_id(type) == TK_TUPLETYPE)
{
BUILD(new_type, type, NODE(TK_TUPLETYPE));
for(ast_t* p = ast_child(type); p != NULL; p = ast_sibling(p))
{
if(ast_id(p) == TK_NOMINAL &&
is_name_dontcare(ast_name(ast_childidx(p, 1))))
{
BUILD(dontcare, new_type, NODE(TK_DONTCARETYPE));
ast_append(new_type, dontcare);
}
else
ast_append(new_type, p);
}
REPLACE(astp,
NODE(TK_AS, TREE(expr) TREE(new_type)));
}
return AST_OK;
}
static ast_result_t sugar_binop(ast_t** astp, const char* fn_name, const char* fn_name_partial)
{
AST_GET_CHILDREN(*astp, left, right, question);
ast_t* positional = ast_from(right, TK_POSITIONALARGS);
if(ast_id(right) == TK_TUPLE)
{
ast_t* value = ast_child(right);
while(value != NULL)
{
BUILD(arg, right, NODE(TK_SEQ, TREE(value)));
ast_append(positional, arg);
value = ast_sibling(value);
}
} else {
BUILD(arg, right, NODE(TK_SEQ, TREE(right)));
ast_add(positional, arg);
}
const char* name = fn_name;
if(fn_name_partial != NULL && ast_id(question) == TK_QUESTION)
name = fn_name_partial;
REPLACE(astp,
NODE(TK_CALL,
NODE(TK_DOT, TREE(left) ID(name))
TREE(positional)
NONE
TREE(question)));
return AST_OK;
}
static ast_result_t sugar_unop(ast_t** astp, const char* fn_name)
{
AST_GET_CHILDREN(*astp, expr);
REPLACE(astp,
NODE(TK_CALL,
NODE(TK_DOT, TREE(expr) ID(fn_name))
NONE
NONE
NONE));
return AST_OK;
}
static ast_result_t sugar_ffi(pass_opt_t* opt, ast_t* ast)
{
AST_GET_CHILDREN(ast, id, typeargs, args, named_args);
const char* name = ast_name(id);
size_t len = ast_name_len(id);
// Check for \0 in ffi name (it may be a string literal)
if(memchr(name, '\0', len) != NULL)
{
ast_error(opt->check.errors, ast,
"FFI function names cannot include null characters");
return AST_ERROR;
}
// Prefix '@' to the name
char* new_name = (char*)ponyint_pool_alloc_size(len + 2);
new_name[0] = '@';
memcpy(new_name + 1, name, len);
new_name[len + 1] = '\0';
ast_t* new_id = ast_from_string(id, stringtab_consume(new_name, len + 2));
ast_replace(&id, new_id);
return AST_OK;
}
static ast_result_t sugar_ifdef(pass_opt_t* opt, ast_t* ast)
{
pony_assert(opt != NULL);
pony_assert(ast != NULL);
AST_GET_CHILDREN(ast, cond, then_block, else_block, else_cond);
// Combine parent ifdef condition with ours.
ast_t* parent_ifdef_cond = opt->check.frame->ifdef_cond;
if(parent_ifdef_cond != NULL)
{
// We have a parent ifdef, combine its condition with ours.
pony_assert(ast_id(ast_parent(parent_ifdef_cond)) == TK_IFDEF);
REPLACE(&else_cond,
NODE(TK_AND,
TREE(parent_ifdef_cond)
NODE(TK_NOT, TREE(cond))
NODE(TK_NONE)));
REPLACE(&cond,
NODE(TK_AND,
TREE(parent_ifdef_cond)
TREE(cond)
NODE(TK_NONE)));
}
else
{
// Make else condition for our children to use.
REPLACE(&else_cond, NODE(TK_NOT, TREE(cond)));
}
// Normalise condition so and, or and not nodes aren't sugared to function
// calls.
if(!ifdef_cond_normalise(&cond, opt))
{
ast_error(opt->check.errors, ast, "ifdef condition will never be true");
return AST_ERROR;
}
if(!ifdef_cond_normalise(&else_cond, opt))
{
ast_error(opt->check.errors, ast, "ifdef condition is always true");
return AST_ERROR;
}
return sugar_else(ast, 2);
}
static ast_result_t sugar_use(pass_opt_t* opt, ast_t* ast)
{
pony_assert(ast != NULL);
// Normalise condition so and, or and not nodes aren't sugared to function
// calls.
ast_t* guard = ast_childidx(ast, 2);
if(!ifdef_cond_normalise(&guard, opt))
{
ast_error(opt->check.errors, ast, "use guard condition will never be true");
return AST_ERROR;
}
return AST_OK;
}
static ast_result_t sugar_semi(pass_opt_t* options, ast_t** astp)
{
ast_t* ast = *astp;
pony_assert(ast_id(ast) == TK_SEMI);
// Semis are pointless, discard them
pony_assert(ast_child(ast) == NULL);
*astp = ast_sibling(ast);
ast_remove(ast);
// Since we've effectively replaced ast with its successor we need to process
// that too
return pass_sugar(astp, options);
}
static ast_result_t sugar_lambdatype(pass_opt_t* opt, ast_t** astp)
{
pony_assert(astp != NULL);
ast_t* ast = *astp;
pony_assert(ast != NULL);
AST_EXTRACT_CHILDREN(ast, apply_cap, apply_name, apply_t_params, params,
ret_type, error, interface_cap, ephemeral);
bool bare = ast_id(ast) == TK_BARELAMBDATYPE;
if(bare)
{
ast_setid(apply_cap, TK_AT);
ast_setid(interface_cap, TK_VAL);
}
const char* i_name = package_hygienic_id(&opt->check);
ast_t* interface_t_params;
ast_t* t_args;
collect_type_params(ast, NULL, &t_args);
// We will replace {..} with $0