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

Commit

Permalink
feat: function arguments coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Apr 26, 2020
1 parent f19797a commit 766745e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/Capability/Editor/Type.purs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ pinLocations :: FunctionData -> Node -> List.List Pin
pinLocations functionData node = (OutputPin <$ guard (hasOutput node)) <> inputPins functionData

-- Create a location-color pair from a node and data related to it
generateColorPair :: Pin -> Type -> Either ColoringError (Tuple Pin Color)
generateColorPair currentLocation pinType = do
generateColor :: Pin -> Type -> Either ColoringError Color
generateColor currentLocation pinType = do
color <- case pinType of
TVarariable name' -> Right $ RGB shade shade shade
where
shade = seededInt (show name') 100 255
TArrow from to -> combineColors <$> generateColor currentLocation from <*> generateColor currentLocation to
other -> note (UnableToColor other) $ typeToColor other
pure $ Tuple currentLocation color
pure color

-- Createa a typeMap from a node and data about it
generateTypeMap :: (Pin -> Maybe Type) -> FunctionData -> Node -> Either ColoringError (Map.Map Pin Color)
Expand All @@ -89,7 +90,7 @@ generateTypeMap getType functionData node = Map.fromFoldable <$> pairs
pairs =
( sequence
$ List.catMaybes
$ (\pin -> generateColorPair pin <$> getType pin)
$ (\pin -> (map $ Tuple pin) <$> generateColor pin <$> getType pin)
<$> pinLocations functionData node
)

Expand Down
8 changes: 7 additions & 1 deletion src/Component/Editor/HighlightedType.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ highlightedType :: forall h a. (Array (HH.HTML h a) -> HH.HTML h a) -> (Color ->
highlightedType container highlight defaultColor = case _ of
TArrow from to ->
container
[ highlightedType container highlight defaultColor from
[ if isArrow then container [ HH.text "(", result, HH.text ")" ] else result
, HH.text " -> "
, highlightedType container highlight defaultColor to
]
where
isArrow = case from of
TArrow _ _ -> true
_ -> false

result = highlightedType container highlight defaultColor from
TVarariable name' -> highlight (RGB shade shade shade) $ HH.text $ show name'
where
shade = seededInt (show name') 100 255
Expand Down
73 changes: 73 additions & 0 deletions src/Data/Dataflow/Native/Function.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Lunarbox.Data.Dataflow.Native.Function (pipe, identity', const') where

import Lunarbox.Data.Dataflow.Expression (NativeExpression(..))
import Lunarbox.Data.Dataflow.Native.NativeConfig (NativeConfig(..))
import Lunarbox.Data.Dataflow.Runtime (RuntimeValue(..), binaryFunction)
import Lunarbox.Data.Dataflow.Scheme (Scheme(..))
import Lunarbox.Data.Dataflow.Type (TVarName(..), Type(..))
import Lunarbox.Data.Editor.FunctionData (internal)
import Lunarbox.Data.Editor.FunctionName (FunctionName(..))
import Prelude (const, identity, ($))

typePipe :: Scheme
typePipe =
Forall [ input, output ] $ TArrow (TVarariable input)
$ TArrow
(TArrow (TVarariable input) (TVarariable output))
(TVarariable output)
where
input = TVarName "i"

output = TVarName "o"

evalPipe :: RuntimeValue -> RuntimeValue -> RuntimeValue
evalPipe input (Function function) = function input

evalPipe _ _ = Null

pipe :: NativeConfig
pipe =
NativeConfig
{ name: FunctionName "pipe"
, expression: (NativeExpression typePipe $ binaryFunction evalPipe)
, functionData:
internal
[ { name: "input" }
, { name: "function" }
]
}

typeIdentity :: Scheme
typeIdentity = Forall [ input ] $ TArrow (TVarariable input) (TVarariable input)
where
input = TVarName "i"

identity' :: NativeConfig
identity' =
NativeConfig
{ name: FunctionName "identity"
, expression: (NativeExpression typeIdentity $ Function identity)
, functionData:
internal
[ { name: "input" }
]
}

typeConst :: Scheme
typeConst = Forall [ input, ignore ] $ TArrow (TVarariable input) $ TArrow (TVarariable ignore) (TVarariable input)
where
input = TVarName "input"

ignore = TVarName "ignore"

const' :: NativeConfig
const' =
NativeConfig
{ name: FunctionName "pipe"
, expression: (NativeExpression typeConst $ binaryFunction const)
, functionData:
internal
[ { name: "constant value" }
, { name: "ignored value" }
]
}
3 changes: 2 additions & 1 deletion src/Data/Dataflow/Native/Prelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ module Lunarbox.Data.Dataflow.Native.Prelude
) where

import Lunarbox.Data.Dataflow.Native.ControlFlow (if')
import Lunarbox.Data.Dataflow.Native.Function (const', identity', pipe)
import Lunarbox.Data.Dataflow.Native.Math (add)
import Lunarbox.Data.Dataflow.Native.NativeConfig (NativeConfig, loadNativeConfigs)
import Lunarbox.Data.Editor.State (State)

configs :: Array NativeConfig
configs = [ add, if' ]
configs = [ add, if', pipe, identity', const' ]

loadPrelude :: State -> State
loadPrelude = loadNativeConfigs configs

0 comments on commit 766745e

Please sign in to comment.