Skip to content

Commit 8af0f18

Browse files
committed
changed concat functionality, other minor changes
1 parent cb08f38 commit 8af0f18

File tree

19 files changed

+174
-122
lines changed

19 files changed

+174
-122
lines changed

compiler/src/codegen/compcore.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,14 +3112,14 @@ and compile_instr = (wasm_mod, env, instr) =>
31123112
compile_record_op(wasm_mod, env, record, record_op)
31133113
| MClosureOp(closure_op, closure) =>
31143114
compile_closure_op(wasm_mod, env, closure, closure_op)
3115-
| MCollectionConcat(t, colls) =>
3116-
let colls_arr = allocate_array(wasm_mod, env, colls);
3115+
| MCollectionConcat(t, collections) =>
3116+
let collections_arr = allocate_array(wasm_mod, env, collections);
31173117
let concat =
31183118
switch (t) {
31193119
| TExpListConcat => call_list_concat
31203120
| TExpArrayConcat => call_array_concat
31213121
};
3122-
concat(wasm_mod, env, [colls_arr]);
3122+
concat(wasm_mod, env, [collections_arr]);
31233123
| MPrim0(p0) => compile_prim0(wasm_mod, env, p0)
31243124
| MPrim1(p1, arg) => compile_prim1(wasm_mod, env, p1, arg, instr.instr_loc)
31253125
| MPrim2(p2, arg1, arg2) => compile_prim2(wasm_mod, env, p2, arg1, arg2)

compiler/src/codegen/transl_anf.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ module RegisterAllocation = {
185185
| MArrayOp(aop, i) => MArrayOp(aop, apply_allocation_to_imm(i))
186186
| MRecordOp(rop, i) => MRecordOp(rop, apply_allocation_to_imm(i))
187187
| MAdtOp(aop, i) => MAdtOp(aop, apply_allocation_to_imm(i))
188-
| MCollectionConcat(t, colls) =>
189-
MCollectionConcat(t, List.map(apply_allocation_to_imm, colls))
188+
| MCollectionConcat(t, collections) =>
189+
MCollectionConcat(t, List.map(apply_allocation_to_imm, collections))
190190
| MClosureOp(cop, i) => MClosureOp(cop, apply_allocation_to_imm(i))
191191
| MCallRaw({func, func_type, args}) =>
192192
MCallRaw({

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
@@ -2824,8 +2824,8 @@ and print_expression_inner =
28242824
print_ident(txt),
28252825
]);
28262826
print_assignment(~original_source, ~comments, left, expression2);
2827-
| PExpCollectionConcat(concat_t, colls) =>
2828-
let list_length = List.length(colls);
2827+
| PExpCollectionConcat(concat_t, collections) =>
2828+
let list_length = List.length(collections);
28292829

28302830
let items =
28312831
List.mapi(
@@ -2841,7 +2841,7 @@ and print_expression_inner =
28412841
// 3]
28422842
let end_line_comments =
28432843
if (index < list_length - 2) {
2844-
let (_, next_e) = List.nth(colls, index + 1);
2844+
let (_, next_e) = List.nth(collections, index + 1);
28452845
Comment_utils.get_comments_between_locations(
28462846
~loc1=e.Parsetree.pexp_loc,
28472847
~loc2=next_e.pexp_loc,
@@ -2903,7 +2903,7 @@ and print_expression_inner =
29032903
};
29042904
(expr, end_line_comments);
29052905
},
2906-
colls,
2906+
collections,
29072907
);
29082908

29092909
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
@@ -115,11 +115,11 @@ module FreeVarsArg: Anf_iterator.IterArgument = {
115115
Ident.Set.empty,
116116
[arg1, arg2, arg3],
117117
)
118-
| CCollectionConcat(t, colls) =>
118+
| CCollectionConcat(t, collections) =>
119119
List.fold_left(
120120
(acc, a) => Ident.Set.union(imm_free_vars(a), acc),
121121
Ident.Set.empty,
122-
colls,
122+
collections,
123123
)
124124
| CRecord(_, args) =>
125125
List.fold_left(

compiler/src/middle_end/anf_helper.re

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ module Comp = {
142142
~env?,
143143
CSetRecordItem(idx, record, arg),
144144
);
145-
let collection_concat = (~loc=?, ~attributes=?, ~env=?, t, colls) =>
145+
let collection_concat = (~loc=?, ~attributes=?, ~env=?, t, collections) =>
146146
mk(
147147
~loc?,
148148
~attributes?,
149149
~allocation_type=Managed,
150150
~env?,
151-
CCollectionConcat(t, colls),
151+
CCollectionConcat(t, collections),
152152
);
153153
let if_ = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, cond, tru, fals) =>
154154
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
@@ -81,7 +81,8 @@ module MakeIter = (Iter: IterArgument) => {
8181
| CSetRecordItem(_, record, arg) =>
8282
iter_imm_expression(record);
8383
iter_imm_expression(arg);
84-
| CCollectionConcat(_, colls) => List.iter(iter_imm_expression, colls)
84+
| CCollectionConcat(_, collections) =>
85+
List.iter(iter_imm_expression, collections)
8586
| CIf(c, t, f) =>
8687
iter_imm_expression(c);
8788
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
@@ -195,9 +195,9 @@ module MakeMap = (Iter: MapArgument) => {
195195
process_imm_expression(arg),
196196
),
197197
)
198-
| CCollectionConcat(t, colls) =>
198+
| CCollectionConcat(t, collections) =>
199199
leave_with(
200-
CCollectionConcat(t, List.map(process_imm_expression, colls)),
200+
CCollectionConcat(t, List.map(process_imm_expression, collections)),
201201
)
202202
| CIf(cond, t, f) =>
203203
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
@@ -819,9 +819,10 @@ let rec transl_imm =
819819
),
820820
],
821821
);
822-
| TExpCollectionConcat(t, colls) =>
822+
| TExpCollectionConcat(t, collections) =>
823823
let tmp = gensym("catcollection");
824-
let (new_args, new_setup) = List.split(List.map(transl_imm, colls));
824+
let (new_args, new_setup) =
825+
List.split(List.map(transl_imm, collections));
825826
(
826827
Imm.id(~loc, ~env, tmp),
827828
List.concat(new_setup)

compiler/src/parsing/ast_helper.re

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

39+
type array_concat_item =
40+
| ArrayConcatItems(list(expression))
41+
| ArrayConcatSpread(expression);
42+
3943
let ident_empty = {
4044
txt: Identifier.IdentName(Location.mknoloc("[]")),
4145
loc: Location.dummy_loc,
@@ -390,41 +394,49 @@ module Expression = {
390394
tuple_construct(~loc, ~attributes?, ident_cons, [expr, empty])
391395
| ListSpread(expr, _) => expr
392396
};
393-
if (List.exists(
394-
expr =>
395-
switch (expr) {
396-
| ListSpread(_) => true
397-
| ListItem(_) => false
398-
},
399-
rest,
400-
)) {
401-
collection_concat(
402-
~loc,
403-
~attributes?,
404-
PExpListConcat,
405-
List.map(
406-
expr =>
397+
let has_nonfinal_spread =
398+
List.exists(
399+
expr =>
400+
switch (expr) {
401+
| ListSpread(_) => true
402+
| ListItem(_) => false
403+
},
404+
rest,
405+
);
406+
if (has_nonfinal_spread) {
407+
let grouped =
408+
List.fold_left(
409+
(acc, expr) => {
407410
switch (expr) {
408-
| ListSpread(expr, loc) => (
409-
PExpSpreadExpr,
411+
| ListSpread(expr, loc) => [
410412
{...expr, pexp_loc: loc},
411-
)
412-
| ListItem(expr) => (
413-
PExpNonSpreadExpr,
414-
// Still convert to a single-element list to make later compilation steps easier
413+
...acc,
414+
]
415+
| ListItem(expr) =>
416+
let (first, rest) =
417+
switch (acc) {
418+
| [first, ...rest] => (first, rest)
419+
| _ => assert(false)
420+
};
421+
[
415422
tuple_construct(
416-
~loc=expr.pexp_loc,
423+
~loc,
417424
~attributes?,
418425
ident_cons,
419-
[
420-
expr,
421-
tuple_construct(~loc=expr.pexp_loc, ident_empty, []),
422-
],
426+
[expr, first],
423427
),
424-
)
425-
},
426-
a,
427-
),
428+
...rest,
429+
];
430+
}
431+
},
432+
[base],
433+
rest,
434+
);
435+
collection_concat(
436+
~loc,
437+
~attributes?,
438+
PExpListConcat,
439+
List.map(expr => (PExpSpreadExpr, expr), grouped),
428440
);
429441
} else {
430442
List.fold_left(
@@ -446,51 +458,70 @@ module Expression = {
446458
{...list, pexp_loc: loc};
447459
};
448460
let array_items = (~loc, ~attributes=?, a) => {
449-
let no_spreads =
450-
List.for_all(
461+
let has_spread =
462+
List.exists(
451463
x => {
452464
switch (x) {
453-
| ArrayItem(_) => true
454-
| ArraySpread(_) => false
465+
| ArraySpread(_) => true
466+
| ArrayItem(_) => false
455467
}
456468
},
457469
a,
458470
);
459-
if (no_spreads) {
460-
array(
461-
~loc,
462-
~attributes?,
463-
List.map(
464-
expr =>
471+
if (has_spread) {
472+
let grouped =
473+
List.fold_right(
474+
(expr, acc) => {
465475
switch (expr) {
466-
| ArraySpread(_) =>
467-
failwith(
468-
"Impossible: spread in array when existence has been disproven",
469-
)
470-
| ArrayItem(expr) => expr
471-
},
476+
| ArrayItem(expr) =>
477+
switch (acc) {
478+
| [ArrayConcatItems(exprs), ...rest] => [
479+
ArrayConcatItems([expr, ...exprs]),
480+
...rest,
481+
]
482+
| _ => [ArrayConcatItems([expr]), ...acc]
483+
}
484+
| ArraySpread(expr, loc) => [
485+
ArrayConcatSpread({...expr, pexp_loc: loc}),
486+
...acc,
487+
]
488+
}
489+
},
472490
a,
473-
),
474-
);
475-
} else {
491+
[],
492+
);
476493
collection_concat(
477494
~loc,
478495
~attributes?,
479496
PExpArrayConcat,
480497
List.map(
481498
x => {
482499
switch (x) {
483-
| ArrayItem(expr) => (
500+
| ArrayConcatItems([first, ...rest] as exprs) => (
484501
PExpNonSpreadExpr,
485-
// Still convert to a single-element array to make later compilation steps easier
486-
array(~loc=expr.pexp_loc, ~attributes?, [expr]),
487-
)
488-
| ArraySpread(expr, loc) => (
489-
PExpSpreadExpr,
490-
{...expr, pexp_loc: loc},
502+
array(~loc=first.pexp_loc, ~attributes?, exprs),
491503
)
504+
| ArrayConcatItems([]) =>
505+
failwith("Impossible: empty ArrayConcatItems")
506+
| ArrayConcatSpread(expr) => (PExpSpreadExpr, expr)
492507
}
493508
},
509+
grouped,
510+
),
511+
);
512+
} else {
513+
array(
514+
~loc,
515+
~attributes?,
516+
List.map(
517+
expr =>
518+
switch (expr) {
519+
| ArraySpread(_) =>
520+
failwith(
521+
"Impossible: spread in array when existence has been disproven",
522+
)
523+
| ArrayItem(expr) => expr
524+
},
494525
a,
495526
),
496527
);

0 commit comments

Comments
 (0)