Skip to content

Commit 8d465b7

Browse files
ibdafnaospencer
andauthored
feat(compiler)!: Refactor enum constructors (#1211)
Co-authored-by: Oscar Spencer <oscar@grain-lang.org> Closes #1021 Closes #1502
1 parent 2c626ed commit 8d465b7

File tree

381 files changed

+18986
-11578
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

381 files changed

+18986
-11578
lines changed

compiler/src/codegen/compcore.re

Lines changed: 244 additions & 339 deletions
Large diffs are not rendered by default.

compiler/src/codegen/compcore.rei

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ open Grain_middle_end;
33
open Mashtree;
44
open Binaryen;
55

6+
let grain_main: string;
7+
let grain_type_metadata: string;
8+
let grain_start: string;
9+
610
type codegen_env = {
711
name: option(string),
812
num_args: int,

compiler/src/codegen/mashtree.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type prim1 =
190190
| NewInt64
191191
| NewFloat32
192192
| NewFloat64
193+
| BuiltinId
193194
| LoadAdtVariant
194195
| StringSize
195196
| BytesSize

compiler/src/codegen/runtime_errors.re

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ open Sexplib.Conv;
55
type grain_error =
66
| IndexOutOfBounds
77
| IndexNonInteger
8-
| MatchFailure
9-
| AssertionError;
8+
| MatchFailure;

compiler/src/codegen/runtime_errors.rei

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
type grain_error =
55
| IndexOutOfBounds
66
| IndexNonInteger
7-
| MatchFailure
8-
| AssertionError;
7+
| MatchFailure;

compiler/src/codegen/transl_anf.re

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,71 +1361,6 @@ let transl_signature = (~functions, ~imports, signature) => {
13611361
| ReprValue(_) => TSigValue(vid, vd)
13621362
};
13631363
}
1364-
| TSigType(tid, {type_kind: TDataVariant(cds)} as td, rs) => {
1365-
let cds =
1366-
List.map(
1367-
({cd_id, cd_repr} as cd) => {
1368-
exports :=
1369-
[
1370-
GlobalExport({
1371-
ex_global_name: Ident.name(cd_id),
1372-
ex_global_internal_name: Ident.unique_name(cd_id),
1373-
}),
1374-
...exports^,
1375-
];
1376-
switch (cd_repr) {
1377-
| ReprFunction(args, res, _) =>
1378-
let external_name = Ident.name(cd_id);
1379-
let internal_name = Ident.unique_name(cd_id);
1380-
exports :=
1381-
[
1382-
FunctionExport({
1383-
ex_function_name: external_name,
1384-
ex_function_internal_name: internal_name,
1385-
}),
1386-
...exports^,
1387-
];
1388-
{
1389-
...cd,
1390-
cd_repr: ReprFunction(args, res, Direct(internal_name)),
1391-
};
1392-
| ReprValue(_) => cd
1393-
};
1394-
},
1395-
cds,
1396-
);
1397-
TSigType(tid, {...td, type_kind: TDataVariant(cds)}, rs);
1398-
}
1399-
| TSigTypeExt(tid, {ext_name, ext_args, ext_repr} as cstr, rs) => {
1400-
exports :=
1401-
[
1402-
GlobalExport({
1403-
ex_global_name: Ident.name(ext_name),
1404-
ex_global_internal_name: Ident.unique_name(ext_name),
1405-
}),
1406-
...exports^,
1407-
];
1408-
let cstr =
1409-
switch (ext_repr) {
1410-
| ReprFunction(args, res, _) =>
1411-
let external_name = Ident.name(ext_name);
1412-
let internal_name = Ident.unique_name(ext_name);
1413-
exports :=
1414-
[
1415-
FunctionExport({
1416-
ex_function_name: external_name,
1417-
ex_function_internal_name: internal_name,
1418-
}),
1419-
...exports^,
1420-
];
1421-
{
1422-
...cstr,
1423-
ext_repr: ReprFunction(args, res, Direct(internal_name)),
1424-
};
1425-
| ReprValue(_) => cstr
1426-
};
1427-
TSigTypeExt(tid, cstr, rs);
1428-
}
14291364
| _ as item => item,
14301365
signature.Cmi_format.cmi_sign,
14311366
);

compiler/src/formatting/debug.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ let debug_expression = (expr: Parsetree.expression) => {
8181
| PExpLambda(patterns, expression) =>
8282
print_loc("PExpLambda", expr.pexp_loc)
8383
| PExpApp(func, expressions) => print_loc("PExpApp", expr.pexp_loc)
84+
| PExpConstruct(func, expression) =>
85+
print_loc("PExpConstruct", expr.pexp_loc)
8486
| PExpBlock(expressions) => print_loc("PExpBlock", expr.pexp_loc)
8587
| PExpBoxAssign(expression, expression1) =>
8688
print_loc("PExpBoxAssign", expr.pexp_loc)

compiler/src/formatting/format.re

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,16 +1125,16 @@ and resugar_pattern_list_inner = (patterns: list(Parsetree.pattern)) => {
11251125
switch (patterns) {
11261126
| [arg1, arg2, ..._] =>
11271127
switch (arg2.ppat_desc) {
1128-
| PPatConstruct(innerfunc, innerpatterns) =>
1129-
let func =
1130-
switch (innerfunc.txt) {
1128+
| PPatConstruct(innercstr, innerpatterns) =>
1129+
let cstr =
1130+
switch (innercstr.txt) {
11311131
| IdentName({txt: name}) => name
11321132
| _ => ""
11331133
};
11341134

1135-
if (func == "[]") {
1135+
if (cstr == "[]") {
11361136
[RegularPattern(arg1)];
1137-
} else if (func == list_cons) {
1137+
} else if (cstr == list_cons) {
11381138
let inner = resugar_pattern_list_inner(innerpatterns);
11391139
[RegularPattern(arg1), ...inner];
11401140
} else {
@@ -1148,20 +1148,6 @@ and resugar_pattern_list_inner = (patterns: list(Parsetree.pattern)) => {
11481148
};
11491149
}
11501150

1151-
and is_empty_list = (expr: Parsetree.expression) => {
1152-
switch (expr.pexp_desc) {
1153-
| PExpId(loc) =>
1154-
let loc_txt =
1155-
switch (loc.txt) {
1156-
| IdentName({txt: name}) => name
1157-
| _ => ""
1158-
};
1159-
1160-
loc_txt == "[]";
1161-
| _ => false
1162-
};
1163-
}
1164-
11651151
and resugar_list =
11661152
(
11671153
~original_source: array(string),
@@ -1323,21 +1309,11 @@ and resugar_list_inner = (expressions: list(Parsetree.expression)) =>
13231309
switch (expressions) {
13241310
| [arg1, arg2] =>
13251311
switch (arg2.pexp_desc) {
1326-
| PExpApp(innerfunc, innerexpressions) =>
1327-
let func_name = get_function_name(innerfunc);
1328-
1329-
if (func_name == list_cons) {
1330-
let inner = resugar_list_inner(innerexpressions);
1331-
List.append([Regular(arg1)], inner);
1332-
} else {
1333-
[Regular(arg1), Spread(arg2)];
1334-
};
1335-
| _ =>
1336-
if (is_empty_list(arg2)) {
1337-
[Regular(arg1)];
1338-
} else {
1339-
[Regular(arg1), Spread(arg2)];
1340-
}
1312+
| PExpConstruct({txt: IdentName({txt: "[...]"})}, innerexpressions) =>
1313+
let inner = resugar_list_inner(innerexpressions);
1314+
List.append([Regular(arg1)], inner);
1315+
| PExpConstruct({txt: IdentName({txt: "[]"})}, _) => [Regular(arg1)]
1316+
| _ => [Regular(arg1), Spread(arg2)]
13411317
}
13421318
| _ =>
13431319
// Grain syntax makes it impossible to construct a list cons without
@@ -2438,8 +2414,6 @@ and print_other_application =
24382414
)
24392415
| _ when infixop(function_name) =>
24402416
raise(IllegalParse("Formatter error, wrong number of args "))
2441-
| _ when function_name == list_cons =>
2442-
resugar_list(~original_source, ~comments, expressions)
24432417
| [first_expr, ..._]
24442418
when Array.exists(fn => function_name == fn, exception_primitives) =>
24452419
Doc.concat([
@@ -3617,6 +3591,24 @@ and print_expression_inner =
36173591
~comments=comments_in_expression,
36183592
func,
36193593
);
3594+
| PExpConstruct({txt: IdentName({txt: "[...]"})}, expressions) =>
3595+
resugar_list(~original_source, ~comments, expressions)
3596+
| PExpConstruct({txt: IdentName({txt: "[]"})}, expressions) =>
3597+
Doc.text("[]")
3598+
| PExpConstruct({txt: id}, []) => print_ident(id)
3599+
| PExpConstruct(constr, expressions) =>
3600+
let comments_in_expression =
3601+
Comment_utils.get_comments_inside_location(
3602+
~location=expr.pexp_loc,
3603+
comments,
3604+
);
3605+
print_application(
3606+
~expression_parent,
3607+
~expressions,
3608+
~original_source,
3609+
~comments=comments_in_expression,
3610+
Ast_helper.Exp.ident(constr),
3611+
);
36203612
| PExpBlock(expressions) =>
36213613
switch (expressions) {
36223614
| [] =>
@@ -3719,6 +3711,7 @@ and print_expression_inner =
37193711
and is_grouped_access_expression = (expr: Parsetree.expression) => {
37203712
switch (expr.pexp_desc) {
37213713
| PExpConstant(_)
3714+
| PExpConstruct(_)
37223715
| PExpTuple(_)
37233716
| PExpId(_)
37243717
| PExpArrayGet(_)

0 commit comments

Comments
 (0)