Skip to content

Commit 7af1d6a

Browse files
committed
changed concat functionality, other minor changes
1 parent c7067b0 commit 7af1d6a

File tree

17 files changed

+170
-119
lines changed

17 files changed

+170
-119
lines changed

compiler/src/codegen/compcore.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,14 +3016,14 @@ and compile_instr = (wasm_mod, env, instr) =>
30163016
compile_record_op(wasm_mod, env, record, record_op)
30173017
| MClosureOp(closure_op, closure) =>
30183018
compile_closure_op(wasm_mod, env, closure, closure_op)
3019-
| MCollectionConcat(t, colls) =>
3020-
let colls_arr = allocate_array(wasm_mod, env, colls);
3019+
| MCollectionConcat(t, collections) =>
3020+
let collections_arr = allocate_array(wasm_mod, env, collections);
30213021
let concat =
30223022
switch (t) {
30233023
| TExpListConcat => call_list_concat
30243024
| TExpArrayConcat => call_array_concat
30253025
};
3026-
concat(wasm_mod, env, [colls_arr]);
3026+
concat(wasm_mod, env, [collections_arr]);
30273027
| MPrim0(p0) => compile_prim0(wasm_mod, env, p0)
30283028
| MPrim1(p1, arg) => compile_prim1(wasm_mod, env, p1, arg, instr.instr_loc)
30293029
| MPrim2(p2, arg1, arg2) => compile_prim2(wasm_mod, env, p2, arg1, arg2)

compiler/src/formatting/debug.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let debug_expression = (expr: Parsetree.expression) => {
6161
print_loc("PExpRecordGet", expr.pexp_loc)
6262
| PExpRecordSet(expression, {txt, _}, expression2) =>
6363
print_loc("PExpRecordSet", expr.pexp_loc)
64-
| PExpCollectionConcat(t, colls) =>
64+
| PExpCollectionConcat(t, collections) =>
6565
print_loc("PExpCollectionConcat", expr.pexp_loc)
6666
| PExpMatch(expression, match_branches) =>
6767
print_loc("PExpMatch", expr.pexp_loc)

compiler/src/formatting/format.re

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,8 +2971,8 @@ and print_expression_inner =
29712971
print_ident(txt),
29722972
]);
29732973
print_assignment(~original_source, ~comments, left, expression2);
2974-
| PExpCollectionConcat(concat_t, colls) =>
2975-
let list_length = List.length(colls);
2974+
| PExpCollectionConcat(concat_t, collections) =>
2975+
let list_length = List.length(collections);
29762976

29772977
let items =
29782978
List.mapi(
@@ -2988,7 +2988,7 @@ and print_expression_inner =
29882988
// 3]
29892989
let end_line_comments =
29902990
if (index < list_length - 2) {
2991-
let (_, next_e) = List.nth(colls, index + 1);
2991+
let (_, next_e) = List.nth(collections, index + 1);
29922992
Comment_utils.get_comments_between_locations(
29932993
~loc1=e.Parsetree.pexp_loc,
29942994
~loc2=next_e.pexp_loc,
@@ -3050,7 +3050,7 @@ and print_expression_inner =
30503050
};
30513051
(expr, end_line_comments);
30523052
},
3053-
colls,
3053+
collections,
30543054
);
30553055

30563056
let (last_line_breaks_for_comments, list_items) =

compiler/src/middle_end/analyze_free_vars.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ module FreeVarsArg: Anf_iterator.IterArgument = {
109109
Ident.Set.empty,
110110
[arg1, arg2, arg3],
111111
)
112-
| CCollectionConcat(t, colls) =>
112+
| CCollectionConcat(t, collections) =>
113113
List.fold_left(
114114
(acc, a) => Ident.Set.union(imm_free_vars(a), acc),
115115
Ident.Set.empty,
116-
colls,
116+
collections,
117117
)
118118
| CRecord(_, _, args) =>
119119
List.fold_left(

compiler/src/middle_end/anf_helper.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ module Comp = {
146146
~env?,
147147
CSetRecordItem(idx, record, arg),
148148
);
149-
let collection_concat = (~loc=?, ~attributes=?, ~env=?, t, colls) =>
149+
let collection_concat = (~loc=?, ~attributes=?, ~env=?, t, collections) =>
150150
mk(
151151
~loc?,
152152
~attributes?,
153153
~allocation_type=Managed,
154154
~env?,
155-
CCollectionConcat(t, colls),
155+
CCollectionConcat(t, collections),
156156
);
157157
let if_ = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, cond, tru, fals) =>
158158
mk(~loc?, ~attributes?, ~allocation_type, ~env?, CIf(cond, tru, fals));

compiler/src/middle_end/anf_iterator.re

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ module MakeIter = (Iter: IterArgument) => {
8383
| CSetRecordItem(_, record, arg) =>
8484
iter_imm_expression(record);
8585
iter_imm_expression(arg);
86-
| CCollectionConcat(_, colls) => List.iter(iter_imm_expression, colls)
86+
| CCollectionConcat(_, collections) =>
87+
List.iter(iter_imm_expression, collections)
8788
| CIf(c, t, f) =>
8889
iter_imm_expression(c);
8990
iter_anf_expression(t);

compiler/src/middle_end/anf_mapper.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ module MakeMap = (Iter: MapArgument) => {
196196
process_imm_expression(arg),
197197
),
198198
)
199-
| CCollectionConcat(t, colls) =>
199+
| CCollectionConcat(t, collections) =>
200200
leave_with(
201-
CCollectionConcat(t, List.map(process_imm_expression, colls)),
201+
CCollectionConcat(t, List.map(process_imm_expression, collections)),
202202
)
203203
| CIf(cond, t, f) =>
204204
let cond = process_imm_expression(cond);

compiler/src/middle_end/linearize.re

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,9 +929,10 @@ let rec transl_imm =
929929
),
930930
],
931931
);
932-
| TExpCollectionConcat(t, colls) =>
932+
| TExpCollectionConcat(t, collections) =>
933933
let tmp = gensym("catcollection");
934-
let (new_args, new_setup) = List.split(List.map(transl_imm, colls));
934+
let (new_args, new_setup) =
935+
List.split(List.map(transl_imm, collections));
935936
(
936937
Imm.id(~loc, ~env, tmp),
937938
List.concat(new_setup)

compiler/src/parsing/ast_helper.re

Lines changed: 86 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type id = loc(Identifier.t);
3838
type str = loc(string);
3939
type loc = Location.t;
4040

41+
type array_concat_item =
42+
| ArrayConcatItems(list(expression))
43+
| ArrayConcatSpread(expression);
44+
4145
let ident_empty = {
4246
txt: Identifier.IdentName(Location.mknoloc("[]")),
4347
loc: Location.dummy_loc,
@@ -433,41 +437,49 @@ module Expression = {
433437
tuple_construct(~loc, ~attributes?, ident_cons, [expr, empty])
434438
| ListSpread(expr, _) => expr
435439
};
436-
if (List.exists(
437-
expr =>
438-
switch (expr) {
439-
| ListSpread(_) => true
440-
| ListItem(_) => false
441-
},
442-
rest,
443-
)) {
444-
collection_concat(
445-
~loc,
446-
~attributes?,
447-
PExpListConcat,
448-
List.map(
449-
expr =>
440+
let has_nonfinal_spread =
441+
List.exists(
442+
expr =>
443+
switch (expr) {
444+
| ListSpread(_) => true
445+
| ListItem(_) => false
446+
},
447+
rest,
448+
);
449+
if (has_nonfinal_spread) {
450+
let grouped =
451+
List.fold_left(
452+
(acc, expr) => {
450453
switch (expr) {
451-
| ListSpread(expr, loc) => (
452-
PExpSpreadExpr,
454+
| ListSpread(expr, loc) => [
453455
{...expr, pexp_loc: loc},
454-
)
455-
| ListItem(expr) => (
456-
PExpNonSpreadExpr,
457-
// Still convert to a single-element list to make later compilation steps easier
456+
...acc,
457+
]
458+
| ListItem(expr) =>
459+
let (first, rest) =
460+
switch (acc) {
461+
| [first, ...rest] => (first, rest)
462+
| _ => assert(false)
463+
};
464+
[
458465
tuple_construct(
459-
~loc=expr.pexp_loc,
466+
~loc,
460467
~attributes?,
461468
ident_cons,
462-
[
463-
expr,
464-
tuple_construct(~loc=expr.pexp_loc, ident_empty, []),
465-
],
469+
[expr, first],
466470
),
467-
)
468-
},
469-
a,
470-
),
471+
...rest,
472+
];
473+
}
474+
},
475+
[base],
476+
rest,
477+
);
478+
collection_concat(
479+
~loc,
480+
~attributes?,
481+
PExpListConcat,
482+
List.map(expr => (PExpSpreadExpr, expr), grouped),
471483
);
472484
} else {
473485
List.fold_left(
@@ -489,51 +501,70 @@ module Expression = {
489501
{...list, pexp_loc: loc};
490502
};
491503
let array_items = (~loc, ~attributes=?, a) => {
492-
let no_spreads =
493-
List.for_all(
504+
let has_spread =
505+
List.exists(
494506
x => {
495507
switch (x) {
496-
| ArrayItem(_) => true
497-
| ArraySpread(_) => false
508+
| ArraySpread(_) => true
509+
| ArrayItem(_) => false
498510
}
499511
},
500512
a,
501513
);
502-
if (no_spreads) {
503-
array(
504-
~loc,
505-
~attributes?,
506-
List.map(
507-
expr =>
514+
if (has_spread) {
515+
let grouped =
516+
List.fold_right(
517+
(expr, acc) => {
508518
switch (expr) {
509-
| ArraySpread(_) =>
510-
failwith(
511-
"Impossible: spread in array when existence has been disproven",
512-
)
513-
| ArrayItem(expr) => expr
514-
},
519+
| ArrayItem(expr) =>
520+
switch (acc) {
521+
| [ArrayConcatItems(exprs), ...rest] => [
522+
ArrayConcatItems([expr, ...exprs]),
523+
...rest,
524+
]
525+
| _ => [ArrayConcatItems([expr]), ...acc]
526+
}
527+
| ArraySpread(expr, loc) => [
528+
ArrayConcatSpread({...expr, pexp_loc: loc}),
529+
...acc,
530+
]
531+
}
532+
},
515533
a,
516-
),
517-
);
518-
} else {
534+
[],
535+
);
519536
collection_concat(
520537
~loc,
521538
~attributes?,
522539
PExpArrayConcat,
523540
List.map(
524541
x => {
525542
switch (x) {
526-
| ArrayItem(expr) => (
543+
| ArrayConcatItems([first, ...rest] as exprs) => (
527544
PExpNonSpreadExpr,
528-
// Still convert to a single-element array to make later compilation steps easier
529-
array(~loc=expr.pexp_loc, ~attributes?, [expr]),
530-
)
531-
| ArraySpread(expr, loc) => (
532-
PExpSpreadExpr,
533-
{...expr, pexp_loc: loc},
545+
array(~loc=first.pexp_loc, ~attributes?, exprs),
534546
)
547+
| ArrayConcatItems([]) =>
548+
failwith("Impossible: empty ArrayConcatItems")
549+
| ArrayConcatSpread(expr) => (PExpSpreadExpr, expr)
535550
}
536551
},
552+
grouped,
553+
),
554+
);
555+
} else {
556+
array(
557+
~loc,
558+
~attributes?,
559+
List.map(
560+
expr =>
561+
switch (expr) {
562+
| ArraySpread(_) =>
563+
failwith(
564+
"Impossible: spread in array when existence has been disproven",
565+
)
566+
| ArrayItem(expr) => expr
567+
},
537568
a,
538569
),
539570
);

compiler/src/parsing/ast_mapper.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ module E = {
8787
map_loc(sub, f),
8888
sub.expr(sub, v),
8989
)
90-
| PExpCollectionConcat(t, colls) =>
90+
| PExpCollectionConcat(t, collections) =>
9191
collection_concat(
9292
~loc,
9393
~attributes,
9494
t,
95-
List.map(((t, expr)) => (t, sub.expr(sub, expr)), colls),
95+
List.map(((t, expr)) => (t, sub.expr(sub, expr)), collections),
9696
)
9797
| PExpLet(r, m, vbs) =>
9898
let_(~loc, ~attributes, r, m, List.map(sub.value_binding(sub), vbs))

0 commit comments

Comments
 (0)