Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
feat: expression optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Apr 23, 2020
1 parent 410cb4b commit 57e4175
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
41 changes: 41 additions & 0 deletions src/Data/Dataflow/Expression.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module Lunarbox.Data.Dataflow.Expression
, sumarizeExpression
, inputs
, wrap
, optimize
, removeWrappers
, wrapWith
, wrappers
) where

import Prelude
Expand Down Expand Up @@ -185,3 +189,40 @@ printSource = printRawExpression (\e -> printSource e)
-- Wrap an expression in another expression with a custom location
wrap :: forall l. l -> Expression l -> Expression l
wrap location = Chain location <<< pure

-- Unwrap an expression as much as possible
removeWrappers :: forall l. Expression l -> Expression l
removeWrappers (Chain _ (expression : Nil)) = removeWrappers expression

removeWrappers expression = expression

-- Collect all the locations something is wrapped in
wrappers :: forall l. Expression l -> List l
wrappers (Chain location (expression : Nil)) = location : wrappers expression

wrappers _ = Nil

-- Wrap an expression with a list of locations
wrapWith :: forall l. List l -> Expression l -> Expression l
wrapWith (location : locations') = wrapWith locations' <<< wrap location

wrapWith Nil = identity

-- Optimize an expression
optimize :: forall l. Expression l -> Expression l
optimize expression@(Let location name value body) = case removeWrappers body of
Variable location' name'
| name == name' -> wrapWith (wrappers body) $ wrap location' $ optimize value
_ -> Let location name (optimize value) $ optimize body

optimize (FunctionCall location calee argument) = FunctionCall location (optimize calee) $ optimize argument

optimize (Lambda location argument body) = Lambda location argument $ optimize body

optimize (If location condition then' else') = If location (optimize condition) (optimize then') $ optimize else'

optimize (FixPoint location body) = FixPoint location $ optimize body

optimize (Chain location expressions) = Chain location $ optimize <$> expressions

optimize expression = expression
16 changes: 10 additions & 6 deletions src/Data/Editor/Node.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Lens.Record (prop)
import Data.List (List(..), foldl, mapWithIndex, (!!))
import Data.Maybe (Maybe(..), maybe)
import Data.Symbol (SProxy(..))
import Lunarbox.Data.Dataflow.Class.Expressible (nullExpr)
import Lunarbox.Data.Dataflow.Expression (Expression(..), VarName(..), wrap)
import Lunarbox.Data.Editor.ExtendedLocation (ExtendedLocation(..), nothing)
import Lunarbox.Data.Editor.FunctionName (FunctionName)
Expand Down Expand Up @@ -63,24 +64,27 @@ compileNode nodes id child =
InputNode -> inputNode id child
OutputNode outputId ->
outputNode id case outputId of
Just outputId' -> Variable (Location outputId') $ VarName $ show outputId'
Just outputId' -> Variable Nowhere $ VarName $ show outputId'
Nothing -> nothing
ComplexNode { inputs, function } -> Let Nowhere name value child
where
name = VarName $ show id

calee = Variable (Location id) $ VarName $ show function
calee = Variable Nowhere $ VarName $ show function

arguments =
mapWithIndex
( \index id' ->
wrap (DeepLocation id $ InputPin index) case id' of
Just id'' -> Variable Nowhere $ VarName $ show id''
Nothing -> nothing
let
location = DeepLocation id $ InputPin index
in
case id' of
Just id'' -> Variable location $ VarName $ show id''
Nothing -> nullExpr location
)
inputs

value = functionCall (DeepLocation id OutputPin) calee arguments
value = wrap (Location id) $ functionCall (DeepLocation id OutputPin) calee arguments

-- Lenses
_ComplexNode :: Prism' Node ComplexNodeData
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Editor/Project.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Data.Newtype (class Newtype)
import Data.Set as Set
import Data.Symbol (SProxy(..))
import Data.Unfoldable (class Unfoldable)
import Lunarbox.Data.Dataflow.Expression (Expression)
import Lunarbox.Data.Dataflow.Expression (Expression, optimize)
import Lunarbox.Data.Dataflow.Graph (compileGraph)
import Lunarbox.Data.Editor.DataflowFunction (DataflowFunction(..), _VisualFunction, compileDataflowFunction)
import Lunarbox.Data.Editor.FunctionName (FunctionName(..))
Expand All @@ -49,7 +49,7 @@ _ProjectMain :: Lens' Project FunctionName
_ProjectMain = newtypeIso <<< prop (SProxy :: _ "main")

compileProject :: Project -> Expression Location
compileProject (Project { functions, main }) = compileGraph compileDataflowFunction functions main
compileProject (Project { functions, main }) = optimize $ compileGraph compileDataflowFunction functions main

createEmptyFunction :: NodeId -> DataflowFunction
createEmptyFunction id =
Expand Down

0 comments on commit 57e4175

Please sign in to comment.