Skip to content

Commit

Permalink
Improve readability and efficiency in Fir compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Aug 1, 2024
1 parent 80fc364 commit af06927
Showing 1 changed file with 22 additions and 51 deletions.
73 changes: 22 additions & 51 deletions implement/pine/ElmTime/compile-elm-program/src/FirCompiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,19 @@ emitExpressionInDeclarationBlock stackBeforeAddingDeps blockDeclarations mainExp
mainExpressionOuterDependencies =
Set.toList (listTransitiveDependenciesOfExpression stackBefore mainExpression)

usedBlockDeclarationsAndImports : List ( String, Expression )
usedBlockDeclarationsAndImports =
List.foldl
(\declName aggregate ->
case Common.assocListGet declName blockDeclarations of
Just declExpression ->
( declName, declExpression ) :: aggregate

Nothing ->
case Common.assocListGet declName stackBeforeAddingDeps.importedFunctions of
Nothing ->
aggregate
usedBlockDeclarations : List ( String, Expression )
usedBlockDeclarations =
List.filter
(\( declName, _ ) -> List.member declName mainExpressionOuterDependencies)
blockDeclarations

Just ( _, importedVal ) ->
( declName, LiteralExpression importedVal ) :: aggregate
mainDependsOnImport : Bool
mainDependsOnImport =
List.any
(\( importedName, _ ) ->
List.member importedName mainExpressionOuterDependencies
)
[]
mainExpressionOuterDependencies
stackBefore.importedFunctions

mainExpressionAsFunction : DeclBlockFunctionEntry
mainExpressionAsFunction =
Expand All @@ -298,26 +293,21 @@ emitExpressionInDeclarationBlock stackBeforeAddingDeps blockDeclarations mainExp

closureCaptures : List ( String, EnvironmentDeconstructionEntry )
closureCaptures =
List.foldl
(\( declName, deconstruction ) aggregate ->
if List.member declName mainExpressionOuterDependencies then
( declName, deconstruction ) :: aggregate

else
aggregate
List.filter
(\( declName, deconstruction ) ->
List.member declName mainExpressionOuterDependencies
)
[]
stackBefore.environmentDeconstructions
in
case ( mainExprParams, usedBlockDeclarationsAndImports ) of
( [], [] ) ->
case ( mainExprParams, usedBlockDeclarations, mainDependsOnImport ) of
( [], [], False ) ->
emitExpression stackBeforeAddingDeps mainExprInnerExpr

_ ->
case
emitDeclarationBlock
stackBefore
usedBlockDeclarationsAndImports
usedBlockDeclarations
(DeclBlockClosureCaptures closureCaptures)
(DeclBlockRootDeps [ mainExpression ])
of
Expand Down Expand Up @@ -392,17 +382,6 @@ emitDeclarationBlock stackBefore blockDeclarations (DeclBlockClosureCaptures con
dependenciesRelations =
Dict.union availableEmittedDependencies blockDeclarationsDirectDependencies

stackBeforeAvailableDeclarations : List String
stackBeforeAvailableDeclarations =
List.foldl
(\( functionName, _ ) aggregate -> functionName :: aggregate)
(List.foldl
(\( declName, _ ) aggregate -> declName :: aggregate)
[]
stackBefore.environmentDeconstructions
)
stackBefore.environmentFunctions

usedAvailableEmittedForInternals : List ( String, EnvironmentFunctionEntry, Pine.Expression )
usedAvailableEmittedForInternals =
if List.member environmentFunctionPartialApplicationName forwardedDecls then
Expand All @@ -424,7 +403,6 @@ emitDeclarationBlock stackBefore blockDeclarations (DeclBlockClosureCaptures con
rootDependencies
stackBefore
dependenciesRelations
stackBeforeAvailableDeclarations

usedAvailableEmitted : List ( String, EnvironmentFunctionEntry, Pine.Expression )
usedAvailableEmitted =
Expand Down Expand Up @@ -721,9 +699,8 @@ emittedImportsFromRoots :
List Expression
-> EmitStack
-> Dict.Dict String (Set.Set String)
-> List String
-> List ( String, EnvironmentFunctionEntry, Pine.Expression )
emittedImportsFromRoots rootDependencies emitStack dependenciesRelations stackBeforeAvailableDeclarations =
emittedImportsFromRoots rootDependencies emitStack dependenciesRelations =
case emitStack.importedFunctions of
[] ->
{-
Expand Down Expand Up @@ -756,11 +733,7 @@ emittedImportsFromRoots rootDependencies emitStack dependenciesRelations stackBe
aggregate

Just ( availableEmitted, emittedValue ) ->
if List.member depName stackBeforeAvailableDeclarations then
aggregate

else
( depName, availableEmitted, Pine.LiteralExpression emittedValue ) :: aggregate
( depName, availableEmitted, Pine.LiteralExpression emittedValue ) :: aggregate
)
[]
allDependencies
Expand Down Expand Up @@ -811,12 +784,10 @@ expressionNeedsAdaptiveApplication expression =
True

DeclarationBlockExpression declarations innerExpression ->
List.foldl
(\( _, decl ) aggregate ->
aggregate || expressionNeedsAdaptiveApplication decl
)
(expressionNeedsAdaptiveApplication innerExpression)
List.any
(\( _, decl ) -> expressionNeedsAdaptiveApplication decl)
declarations
|| expressionNeedsAdaptiveApplication innerExpression

ReferenceExpression _ ->
False
Expand Down

0 comments on commit af06927

Please sign in to comment.