Skip to content

Commit

Permalink
Move PNC apply to separate Expr/Pattern variant
Browse files Browse the repository at this point in the history
  • Loading branch information
gamebox committed Jan 8, 2025
1 parent 96fc573 commit 898b3f5
Show file tree
Hide file tree
Showing 70 changed files with 875 additions and 557 deletions.
19 changes: 7 additions & 12 deletions crates/compiler/can/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use roc_error_macros::internal_error;
use roc_module::{called_via::CalledVia, symbol::Symbol};
use roc_parse::ast::{self, Collection, PatternApplyStyle};
use roc_parse::ast::{self, Collection};
use roc_region::all::{Loc, Region};

use crate::{env::Env, pattern::Pattern, scope::Scope};
Expand All @@ -27,7 +27,6 @@ fn to_encoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
DERIVED_REGION,
ast::Pattern::Identifier { ident: payload },
)]),
ast::PatternApplyStyle::Whitespace,
);

// Encode.to_encoder(payload)
Expand All @@ -40,7 +39,7 @@ fn to_encoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
module_name: "",
ident: payload,
})]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

// \@Opaq payload -> Encode.to_encoder(payload)
Expand Down Expand Up @@ -133,7 +132,6 @@ fn hash<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
DERIVED_REGION,
ast::Pattern::Identifier { ident: payload },
)]),
PatternApplyStyle::Whitespace,
);

// Hash.hash(hasher, payload)
Expand All @@ -152,7 +150,7 @@ fn hash<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
ident: payload,
}),
]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

// \hasher, @Opaq payload -> Hash.hash(hasher, payload)
Expand Down Expand Up @@ -180,7 +178,6 @@ fn is_eq<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
DERIVED_REGION,
ast::Pattern::Identifier { ident: payload1 },
)]),
PatternApplyStyle::Whitespace,
);
// \@Opaq payload2
let opaque2 = ast::Pattern::Apply(
Expand All @@ -189,7 +186,6 @@ fn is_eq<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
DERIVED_REGION,
ast::Pattern::Identifier { ident: payload2 },
)]),
PatternApplyStyle::Whitespace,
);

// Bool.is_eq(payload1, payload2)
Expand All @@ -208,7 +204,7 @@ fn is_eq<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
ident: payload2,
}),
]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

// \@Opaq payload1, @Opaq payload2 -> Bool.is_eq(payload1, payload2)
Expand Down Expand Up @@ -236,7 +232,6 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
DERIVED_REGION,
ast::Pattern::Identifier { ident: payload },
)]),
PatternApplyStyle::Whitespace,
);

// Inspect.to_inspector(payload)
Expand All @@ -249,7 +244,7 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
module_name: "",
ident: payload,
})]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

// Inspect.tag("@opaque", [Inspect.to_inspector(payload)])
Expand All @@ -264,7 +259,7 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
ident: "tag",
}),
&*env.arena.alloc([&*opaque_name, &*to_inspector_list]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

let fmt = "#fmt";
Expand All @@ -282,7 +277,7 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
ident: fmt,
}),
]),
roc_module::called_via::CalledVia::Space,
CalledVia::Space,
));

let custom_closure = alloc_expr(ast::Expr::Closure(
Expand Down
122 changes: 111 additions & 11 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use roc_module::called_via::{BinOp, CalledVia};
use roc_module::ident::ModuleName;
use roc_parse::ast::Expr::{self, *};
use roc_parse::ast::{
is_expr_suffixed, AssignedField, Collection, Defs, ModuleImportParams, Pattern,
PatternApplyStyle, ResultTryKind, StrLiteral, StrSegment, TryTarget, TypeAnnotation, ValueDef,
WhenBranch,
is_expr_suffixed, AssignedField, Collection, Defs, ModuleImportParams, Pattern, ResultTryKind,
StrLiteral, StrSegment, TryTarget, TypeAnnotation, ValueDef, WhenBranch,
};
use roc_problem::can::Problem;
use roc_region::all::{Loc, Region};
Expand Down Expand Up @@ -178,10 +177,9 @@ fn new_op_call_expr<'a>(
env.arena.alloc(Loc::at(left.region, Pattern::Tag("Ok")));
branch_1_patts.push(Loc::at(
left.region,
Pattern::Apply(
Pattern::PncApply(
branch_1_tag,
branch_1_patts_args.into_bump_slice(),
PatternApplyStyle::ParensAndCommas,
Collection::with_items(branch_1_patts_args.into_bump_slice()),
),
));
let branch_one: &WhenBranch<'_> = env.arena.alloc(WhenBranch {
Expand All @@ -203,10 +201,9 @@ fn new_op_call_expr<'a>(
env.arena.alloc(Loc::at(left.region, Pattern::Tag("Err")));
branch_2_patts.push(Loc::at(
right.region,
Pattern::Apply(
Pattern::PncApply(
branch_2_tag,
branch_2_patts_args.into_bump_slice(),
PatternApplyStyle::ParensAndCommas,
Collection::with_items(branch_2_patts_args.into_bump_slice()),
),
));
let branch_two: &WhenBranch<'_> = env.arena.alloc(WhenBranch {
Expand Down Expand Up @@ -1126,6 +1123,94 @@ pub fn desugar_expr<'a>(
region: loc_expr.region,
})
}
PncApply(Loc { value: Dbg, .. }, loc_args) => {
if loc_args.is_empty() {
env.problem(Problem::UnappliedDbg {
region: loc_expr.region,
});
env.arena.alloc(Loc {
value: *desugar_invalid_dbg_expr(env, scope, loc_expr.region),
region: loc_expr.region,
})
} else if loc_args.len() > 1 {
let args_region = Region::span_across(
&loc_args.items.first().unwrap().region,
&loc_args.items.last().unwrap().region,
);
env.problem(Problem::OverAppliedDbg {
region: args_region,
});

env.arena.alloc(Loc {
value: *desugar_invalid_dbg_expr(env, scope, loc_expr.region),
region: loc_expr.region,
})
} else {
let desugared_arg = desugar_expr(env, scope, loc_args.items.first().unwrap());

env.arena.alloc(Loc {
value: *desugar_dbg_expr(env, scope, desugared_arg, loc_expr.region),
region: loc_expr.region,
})
}
}
PncApply(
Loc {
value: Try,
region: _,
},
loc_args,
) => {
let result_expr = if loc_args.len() == 1 {
desugar_expr(env, scope, loc_args.items[0])
} else {
let function = desugar_expr(env, scope, loc_args.items.first().unwrap());
let mut desugared_args = Vec::with_capacity_in(loc_args.len() - 1, env.arena);
for loc_arg in &loc_args.items[1..] {
desugared_args.push(desugar_expr(env, scope, loc_arg));
}

let args_region = Region::span_across(
&loc_args.items[0].region,
&loc_args.items[loc_args.items.len() - 1].region,
);

env.arena.alloc(Loc::at(
args_region,
Expr::Apply(function, desugared_args.into_bump_slice(), CalledVia::Try),
))
};

env.arena.alloc(Loc::at(
loc_expr.region,
Expr::LowLevelTry(result_expr, ResultTryKind::KeywordPrefix),
))
}
PncApply(loc_fn, loc_args) => {
let mut desugared_args = Vec::with_capacity_in(loc_args.len(), env.arena);

for loc_arg in loc_args.iter() {
let mut current = loc_arg.value;
let arg = loop {
match current {
SpaceBefore(expr, _) | SpaceAfter(expr, _) => {
current = *expr;
}
_ => break loc_arg,
}
};

desugared_args.push(desugar_expr(env, scope, arg));
}

let desugared_args = Collection::with_items(desugared_args.into_bump_slice());

env.arena.alloc(Loc {
value: PncApply(desugar_expr(env, scope, loc_fn), desugared_args),
region: loc_expr.region,
})
}

When(loc_cond_expr, branches) => {
let loc_desugared_cond = &*env.arena.alloc(desugar_expr(env, scope, loc_cond_expr));
let mut desugared_branches = Vec::with_capacity_in(branches.len(), env.arena);
Expand Down Expand Up @@ -1422,7 +1507,19 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
| MalformedIdent(_, _)
| QualifiedIdentifier { .. } => pattern,

Apply(tag, arg_patterns, style) => {
Apply(tag, arg_patterns) => {
// Skip desugaring the tag, it should either be a Tag or OpaqueRef
let mut desugared_arg_patterns = Vec::with_capacity_in(arg_patterns.len(), env.arena);
for arg_pattern in arg_patterns.iter() {
desugared_arg_patterns.push(Loc {
region: arg_pattern.region,
value: desugar_pattern(env, scope, arg_pattern.value),
});
}

Apply(tag, desugared_arg_patterns.into_bump_slice())
}
PncApply(tag, arg_patterns) => {
// Skip desugaring the tag, it should either be a Tag or OpaqueRef
let mut desugared_arg_patterns = Vec::with_capacity_in(arg_patterns.len(), env.arena);
for arg_pattern in arg_patterns.iter() {
Expand All @@ -1432,7 +1529,10 @@ fn desugar_pattern<'a>(env: &mut Env<'a>, scope: &mut Scope, pattern: Pattern<'a
});
}

Apply(tag, desugared_arg_patterns.into_bump_slice(), style)
PncApply(
tag,
Collection::with_items(desugared_arg_patterns.into_bump_slice()),
)
}
RecordDestructure(field_patterns) => {
RecordDestructure(desugar_record_destructures(env, scope, field_patterns))
Expand Down
Loading

0 comments on commit 898b3f5

Please sign in to comment.