Merge pull request #5 from rtfeldman/desugar-apply

Desugar pizza into Apply
This commit is contained in:
Richard Feldman 2019-11-16 00:10:29 +00:00 committed by GitHub
commit 4af0ee0e64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 3 deletions

View file

@ -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 {
Pizza => {
// 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 => {
// 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 mut args = Vec::with_capacity_in(2, arena);
args.push(*arena.alloc(left));
args.push(*arena.alloc(right));
args.push(left);
args.push(right);
let loc_expr = arena.alloc(Located {
value: Expr::Var(module_parts, name),

View file

@ -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]
// fn identity() {
// infer_eq(