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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
|
open Common;;
open Token;;
open Parser;;
(* NB: pexps (parser-expressions) are only used transiently during
* parsing, static-evaluation and syntax-expansion. They're desugared
* into the general "item" AST and/or evaluated as part of the
* outermost "cexp" expressions. Expressions that can show up in source
* correspond to this loose grammar and have a wide-ish flexibility in
* *theoretical* composition; only subsets of those compositions are
* legal in various AST contexts.
*
* Desugaring on the fly is unfortunately complicated enough to require
* -- or at least "make much more convenient" -- this two-pass
* routine.
*)
(* Pexp grammar. Includes names, idents, types, constrs, binops and unops,
etc. *)
let parse_ident (ps:pstate) : Ast.ident =
match peek ps with
IDENT id -> (bump ps; id)
(* Decay IDX tokens to identifiers if they occur ousdide name paths. *)
| IDX i -> (bump ps; string_of_tok (IDX i))
| _ -> raise (unexpected ps)
;;
(* Enforces the restricted pexp grammar when applicable (e.g. after "bind") *)
let check_rstr_start (ps:pstate) : 'a =
if (ps.pstate_rstr) then
match peek ps with
IDENT _ | LPAREN -> ()
| _ -> raise (unexpected ps)
;;
let rec parse_name_component (ps:pstate) : Ast.name_component =
match peek ps with
IDENT id ->
(bump ps;
match peek ps with
LBRACKET ->
let tys =
ctxt "name_component: apply"
(bracketed_one_or_more LBRACKET RBRACKET
(Some COMMA) parse_ty) ps
in
Ast.COMP_app (id, tys)
| _ -> Ast.COMP_ident id)
| IDX i ->
bump ps;
Ast.COMP_idx i
| _ -> raise (unexpected ps)
and parse_name_base (ps:pstate) : Ast.name_base =
match peek ps with
IDENT i ->
(bump ps;
match peek ps with
LBRACKET ->
let tys =
ctxt "name_base: apply"
(bracketed_one_or_more LBRACKET RBRACKET
(Some COMMA) parse_ty) ps
in
Ast.BASE_app (i, tys)
| _ -> Ast.BASE_ident i)
| _ -> raise (unexpected ps)
and parse_name_ext (ps:pstate) (base:Ast.name) : Ast.name =
match peek ps with
DOT ->
bump ps;
let comps = one_or_more DOT parse_name_component ps in
Array.fold_left (fun x y -> Ast.NAME_ext (x, y)) base comps
| _ -> base
and parse_name (ps:pstate) : Ast.name =
let base = Ast.NAME_base (parse_name_base ps) in
let name = parse_name_ext ps base in
if Ast.sane_name name
then name
else raise (err "malformed name" ps)
and parse_carg_base (ps:pstate) : Ast.carg_base =
match peek ps with
STAR -> bump ps; Ast.BASE_formal
| _ -> Ast.BASE_named (parse_name_base ps)
and parse_carg (ps:pstate) : Ast.carg =
match peek ps with
IDENT _ | STAR ->
begin
let base = Ast.CARG_base (parse_carg_base ps) in
let path =
match peek ps with
DOT ->
bump ps;
let comps = one_or_more DOT parse_name_component ps in
Array.fold_left
(fun x y -> Ast.CARG_ext (x, y)) base comps
| _ -> base
in
Ast.CARG_path path
end
| _ ->
Ast.CARG_lit (parse_lit ps)
and parse_constraint (ps:pstate) : Ast.constr =
match peek ps with
(*
* NB: A constraint *looks* a lot like an EXPR_call, but is restricted
* syntactically: the constraint name needs to be a name (not an lval)
* and the constraint args all need to be cargs, which are similar to
* names but can begin with the 'formal' base anchor '*'.
*)
IDENT _ ->
let n = ctxt "constraint: name" parse_name ps in
let args = ctxt "constraint: args"
(bracketed_zero_or_more
LPAREN RPAREN (Some COMMA)
parse_carg) ps
in
{ Ast.constr_name = n;
Ast.constr_args = args }
| _ -> raise (unexpected ps)
and parse_constrs (ps:pstate) : Ast.constrs =
ctxt "state: constraints" (one_or_more COMMA parse_constraint) ps
and parse_optional_trailing_constrs (ps:pstate) : Ast.constrs =
match peek ps with
COLON -> (bump ps; parse_constrs ps)
| _ -> [| |]
and parse_opacity (ps:pstate) : Ast.opacity =
match peek ps with
ABS -> bump ps; Ast.OPA_abstract
| _ -> Ast.OPA_transparent
and parse_layer (ps:pstate) : Ast.layer =
match peek ps with
STATE -> bump ps; Ast.LAYER_state
| GC -> bump ps; Ast.LAYER_gc
| _ -> Ast.LAYER_value
and parse_effect (ps:pstate) : Ast.effect =
match peek ps with
IMPURE -> bump ps; Ast.EFF_impure
| UNSAFE -> bump ps; Ast.EFF_unsafe
| _ -> Ast.EFF_pure
and parse_mutability (ps:pstate) : Ast.mutability =
match peek ps with
MUTABLE ->
begin
(* HACK: ignore "mutable?" *)
bump ps;
match peek ps with
QUES -> bump ps; Ast.MUT_immutable
| _ -> Ast.MUT_mutable
end
| _ -> Ast.MUT_immutable
and parse_ty_fn
(effect:Ast.effect)
(ps:pstate)
: (Ast.ty_fn * Ast.ident option) =
match peek ps with
FN | ITER ->
let is_iter = (peek ps) = ITER in
bump ps;
let ident =
match peek ps with
IDENT i -> bump ps; Some i
| _ -> None
in
let in_slots =
match peek ps with
_ ->
bracketed_zero_or_more LPAREN RPAREN (Some COMMA)
(parse_slot_and_optional_ignored_ident true) ps
in
let out_slot =
match peek ps with
RARROW -> (bump ps; parse_slot false ps)
| _ -> slot_nil
in
let constrs = parse_optional_trailing_constrs ps in
let tsig = { Ast.sig_input_slots = in_slots;
Ast.sig_input_constrs = constrs;
Ast.sig_output_slot = out_slot; }
in
let taux = { Ast.fn_effect = effect;
Ast.fn_is_iter = is_iter; }
in
let tfn = (tsig, taux) in
(tfn, ident)
| _ -> raise (unexpected ps)
and check_dup_rec_labels ps labels =
arr_check_dups labels
(fun l _ ->
raise (err (Printf.sprintf
"duplicate record label: %s" l) ps));
and parse_atomic_ty (ps:pstate) : Ast.ty =
match peek ps with
BOOL ->
bump ps;
Ast.TY_bool
| INT ->
bump ps;
Ast.TY_int
| UINT ->
bump ps;
Ast.TY_uint
| CHAR ->
bump ps;
Ast.TY_char
| STR ->
bump ps;
Ast.TY_str
| ANY ->
bump ps;
Ast.TY_any
| TASK ->
bump ps;
Ast.TY_task
| CHAN ->
bump ps;
Ast.TY_chan (bracketed LBRACKET RBRACKET parse_ty ps)
| PORT ->
bump ps;
Ast.TY_port (bracketed LBRACKET RBRACKET parse_ty ps)
| VEC ->
bump ps;
Ast.TY_vec (bracketed LBRACKET RBRACKET parse_ty ps)
| IDENT _ -> Ast.TY_named (parse_name ps)
| REC ->
bump ps;
let parse_rec_entry ps =
let (ty, ident) = parse_ty_and_ident ps in
(ident, ty)
in
let entries = paren_comma_list parse_rec_entry ps in
let labels = Array.map (fun (l, _) -> l) entries in
begin
check_dup_rec_labels ps labels;
Ast.TY_rec entries
end
| TUP ->
bump ps;
let tys = paren_comma_list parse_ty ps in
Ast.TY_tup tys
| MACH m ->
bump ps;
Ast.TY_mach m
| STATE | GC | IMPURE | UNSAFE | OBJ | FN | ITER ->
let layer = parse_layer ps in
let effect = parse_effect ps in
begin
match peek ps with
OBJ ->
bump ps;
if effect <> Ast.EFF_pure
then raise (err "effect specified for obj" ps);
let methods = Hashtbl.create 0 in
let parse_method ps =
let effect = parse_effect ps in
let (tfn, ident) = parse_ty_fn effect ps in
expect ps SEMI;
match ident with
None ->
raise (err (Printf.sprintf
"missing method identifier") ps)
| Some i -> htab_put methods i tfn
in
ignore (bracketed_zero_or_more LBRACE RBRACE
None parse_method ps);
Ast.TY_obj (layer, methods)
| FN | ITER ->
if layer <> Ast.LAYER_value
then raise (err "layer specified for fn or iter" ps);
Ast.TY_fn (fst (parse_ty_fn effect ps))
| _ -> raise (unexpected ps)
end
| AT ->
bump ps;
Ast.TY_box (parse_ty ps)
| MUTABLE ->
bump ps;
begin
(* HACK: ignore "mutable?" *)
match peek ps with
QUES -> bump ps; parse_ty ps
| _ -> Ast.TY_mutable (parse_ty ps)
end
| LPAREN ->
begin
bump ps;
match peek ps with
RPAREN ->
bump ps;
Ast.TY_nil
| _ ->
let t = parse_ty ps in
expect ps RPAREN;
t
end
| _ -> raise (unexpected ps)
and flag (ps:pstate) (tok:token) : bool =
if peek ps = tok
then (bump ps; true)
else false
and parse_slot (aliases_ok:bool) (ps:pstate) : Ast.slot =
let mode =
match (peek ps, aliases_ok) with
(AND, true) -> bump ps; Ast.MODE_alias
| (AND, false) -> raise (err "alias slot in prohibited context" ps)
| _ -> Ast.MODE_local
in
let ty = parse_ty ps in
{ Ast.slot_mode = mode;
Ast.slot_ty = Some ty }
and parse_slot_and_ident
(aliases_ok:bool)
(ps:pstate)
: (Ast.slot * Ast.ident) =
let slot = ctxt "slot and ident: slot" (parse_slot aliases_ok) ps in
let ident = ctxt "slot and ident: ident" parse_ident ps in
(slot, ident)
and parse_ty_and_ident
(ps:pstate)
: (Ast.ty * Ast.ident) =
let ty = ctxt "ty and ident: ty" parse_ty ps in
let ident = ctxt "ty and ident: ident" parse_ident ps in
(ty, ident)
and parse_slot_and_optional_ignored_ident
(aliases_ok:bool)
(ps:pstate)
: Ast.slot =
let slot = parse_slot aliases_ok ps in
begin
match peek ps with
IDENT _ -> bump ps
| _ -> ()
end;
slot
and parse_identified_slot
(aliases_ok:bool)
(ps:pstate)
: Ast.slot identified =
let apos = lexpos ps in
let slot = parse_slot aliases_ok ps in
let bpos = lexpos ps in
span ps apos bpos slot
and parse_constrained_ty (ps:pstate) : Ast.ty =
let base = ctxt "ty: base" parse_atomic_ty ps in
match peek ps with
COLON ->
bump ps;
let constrs = ctxt "ty: constrs" parse_constrs ps in
Ast.TY_constrained (base, constrs)
| _ -> base
and parse_ty (ps:pstate) : Ast.ty =
parse_constrained_ty ps
and parse_rec_input (ps:pstate)
: (Ast.ident * Ast.mutability * Ast.pexp) =
let mutability = parse_mutability ps in
let lab = (ctxt "rec input: label" parse_ident ps) in
match peek ps with
EQ ->
bump ps;
let pexp = ctxt "rec input: expr" parse_pexp ps in
(lab, mutability, pexp)
| _ -> raise (unexpected ps)
and parse_rec_body (ps:pstate) : Ast.pexp' =
begin
expect ps LPAREN;
match peek ps with
RPAREN -> Ast.PEXP_rec ([||], None)
| WITH -> raise (err "empty record extension" ps)
| _ ->
let inputs = one_or_more COMMA parse_rec_input ps in
let labels = Array.map (fun (l, _, _) -> l) inputs in
begin
check_dup_rec_labels ps labels;
match peek ps with
RPAREN -> (bump ps; Ast.PEXP_rec (inputs, None))
| WITH ->
begin
bump ps;
let base =
ctxt "rec input: extension base"
parse_pexp ps
in
expect ps RPAREN;
Ast.PEXP_rec (inputs, Some base)
end
| _ -> raise (err "expected 'with' or ')'" ps)
end
end
and parse_lit (ps:pstate) : Ast.lit =
match peek ps with
LIT_INT i -> (bump ps; Ast.LIT_int i)
| LIT_UINT i -> (bump ps; Ast.LIT_uint i)
| LIT_MACH_INT (tm, i) -> (bump ps; Ast.LIT_mach_int (tm, i))
| LIT_CHAR c -> (bump ps; Ast.LIT_char c)
| LIT_BOOL b -> (bump ps; Ast.LIT_bool b)
| _ -> raise (unexpected ps)
and parse_bottom_pexp (ps:pstate) : Ast.pexp =
check_rstr_start ps;
let apos = lexpos ps in
match peek ps with
AT ->
bump ps;
let mutability = parse_mutability ps in
let inner = parse_pexp ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_box (mutability, inner))
| TUP ->
bump ps;
let pexps =
ctxt "paren pexps(s)" (rstr false parse_mutable_and_pexp_list) ps
in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_tup pexps)
| REC ->
bump ps;
let body = ctxt "rec pexp: rec body" parse_rec_body ps in
let bpos = lexpos ps in
span ps apos bpos body
| VEC ->
bump ps;
let pexps =
ctxt "paren pexps(s)" (rstr false parse_mutable_and_pexp_list) ps
in
let mutability = ref Ast.MUT_immutable in
let pexps =
Array.mapi
begin
fun i (mut, e) ->
if i = 0
then
mutability := mut
else
if mut <> Ast.MUT_immutable
then
raise
(err "'mutable' keyword after first vec element" ps);
e
end
pexps
in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_vec (!mutability, pexps))
| LIT_STR s ->
bump ps;
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_str s)
| PORT ->
begin
bump ps;
expect ps LPAREN;
expect ps RPAREN;
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_port)
end
| CHAN ->
begin
bump ps;
let port =
match peek ps with
LPAREN ->
begin
bump ps;
match peek ps with
RPAREN -> (bump ps; None)
| _ ->
let lv = parse_pexp ps in
expect ps RPAREN;
Some lv
end
| _ -> raise (unexpected ps)
in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_chan port)
end
| SPAWN ->
bump ps;
let domain =
match peek ps with
THREAD -> bump ps; Ast.DOMAIN_thread
| _ -> Ast.DOMAIN_local
in
(* Spawns either have an explicit literal string for the spawned
task's name, or the task is named as the entry call
expression. *)
let explicit_name =
match peek ps with
LIT_STR s -> bump ps; Some s
| _ -> None
in
let pexp =
ctxt "spawn [domain] [name] pexp: init call" parse_pexp ps
in
let bpos = lexpos ps in
let name =
match explicit_name with
Some s -> s
(* FIXME: string_of_span returns a string like
"./driver.rs:10:16 - 11:52", not the actual text at those
characters *)
| None -> Session.string_of_span { lo = apos; hi = bpos }
in
span ps apos bpos (Ast.PEXP_spawn (domain, name, pexp))
| BIND ->
let apos = lexpos ps in
begin
bump ps;
let pexp = ctxt "bind pexp: function" (rstr true parse_pexp) ps in
let args =
ctxt "bind args"
(paren_comma_list parse_bind_arg) ps
in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_bind (pexp, args))
end
| IDENT i ->
begin
bump ps;
match peek ps with
LBRACKET ->
begin
let tys =
ctxt "apply-type expr"
(bracketed_one_or_more LBRACKET RBRACKET
(Some COMMA) parse_ty) ps
in
let bpos = lexpos ps in
span ps apos bpos
(Ast.PEXP_lval (Ast.PLVAL_base (Ast.BASE_app (i, tys))))
end
| _ ->
begin
let bpos = lexpos ps in
span ps apos bpos
(Ast.PEXP_lval (Ast.PLVAL_base (Ast.BASE_ident i)))
end
end
| STAR ->
bump ps;
let inner = parse_pexp ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_lval (Ast.PLVAL_ext_deref inner))
| POUND ->
bump ps;
let name = parse_name ps in
let args =
match peek ps with
LPAREN ->
parse_pexp_list ps
| _ -> [| |]
in
let str =
match peek ps with
LBRACE ->
begin
bump_bracequote ps;
match peek ps with
BRACEQUOTE s -> bump ps; Some s
| _ -> raise (unexpected ps)
end
| _ -> None
in
let bpos = lexpos ps in
span ps apos bpos
(Ast.PEXP_custom (name, args, str))
| LPAREN ->
begin
bump ps;
match peek ps with
RPAREN ->
bump ps;
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_lit Ast.LIT_nil)
| _ ->
let pexp = parse_pexp ps in
expect ps RPAREN;
pexp
end
| _ ->
let lit = parse_lit ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_lit lit)
and parse_bind_arg (ps:pstate) : Ast.pexp option =
match peek ps with
UNDERSCORE -> (bump ps; None)
| _ -> Some (parse_pexp ps)
and parse_ext_pexp (ps:pstate) (pexp:Ast.pexp) : Ast.pexp =
let apos = lexpos ps in
match peek ps with
LPAREN ->
if ps.pstate_rstr
then pexp
else
let args = parse_pexp_list ps in
let bpos = lexpos ps in
let ext = span ps apos bpos (Ast.PEXP_call (pexp, args)) in
parse_ext_pexp ps ext
| DOT ->
begin
bump ps;
let ext =
match peek ps with
LPAREN ->
bump ps;
let rhs = rstr false parse_pexp ps in
expect ps RPAREN;
let bpos = lexpos ps in
span ps apos bpos
(Ast.PEXP_lval (Ast.PLVAL_ext_pexp (pexp, rhs)))
| _ ->
let rhs = parse_name_component ps in
let bpos = lexpos ps in
span ps apos bpos
(Ast.PEXP_lval (Ast.PLVAL_ext_name (pexp, rhs)))
in
parse_ext_pexp ps ext
end
| _ -> pexp
and parse_negation_pexp (ps:pstate) : Ast.pexp =
let apos = lexpos ps in
match peek ps with
NOT ->
bump ps;
let rhs = ctxt "negation pexp" parse_negation_pexp ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_unop (Ast.UNOP_not, rhs))
| TILDE ->
bump ps;
let rhs = ctxt "negation pexp" parse_negation_pexp ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_unop (Ast.UNOP_bitnot, rhs))
| MINUS ->
bump ps;
let rhs = ctxt "negation pexp" parse_negation_pexp ps in
let bpos = lexpos ps in
span ps apos bpos (Ast.PEXP_unop (Ast.UNOP_neg, rhs))
| _ ->
let lhs = parse_bottom_pexp ps in
parse_ext_pexp ps lhs
(* Binops are all left-associative, *)
(* so we factor out some of the parsing code here. *)
and binop_build
(ps:pstate)
(name:string)
(apos:pos)
(rhs_parse_fn:pstate -> Ast.pexp)
(lhs:Ast.pexp)
(step_fn:Ast.pexp -> Ast.pexp)
(op:Ast.binop)
: Ast.pexp =
bump ps;
let rhs = (ctxt (name ^ " rhs") rhs_parse_fn ps) in
let bpos = lexpos ps in
let node = span ps apos bpos (Ast.PEXP_binop (op, lhs, rhs)) in
step_fn node
and parse_factor_pexp (ps:pstate) : Ast.pexp =
let name = "factor pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_negation_pexp ps in
let build = binop_build ps name apos parse_negation_pexp in
let rec step accum =
match peek ps with
STAR -> build accum step Ast.BINOP_mul
| SLASH -> build accum step Ast.BINOP_div
| PERCENT -> build accum step Ast.BINOP_mod
| _ -> accum
in
step lhs
and parse_term_pexp (ps:pstate) : Ast.pexp =
let name = "term pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_factor_pexp ps in
let build = binop_build ps name apos parse_factor_pexp in
let rec step accum =
match peek ps with
PLUS -> build accum step Ast.BINOP_add
| MINUS -> build accum step Ast.BINOP_sub
| _ -> accum
in
step lhs
and parse_shift_pexp (ps:pstate) : Ast.pexp =
let name = "shift pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_term_pexp ps in
let build = binop_build ps name apos parse_term_pexp in
let rec step accum =
match peek ps with
LSL -> build accum step Ast.BINOP_lsl
| LSR -> build accum step Ast.BINOP_lsr
| ASR -> build accum step Ast.BINOP_asr
| _ -> accum
in
step lhs
and parse_and_pexp (ps:pstate) : Ast.pexp =
let name = "and pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_shift_pexp ps in
let build = binop_build ps name apos parse_shift_pexp in
let rec step accum =
match peek ps with
AND -> build accum step Ast.BINOP_and
| _ -> accum
in
step lhs
and parse_xor_pexp (ps:pstate) : Ast.pexp =
let name = "xor pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_and_pexp ps in
let build = binop_build ps name apos parse_and_pexp in
let rec step accum =
match peek ps with
CARET -> build accum step Ast.BINOP_xor
| _ -> accum
in
step lhs
and parse_or_pexp (ps:pstate) : Ast.pexp =
let name = "or pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_xor_pexp ps in
let build = binop_build ps name apos parse_xor_pexp in
let rec step accum =
match peek ps with
OR -> build accum step Ast.BINOP_or
| _ -> accum
in
step lhs
and parse_as_pexp (ps:pstate) : Ast.pexp =
let apos = lexpos ps in
let pexp = ctxt "as pexp" parse_or_pexp ps in
let rec step accum =
match peek ps with
AS ->
bump ps;
let tapos = lexpos ps in
let t = parse_ty ps in
let bpos = lexpos ps in
let t = span ps tapos bpos t in
let node =
span ps apos bpos
(Ast.PEXP_unop ((Ast.UNOP_cast t), accum))
in
step node
| _ -> accum
in
step pexp
and parse_relational_pexp (ps:pstate) : Ast.pexp =
let name = "relational pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_as_pexp ps in
let build = binop_build ps name apos parse_as_pexp in
let rec step accum =
match peek ps with
LT -> build accum step Ast.BINOP_lt
| LE -> build accum step Ast.BINOP_le
| GE -> build accum step Ast.BINOP_ge
| GT -> build accum step Ast.BINOP_gt
| _ -> accum
in
step lhs
and parse_equality_pexp (ps:pstate) : Ast.pexp =
let name = "equality pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_relational_pexp ps in
let build = binop_build ps name apos parse_relational_pexp in
let rec step accum =
match peek ps with
EQEQ -> build accum step Ast.BINOP_eq
| NE -> build accum step Ast.BINOP_ne
| _ -> accum
in
step lhs
and parse_andand_pexp (ps:pstate) : Ast.pexp =
let name = "andand pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_equality_pexp ps in
let rec step accum =
match peek ps with
ANDAND ->
bump ps;
let rhs = parse_equality_pexp ps in
let bpos = lexpos ps in
let node = span ps apos bpos (Ast.PEXP_lazy_and (accum, rhs)) in
step node
| _ -> accum
in
step lhs
and parse_oror_pexp (ps:pstate) : Ast.pexp =
let name = "oror pexp" in
let apos = lexpos ps in
let lhs = ctxt (name ^ " lhs") parse_andand_pexp ps in
let rec step accum =
match peek ps with
OROR ->
bump ps;
let rhs = parse_andand_pexp ps in
let bpos = lexpos ps in
let node = span ps apos bpos (Ast.PEXP_lazy_or (accum, rhs)) in
step node
| _ -> accum
in
step lhs
and parse_pexp (ps:pstate) : Ast.pexp =
parse_oror_pexp ps
and parse_mutable_and_pexp (ps:pstate) : (Ast.mutability * Ast.pexp) =
let mutability = parse_mutability ps in
(mutability, parse_as_pexp ps)
and parse_pexp_list (ps:pstate) : Ast.pexp array =
match peek ps with
LPAREN ->
bracketed_zero_or_more LPAREN RPAREN (Some COMMA)
(ctxt "pexp list" parse_pexp) ps
| _ -> raise (unexpected ps)
and parse_mutable_and_pexp_list (ps:pstate)
: (Ast.mutability * Ast.pexp) array =
match peek ps with
LPAREN ->
bracketed_zero_or_more LPAREN RPAREN (Some COMMA)
(ctxt "mutable-and-pexp list" parse_mutable_and_pexp) ps
| _ -> raise (unexpected ps)
;;
(*
* Desugarings depend on context:
*
* - If a pexp is used on the RHS of an assignment, it's turned into
* an initialization statement such as STMT_new_rec or such. This
* removes the possibility of initializing into a temp only to
* copy out. If the topmost pexp in such a desugaring is an atom,
* unop or binop, of course, it will still just emit a STMT_copy
* on a primitive expression.
*
* - If a pexp is used in the context where an atom is required, a
* statement declaring a temporary and initializing it with the
* result of the pexp is prepended, and the temporary atom is used.
*)
let rec desugar_lval (ps:pstate) (pexp:Ast.pexp)
: (Ast.stmt array * Ast.lval) =
let s = Hashtbl.find ps.pstate_sess.Session.sess_spans pexp.id in
let (apos, bpos) = (s.lo, s.hi) in
match pexp.node with
Ast.PEXP_lval (Ast.PLVAL_base nb) ->
([||], Ast.LVAL_base (span ps apos bpos nb))
| Ast.PEXP_lval (Ast.PLVAL_ext_name (base_pexp, comp)) ->
let (base_stmts, base_atom) = desugar_expr_atom ps base_pexp in
let base_lval = atom_lval ps base_atom in
(base_stmts, Ast.LVAL_ext (base_lval, Ast.COMP_named comp))
| Ast.PEXP_lval (Ast.PLVAL_ext_pexp (base_pexp, ext_pexp)) ->
let (base_stmts, base_atom) = desugar_expr_atom ps base_pexp in
let (ext_stmts, ext_atom) = desugar_expr_atom ps ext_pexp in
let base_lval = atom_lval ps base_atom in
(Array.append base_stmts ext_stmts,
Ast.LVAL_ext (base_lval, Ast.COMP_atom (clone_atom ps ext_atom)))
| Ast.PEXP_lval (Ast.PLVAL_ext_deref base_pexp) ->
let (base_stmts, base_atom) = desugar_expr_atom ps base_pexp in
let base_lval = atom_lval ps base_atom in
(base_stmts, Ast.LVAL_ext (base_lval, Ast.COMP_deref))
| _ ->
let (stmts, atom) = desugar_expr_atom ps pexp in
(stmts, atom_lval ps atom)
and desugar_expr
(ps:pstate)
(pexp:Ast.pexp)
: (Ast.stmt array * Ast.expr) =
match pexp.node with
Ast.PEXP_unop (op, pe) ->
let (stmts, at) = desugar_expr_atom ps pe in
(stmts, Ast.EXPR_unary (op, at))
| Ast.PEXP_binop (op, lhs, rhs) ->
let (lhs_stmts, lhs_atom) = desugar_expr_atom ps lhs in
let (rhs_stmts, rhs_atom) = desugar_expr_atom ps rhs in
(Array.append lhs_stmts rhs_stmts,
Ast.EXPR_binary (op, lhs_atom, rhs_atom))
| _ ->
let (stmts, at) = desugar_expr_atom ps pexp in
(stmts, Ast.EXPR_atom at)
and desugar_opt_expr_atom
(ps:pstate)
(po:Ast.pexp option)
: (Ast.stmt array * Ast.atom option) =
match po with
None -> ([| |], None)
| Some pexp ->
let (stmts, atom) = desugar_expr_atom ps pexp in
(stmts, Some atom)
and desugar_expr_atom
(ps:pstate)
(pexp:Ast.pexp)
: (Ast.stmt array * Ast.atom) =
let s = Hashtbl.find ps.pstate_sess.Session.sess_spans pexp.id in
let (apos, bpos) = (s.lo, s.hi) in
match pexp.node with
Ast.PEXP_unop _
| Ast.PEXP_binop _
| Ast.PEXP_lazy_or _
| Ast.PEXP_lazy_and _
| Ast.PEXP_rec _
| Ast.PEXP_tup _
| Ast.PEXP_str _
| Ast.PEXP_vec _
| Ast.PEXP_port
| Ast.PEXP_chan _
| Ast.PEXP_call _
| Ast.PEXP_bind _
| Ast.PEXP_spawn _
| Ast.PEXP_custom _
| Ast.PEXP_box _ ->
let (_, tmp, decl_stmt) = build_tmp ps slot_auto apos bpos in
let stmts = desugar_expr_init ps tmp pexp in
(Array.append [| decl_stmt |] stmts,
Ast.ATOM_lval (clone_lval ps tmp))
| Ast.PEXP_lit lit ->
([||], Ast.ATOM_literal (span ps apos bpos lit))
| Ast.PEXP_lval _ ->
let (stmts, lval) = desugar_lval ps pexp in
(stmts, Ast.ATOM_lval lval)
and desugar_expr_atoms
(ps:pstate)
(pexps:Ast.pexp array)
: (Ast.stmt array * Ast.atom array) =
arj1st (Array.map (desugar_expr_atom ps) pexps)
and desugar_opt_expr_atoms
(ps:pstate)
(pexps:Ast.pexp option array)
: (Ast.stmt array * Ast.atom option array) =
arj1st (Array.map (desugar_opt_expr_atom ps) pexps)
and desugar_expr_init
(ps:pstate)
(dst_lval:Ast.lval)
(pexp:Ast.pexp)
: (Ast.stmt array) =
let s = Hashtbl.find ps.pstate_sess.Session.sess_spans pexp.id in
let (apos, bpos) = (s.lo, s.hi) in
(* Helpers. *)
let ss x = span ps apos bpos x in
let cp v = Ast.STMT_copy (clone_lval ps dst_lval, v) in
let aa x y = Array.append x y in
let ac xs = Array.concat xs in
match pexp.node with
Ast.PEXP_lit _
| Ast.PEXP_lval _ ->
let (stmts, atom) = desugar_expr_atom ps pexp in
aa stmts [| ss (cp (Ast.EXPR_atom atom)) |]
| Ast.PEXP_binop (op, lhs, rhs) ->
let (lhs_stmts, lhs_atom) = desugar_expr_atom ps lhs in
let (rhs_stmts, rhs_atom) = desugar_expr_atom ps rhs in
let copy_stmt =
ss (cp (Ast.EXPR_binary (op, lhs_atom, rhs_atom)))
in
ac [ lhs_stmts; rhs_stmts; [| copy_stmt |] ]
(* x = a && b ==> if (a) { x = b; } else { x = false; } *)
| Ast.PEXP_lazy_and (lhs, rhs) ->
let (lhs_stmts, lhs_atom) = desugar_expr_atom ps lhs in
let (rhs_stmts, rhs_atom) = desugar_expr_atom ps rhs in
let sthen =
ss (aa rhs_stmts [| ss (cp (Ast.EXPR_atom rhs_atom)) |])
in
let selse =
ss [| ss (cp (Ast.EXPR_atom
(Ast.ATOM_literal (ss (Ast.LIT_bool false))))) |]
in
let sif =
ss (Ast.STMT_if { Ast.if_test = Ast.EXPR_atom lhs_atom;
Ast.if_then = sthen;
Ast.if_else = Some selse })
in
aa lhs_stmts [| sif |]
(* x = a || b ==> if (a) { x = true; } else { x = b; } *)
| Ast.PEXP_lazy_or (lhs, rhs) ->
let (lhs_stmts, lhs_atom) = desugar_expr_atom ps lhs in
let (rhs_stmts, rhs_atom) = desugar_expr_atom ps rhs in
let sthen =
ss [| ss (cp (Ast.EXPR_atom
(Ast.ATOM_literal (ss (Ast.LIT_bool true))))) |]
in
let selse =
ss (aa rhs_stmts [| ss (cp (Ast.EXPR_atom rhs_atom)) |])
in
let sif =
ss (Ast.STMT_if { Ast.if_test = Ast.EXPR_atom lhs_atom;
Ast.if_then = sthen;
Ast.if_else = Some selse })
in
aa lhs_stmts [| sif |]
| Ast.PEXP_unop (op, rhs) ->
let (rhs_stmts, rhs_atom) = desugar_expr_atom ps rhs in
let expr = Ast.EXPR_unary (op, rhs_atom) in
let copy_stmt = ss (cp expr) in
aa rhs_stmts [| copy_stmt |]
| Ast.PEXP_call (fn, args) ->
let (fn_stmts, fn_atom) = desugar_expr_atom ps fn in
let (arg_stmts, arg_atoms) = desugar_expr_atoms ps args in
let fn_lval = atom_lval ps fn_atom in
let call_stmt = ss (Ast.STMT_call (dst_lval, fn_lval, arg_atoms)) in
ac [ fn_stmts; arg_stmts; [| call_stmt |] ]
| Ast.PEXP_bind (fn, args) ->
let (fn_stmts, fn_atom) = desugar_expr_atom ps fn in
let (arg_stmts, arg_atoms) = desugar_opt_expr_atoms ps args in
let fn_lval = atom_lval ps fn_atom in
let bind_stmt = ss (Ast.STMT_bind (dst_lval, fn_lval, arg_atoms)) in
ac [ fn_stmts; arg_stmts; [| bind_stmt |] ]
| Ast.PEXP_spawn (domain, name, sub) ->
begin
match sub.node with
Ast.PEXP_call (fn, args) ->
let (fn_stmts, fn_atom) = desugar_expr_atom ps fn in
let (arg_stmts, arg_atoms) = desugar_expr_atoms ps args in
let fn_lval = atom_lval ps fn_atom in
let spawn_stmt =
ss (Ast.STMT_spawn
(dst_lval, domain, name, fn_lval, arg_atoms))
in
ac [ fn_stmts; arg_stmts; [| spawn_stmt |] ]
| _ -> raise (err "non-call spawn" ps)
end
| Ast.PEXP_rec (args, base) ->
let (arg_stmts, entries) =
arj1st
begin
Array.map
begin
fun (ident, mutability, pexp) ->
let (stmts, atom) =
desugar_expr_atom ps pexp
in
(stmts, (ident, mutability, atom))
end
args
end
in
begin
match base with
Some base ->
let (base_stmts, base_lval) = desugar_lval ps base in
let rec_stmt =
ss (Ast.STMT_new_rec
(dst_lval, entries, Some base_lval))
in
ac [ arg_stmts; base_stmts; [| rec_stmt |] ]
| None ->
let rec_stmt =
ss (Ast.STMT_new_rec (dst_lval, entries, None))
in
aa arg_stmts [| rec_stmt |]
end
| Ast.PEXP_tup args ->
let muts = Array.to_list (Array.map fst args) in
let (arg_stmts, arg_atoms) =
desugar_expr_atoms ps (Array.map snd args)
in
let arg_atoms = Array.to_list arg_atoms in
let tup_args = Array.of_list (List.combine muts arg_atoms) in
let stmt = ss (Ast.STMT_new_tup (dst_lval, tup_args)) in
aa arg_stmts [| stmt |]
| Ast.PEXP_str s ->
let stmt = ss (Ast.STMT_new_str (dst_lval, s)) in
[| stmt |]
| Ast.PEXP_vec (mutability, args) ->
let (arg_stmts, arg_atoms) = desugar_expr_atoms ps args in
let stmt =
ss (Ast.STMT_new_vec (dst_lval, mutability, arg_atoms))
in
aa arg_stmts [| stmt |]
| Ast.PEXP_port ->
[| ss (Ast.STMT_new_port dst_lval) |]
| Ast.PEXP_chan pexp_opt ->
let (port_stmts, port_opt) =
match pexp_opt with
None -> ([||], None)
| Some port_pexp ->
begin
let (port_stmts, port_atom) =
desugar_expr_atom ps port_pexp
in
let port_lval = atom_lval ps port_atom in
(port_stmts, Some port_lval)
end
in
let chan_stmt =
ss
(Ast.STMT_new_chan (dst_lval, port_opt))
in
aa port_stmts [| chan_stmt |]
| Ast.PEXP_box (mutability, arg) ->
let (arg_stmts, arg_mode_atom) =
desugar_expr_atom ps arg
in
let stmt =
ss (Ast.STMT_new_box (dst_lval, mutability, arg_mode_atom))
in
aa arg_stmts [| stmt |]
| Ast.PEXP_custom (n, a, b) ->
expand_pexp_custom ps apos bpos dst_lval n a b
(*
* FIXME: This is a crude approximation of the syntax-extension system,
* for purposes of prototyping and/or hard-wiring any extensions we
* wish to use in the bootstrap compiler. The eventual aim is to permit
* loading rust crates to process extensions, but this will likely
* require a rust-based frontend, or an ocaml-FFI-based connection to
* rust crates. At the moment we have neither.
*)
and expand_pexp_custom
(ps:pstate)
(apos:pos)
(bpos:pos)
(dst_lval:Ast.lval)
(name:Ast.name)
(pexp_args:Ast.pexp array)
(body:string option)
: (Ast.stmt array) =
let nstr = Fmt.fmt_to_str Ast.fmt_name name in
match (nstr, (Array.length pexp_args), body) with
("shell", 0, Some cmd) ->
let c = Unix.open_process_in cmd in
let b = Buffer.create 32 in
let rec r _ =
try
Buffer.add_char b (input_char c);
r ()
with
End_of_file ->
ignore (Unix.close_process_in c);
Buffer.contents b
in
[| span ps apos bpos
(Ast.STMT_new_str (dst_lval, r())) |]
| ("fmt", nargs, None) ->
if nargs = 0
then raise (err "malformed #fmt call" ps)
else
begin
match pexp_args.(0).node with
Ast.PEXP_str s ->
let (arg_stmts, args) =
desugar_expr_atoms ps
(Array.sub pexp_args 1 (nargs-1))
in
let pieces = Extfmt.parse_fmt_string s in
let fmt_stmts =
fmt_pieces_to_stmts
ps apos bpos dst_lval pieces args
in
Array.append arg_stmts fmt_stmts
| _ ->
raise (err "malformed #fmt call" ps)
end
| _ ->
raise (err ("unknown syntax extension: " ^ nstr) ps)
and fmt_pieces_to_stmts
(ps:pstate)
(apos:pos)
(bpos:pos)
(dst_lval:Ast.lval)
(pieces:Extfmt.piece array)
(args:Ast.atom array)
: (Ast.stmt array) =
let stmts = Queue.create () in
let make_new_tmp _ =
let (_, tmp, decl_stmt) = build_tmp ps slot_auto apos bpos in
Queue.add decl_stmt stmts;
tmp
in
let make_new_str s =
let tmp = make_new_tmp () in
let init_stmt =
span ps apos bpos (Ast.STMT_new_str (clone_lval ps tmp, s))
in
Queue.add init_stmt stmts;
tmp
in
let make_append dst_lval src_atom =
let stmt =
span ps apos bpos
(Ast.STMT_copy_binop
((clone_lval ps dst_lval), Ast.BINOP_add, src_atom))
in
Queue.add stmt stmts
in
let make_append_lval dst_lval src_lval =
make_append dst_lval (Ast.ATOM_lval (clone_lval ps src_lval))
in
let rec make_lval' path =
match path with
[n] ->
Ast.LVAL_base (span ps apos bpos (Ast.BASE_ident n))
| x :: xs ->
Ast.LVAL_ext (make_lval' xs,
Ast.COMP_named (Ast.COMP_ident x))
| [] -> (bug () "make_lval on empty list in #fmt")
in
let make_lval path = make_lval' (List.rev path) in
let make_call dst path args =
let callee = make_lval path in
let stmt =
span ps apos bpos (Ast.STMT_call (dst, callee, args ))
in
Queue.add stmt stmts
in
let ulit i =
Ast.ATOM_literal (span ps apos bpos (Ast.LIT_uint (Int64.of_int i)))
in
let n = ref 0 in
let tmp_lval = make_new_str "" in
let final_stmt =
span ps apos bpos
(Ast.STMT_copy
(clone_lval ps dst_lval,
Ast.EXPR_atom (Ast.ATOM_lval tmp_lval)))
in
Array.iter
begin
fun piece ->
match piece with
Extfmt.PIECE_string s ->
let s_lval = make_new_str s in
make_append_lval tmp_lval s_lval
| Extfmt.PIECE_conversion conv ->
if not
((conv.Extfmt.conv_parameter = None) &&
(conv.Extfmt.conv_flags = []) &&
(conv.Extfmt.conv_width = Extfmt.COUNT_implied) &&
(conv.Extfmt.conv_precision = Extfmt.COUNT_implied))
then
raise (err "conversion not supported in #fmt string" ps);
if !n >= Array.length args
then raise (err "too many conversions in #fmt string" ps);
let arg = args.(!n) in
incr n;
match conv.Extfmt.conv_ty with
Extfmt.TY_str ->
make_append tmp_lval arg
| Extfmt.TY_int Extfmt.SIGNED ->
let t = make_new_tmp () in
make_call t
["std"; "_int"; "to_str" ] [| arg; ulit 10 |];
make_append_lval tmp_lval t
| Extfmt.TY_int Extfmt.UNSIGNED ->
let t = make_new_tmp () in
make_call t
["std"; "_uint"; "to_str" ] [| arg; ulit 10 |];
make_append_lval tmp_lval t
| _ ->
raise (err "conversion not supported in #fmt" ps);
end
pieces;
Queue.add final_stmt stmts;
queue_to_arr stmts;
and atom_lval (_:pstate) (at:Ast.atom) : Ast.lval =
match at with
Ast.ATOM_lval lv -> lv
| Ast.ATOM_literal _
| Ast.ATOM_pexp _ -> bug () "Pexp.atom_lval on non-ATOM_lval"
;;
(*
* Local Variables:
* fill-column: 78;
* indent-tabs-mode: nil
* buffer-file-coding-system: utf-8-unix
* compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
* End:
*)
|