Desugar blanks in function position (#1512)

This commit is contained in:
Ara Adkins 2021-02-25 11:44:13 +00:00 committed by GitHub
parent b79d5f95a6
commit 37a64dd098
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 2 deletions

View File

@ -200,14 +200,18 @@ case object LambdaShorthandToLambda extends IRPass {
) )
val newName = newFn.name val newName = newFn.name
(newFn, Some(newName)) (newFn, Some(newName))
} else (fn, None) } else {
val newFn = desugarExpression(fn, freshNameSupply)
(newFn, None)
}
val processedApp = p.copy( val processedApp = p.copy(
function = updatedFn, function = updatedFn,
arguments = updatedArgs arguments = updatedArgs
) )
// Wrap the app in lambdas from right to left, lambda / shorthand arg // Wrap the app in lambdas from right to left, 1 lambda per shorthand
// arg
val appResult = val appResult =
actualDefArgs.foldRight(processedApp: IR.Expression)((arg, body) => actualDefArgs.foldRight(processedApp: IR.Expression)((arg, body) =>
IR.Function.Lambda(List(arg), body, None) IR.Function.Lambda(List(arg), body, None)

View File

@ -497,4 +497,68 @@ class LambdaShorthandToLambdaTest extends CompilerTest {
arg.defaultValue.get shouldBe an[IR.Function.Lambda] arg.defaultValue.get shouldBe an[IR.Function.Lambda]
} }
} }
"Lambda shorthand in nested functions" should {
"correctly translate the section-function in an application" in {
implicit val ctx: InlineContext = mkInlineContext
val ir =
"""(_ + 5) 5
|""".stripMargin.preprocessExpression.get.desugar
ir shouldBe an[IR.Application.Prefix]
val app = ir.asInstanceOf[IR.Application.Prefix]
app.function shouldBe an[IR.Function.Lambda]
val lam = app.function.asInstanceOf[IR.Function.Lambda]
lam.arguments.length shouldEqual 1
val lamArg1Name = lam.arguments.head
.asInstanceOf[IR.DefinitionArgument.Specified]
.name
.name
val lamBody = lam.body.asInstanceOf[IR.Application.Prefix]
lamBody.arguments.length shouldEqual 2
val appArg1Name = lamBody.arguments.head
.asInstanceOf[IR.CallArgument.Specified]
.value
.asInstanceOf[IR.Name.Literal]
.name
lamArg1Name shouldEqual appArg1Name
}
"correctly translate the function in an application" in {
implicit val ctx: InlineContext = mkInlineContext
val ir =
"""(f _ _ b) b
|""".stripMargin.preprocessExpression.get.desugar
ir shouldBe an[IR.Function.Lambda]
val firstLam = ir.asInstanceOf[IR.Function.Lambda]
firstLam.arguments.length shouldEqual 1
val firstLamArgName = firstLam.arguments.head
.asInstanceOf[IR.DefinitionArgument.Specified]
.name
.name
val secondLam = firstLam.body.asInstanceOf[IR.Function.Lambda]
val secondLamArgName = secondLam.arguments.head
.asInstanceOf[IR.DefinitionArgument.Specified]
.name
.name
val app = secondLam.body.asInstanceOf[IR.Application.Prefix]
app.arguments.length shouldEqual 4
val appArg1Name = app.arguments.head
.asInstanceOf[IR.CallArgument.Specified]
.value
.asInstanceOf[IR.Name]
.name
val appArg2Name = app
.arguments(1)
.asInstanceOf[IR.CallArgument.Specified]
.value
.asInstanceOf[IR.Name]
.name
firstLamArgName shouldEqual appArg1Name
secondLamArgName shouldEqual appArg2Name
}
}
} }

View File

@ -153,5 +153,16 @@ class LambdaShorthandArgsTest extends InterpreterTest {
eval(code) shouldEqual 20 eval(code) shouldEqual 20
} }
"work properly when used inside the function of an application" in {
val code =
"""
|import Builtins
|
|main = (_ - 5) 0
|""".stripMargin
eval(code) shouldEqual -5
}
} }
} }