Skip to content

Commit

Permalink
Fix bug in Fir compiler and simplify compilation and generated code
Browse files Browse the repository at this point in the history
Fix a bug in the Fir to Pine compiler that caused a mismatch in parameter/argument count in some scenarios with nested declaration blocks. This commit also adds a regression test that failed with the previous compiler version.
Since understanding how the lifting works turned out to be difficult, the winning approach does away with the lifting and flattening of declaration blocks entirely and instead introduces a new way of aggregating the environment for nested declaration blocks.
  • Loading branch information
Viir committed Dec 31, 2023
1 parent c2905b9 commit c33d3fc
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 814 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ compileElmSyntaxValueConstructor valueConstructor =
)
|> Pine.ListExpression
]
|> emitWrapperForPartialApplication [] argumentsCount
|> emitWrapperForPartialApplication (Pine.ListExpression []) argumentsCount
|> evaluateAsIndependentExpression
|> Result.withDefault
(Pine.valueFromString "Failed to compile choice type tag constructor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import FirCompiler
( ElmModuleInCompilation
, EmitStack
, Expression(..)
, listDependenciesOfExpression
, listTransitiveDependenciesOfExpression
, parseFunctionParameters
)
import Json.Decode
Expand Down Expand Up @@ -507,7 +507,7 @@ inlineApplicationsOfEnvironmentDeclarations stackBeforeAddingDeps environmentDec
let
newReferencesDependencies =
environmentDeclarations
|> List.map (Tuple.mapSecond (listDependenciesOfExpression stackBeforeAddingDeps))
|> List.map (Tuple.mapSecond (listTransitiveDependenciesOfExpression stackBeforeAddingDeps))
|> Dict.fromList

stackWithEnvironmentDeclDeps =
Expand All @@ -527,14 +527,14 @@ inlineApplicationsOfEnvironmentDeclarations stackBeforeAddingDeps environmentDec

Just appliedFunction ->
let
( appliedFunctionParams, appliedFunctionBody ) =
appliedFunctionParsed =
parseFunctionParameters appliedFunction

dependencies =
listDependenciesOfExpression stackWithEnvironmentDeclDeps appliedFunction
listTransitiveDependenciesOfExpression stackWithEnvironmentDeclDeps appliedFunction
in
if
(List.length arguments /= List.length appliedFunctionParams)
(List.length arguments /= List.length appliedFunctionParsed.parameters)
|| Set.member functionName dependencies
then
Nothing
Expand All @@ -543,7 +543,7 @@ inlineApplicationsOfEnvironmentDeclarations stackBeforeAddingDeps environmentDec
let
replacementsDict : Dict.Dict String Expression
replacementsDict =
appliedFunctionParams
appliedFunctionParsed.parameters
|> List.indexedMap
(\paramIndex paramDeconstructions ->
arguments
Expand Down Expand Up @@ -572,7 +572,7 @@ inlineApplicationsOfEnvironmentDeclarations stackBeforeAddingDeps environmentDec
_ ->
Nothing
in
appliedFunctionBody
appliedFunctionParsed.innerExpression
|> transformExpressionWithOptionalReplacement findReplacementForReference
|> inlineApplicationsOfEnvironmentDeclarations stackWithEnvironmentDeclDeps environmentDeclarations
|> Just
Expand Down
Loading

0 comments on commit c33d3fc

Please sign in to comment.