Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler)!: Refactor enum constructors #1211

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
583 changes: 244 additions & 339 deletions compiler/src/codegen/compcore.re

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions compiler/src/codegen/compcore.rei
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ open Grain_middle_end;
open Mashtree;
open Binaryen;

let grain_main: string;
let grain_type_metadata: string;
let grain_start: string;

type codegen_env = {
name: option(string),
num_args: int,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/codegen/mashtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type prim1 =
| NewInt64
| NewFloat32
| NewFloat64
| BuiltinId
| LoadAdtVariant
| StringSize
| BytesSize
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/codegen/runtime_errors.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ open Sexplib.Conv;
type grain_error =
| IndexOutOfBounds
| IndexNonInteger
| MatchFailure
| AssertionError;
| MatchFailure;
3 changes: 1 addition & 2 deletions compiler/src/codegen/runtime_errors.rei
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
type grain_error =
| IndexOutOfBounds
| IndexNonInteger
| MatchFailure
| AssertionError;
| MatchFailure;
65 changes: 0 additions & 65 deletions compiler/src/codegen/transl_anf.re
Original file line number Diff line number Diff line change
Expand Up @@ -1361,71 +1361,6 @@ let transl_signature = (~functions, ~imports, signature) => {
| ReprValue(_) => TSigValue(vid, vd)
};
}
| TSigType(tid, {type_kind: TDataVariant(cds)} as td, rs) => {
let cds =
List.map(
({cd_id, cd_repr} as cd) => {
exports :=
[
GlobalExport({
ex_global_name: Ident.name(cd_id),
ex_global_internal_name: Ident.unique_name(cd_id),
}),
...exports^,
];
switch (cd_repr) {
| ReprFunction(args, res, _) =>
let external_name = Ident.name(cd_id);
let internal_name = Ident.unique_name(cd_id);
exports :=
[
FunctionExport({
ex_function_name: external_name,
ex_function_internal_name: internal_name,
}),
...exports^,
];
{
...cd,
cd_repr: ReprFunction(args, res, Direct(internal_name)),
};
| ReprValue(_) => cd
};
},
cds,
);
TSigType(tid, {...td, type_kind: TDataVariant(cds)}, rs);
}
| TSigTypeExt(tid, {ext_name, ext_args, ext_repr} as cstr, rs) => {
exports :=
[
GlobalExport({
ex_global_name: Ident.name(ext_name),
ex_global_internal_name: Ident.unique_name(ext_name),
}),
...exports^,
];
let cstr =
switch (ext_repr) {
| ReprFunction(args, res, _) =>
let external_name = Ident.name(ext_name);
let internal_name = Ident.unique_name(ext_name);
exports :=
[
FunctionExport({
ex_function_name: external_name,
ex_function_internal_name: internal_name,
}),
...exports^,
];
{
...cstr,
ext_repr: ReprFunction(args, res, Direct(internal_name)),
};
| ReprValue(_) => cstr
};
TSigTypeExt(tid, cstr, rs);
}
| _ as item => item,
signature.Cmi_format.cmi_sign,
);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/formatting/debug.re
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ let debug_expression = (expr: Parsetree.expression) => {
| PExpLambda(patterns, expression) =>
print_loc("PExpLambda", expr.pexp_loc)
| PExpApp(func, expressions) => print_loc("PExpApp", expr.pexp_loc)
| PExpConstruct(func, expression) =>
print_loc("PExpConstruct", expr.pexp_loc)
| PExpBlock(expressions) => print_loc("PExpBlock", expr.pexp_loc)
| PExpBoxAssign(expression, expression1) =>
print_loc("PExpBoxAssign", expr.pexp_loc)
Expand Down
65 changes: 29 additions & 36 deletions compiler/src/formatting/format.re
Original file line number Diff line number Diff line change
Expand Up @@ -1125,16 +1125,16 @@ and resugar_pattern_list_inner = (patterns: list(Parsetree.pattern)) => {
switch (patterns) {
| [arg1, arg2, ..._] =>
switch (arg2.ppat_desc) {
| PPatConstruct(innerfunc, innerpatterns) =>
let func =
switch (innerfunc.txt) {
| PPatConstruct(innercstr, innerpatterns) =>
let cstr =
switch (innercstr.txt) {
| IdentName({txt: name}) => name
| _ => ""
};

if (func == "[]") {
if (cstr == "[]") {
[RegularPattern(arg1)];
} else if (func == list_cons) {
} else if (cstr == list_cons) {
let inner = resugar_pattern_list_inner(innerpatterns);
[RegularPattern(arg1), ...inner];
} else {
Expand All @@ -1148,20 +1148,6 @@ and resugar_pattern_list_inner = (patterns: list(Parsetree.pattern)) => {
};
}

and is_empty_list = (expr: Parsetree.expression) => {
switch (expr.pexp_desc) {
| PExpId(loc) =>
let loc_txt =
switch (loc.txt) {
| IdentName({txt: name}) => name
| _ => ""
};

loc_txt == "[]";
| _ => false
};
}

and resugar_list =
(
~original_source: array(string),
Expand Down Expand Up @@ -1323,21 +1309,11 @@ and resugar_list_inner = (expressions: list(Parsetree.expression)) =>
switch (expressions) {
| [arg1, arg2] =>
switch (arg2.pexp_desc) {
| PExpApp(innerfunc, innerexpressions) =>
let func_name = get_function_name(innerfunc);

if (func_name == list_cons) {
let inner = resugar_list_inner(innerexpressions);
List.append([Regular(arg1)], inner);
} else {
[Regular(arg1), Spread(arg2)];
};
| _ =>
if (is_empty_list(arg2)) {
[Regular(arg1)];
} else {
[Regular(arg1), Spread(arg2)];
}
| PExpConstruct({txt: IdentName({txt: "[...]"})}, innerexpressions) =>
let inner = resugar_list_inner(innerexpressions);
List.append([Regular(arg1)], inner);
| PExpConstruct({txt: IdentName({txt: "[]"})}, _) => [Regular(arg1)]
| _ => [Regular(arg1), Spread(arg2)]
}
| _ =>
// Grain syntax makes it impossible to construct a list cons without
Expand Down Expand Up @@ -2438,8 +2414,6 @@ and print_other_application =
)
| _ when infixop(function_name) =>
raise(IllegalParse("Formatter error, wrong number of args "))
| _ when function_name == list_cons =>
resugar_list(~original_source, ~comments, expressions)
| [first_expr, ..._]
when Array.exists(fn => function_name == fn, exception_primitives) =>
Doc.concat([
Expand Down Expand Up @@ -3617,6 +3591,24 @@ and print_expression_inner =
~comments=comments_in_expression,
func,
);
| PExpConstruct({txt: IdentName({txt: "[...]"})}, expressions) =>
resugar_list(~original_source, ~comments, expressions)
| PExpConstruct({txt: IdentName({txt: "[]"})}, expressions) =>
Doc.text("[]")
| PExpConstruct({txt: id}, []) => print_ident(id)
| PExpConstruct(constr, expressions) =>
let comments_in_expression =
Comment_utils.get_comments_inside_location(
~location=expr.pexp_loc,
comments,
);
print_application(
~expression_parent,
~expressions,
~original_source,
~comments=comments_in_expression,
Ast_helper.Exp.ident(constr),
);
| PExpBlock(expressions) =>
switch (expressions) {
| [] =>
Expand Down Expand Up @@ -3719,6 +3711,7 @@ and print_expression_inner =
and is_grouped_access_expression = (expr: Parsetree.expression) => {
switch (expr.pexp_desc) {
| PExpConstant(_)
| PExpConstruct(_)
| PExpTuple(_)
| PExpId(_)
| PExpArrayGet(_)
Expand Down
Loading