mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-01 07:41:12 +00:00
Merge pull request #5 from rtfeldman/desugar-apply
Desugar pizza into Apply
This commit is contained in:
commit
4af0ee0e64
2 changed files with 53 additions and 3 deletions
|
@ -199,7 +199,32 @@ pub fn desugar<'a>(arena: &'a Bump, loc_expr: &'a Located<Expr<'a>>) -> &'a Loca
|
||||||
let value = match loc_op.value {
|
let value = match loc_op.value {
|
||||||
Pizza => {
|
Pizza => {
|
||||||
// Rewrite the Pizza operator into an Apply
|
// Rewrite the Pizza operator into an Apply
|
||||||
panic!("TODO desugar |> operator into an Apply");
|
|
||||||
|
match &right.value {
|
||||||
|
Apply(function, arguments, _called_via) => {
|
||||||
|
let mut args = Vec::with_capacity_in(1 + arguments.len(), arena);
|
||||||
|
|
||||||
|
args.push(left);
|
||||||
|
|
||||||
|
for arg in arguments {
|
||||||
|
args.push(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Apply(function, args, CalledVia::BinOp(Pizza))
|
||||||
|
}
|
||||||
|
expr => {
|
||||||
|
// e.g. `1 |> (if b then (\a -> a) else (\c -> c))`
|
||||||
|
let mut args = Vec::with_capacity_in(1, arena);
|
||||||
|
args.push(*arena.alloc(left));
|
||||||
|
|
||||||
|
let function = arena.alloc(Located {
|
||||||
|
value: expr.clone(),
|
||||||
|
region: right.region,
|
||||||
|
});
|
||||||
|
|
||||||
|
Apply(function, args, CalledVia::BinOp(Pizza))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
binop => {
|
binop => {
|
||||||
// This is a normal binary operator like (+), so desugar it
|
// This is a normal binary operator like (+), so desugar it
|
||||||
|
@ -207,8 +232,8 @@ pub fn desugar<'a>(arena: &'a Bump, loc_expr: &'a Located<Expr<'a>>) -> &'a Loca
|
||||||
let (module_parts, name) = desugar_binop(binop, arena);
|
let (module_parts, name) = desugar_binop(binop, arena);
|
||||||
let mut args = Vec::with_capacity_in(2, arena);
|
let mut args = Vec::with_capacity_in(2, arena);
|
||||||
|
|
||||||
args.push(*arena.alloc(left));
|
args.push(left);
|
||||||
args.push(*arena.alloc(right));
|
args.push(right);
|
||||||
|
|
||||||
let loc_expr = arena.alloc(Located {
|
let loc_expr = arena.alloc(Located {
|
||||||
value: Expr::Var(module_parts, name),
|
value: Expr::Var(module_parts, name),
|
||||||
|
|
|
@ -505,6 +505,31 @@ mod test_infer {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pizza_desugar() {
|
||||||
|
infer_eq(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
1 |> (\a -> a)
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
"Int",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pizza_desugar_two_arguments() {
|
||||||
|
infer_eq(
|
||||||
|
indoc!(
|
||||||
|
r#"
|
||||||
|
always = \a b -> a
|
||||||
|
|
||||||
|
1 |> always "foo"
|
||||||
|
"#
|
||||||
|
),
|
||||||
|
"Int",
|
||||||
|
);
|
||||||
|
}
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn identity() {
|
// fn identity() {
|
||||||
// infer_eq(
|
// infer_eq(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue