This is the main module for working with Leaf scripts.
To run this script in Elm you need to call Leaf.run.
import Leaf exposing (Value(..))
"\"Hello World\""
|> Leaf.run Dict.empty
--> Ok (StringVal "Hello World",Dict.empty)Every Leaf script evaluates to a Value
Evaluates a Leaf script. The dictionary may contain fields that can be called from inside the Leaf script.
import Leaf exposing (Value(..))
import Dict exposing (Dict)
context : Dict String Field
context =
[ StringVal "World" |> Leaf.field "name"
, (\s2 s1 -> StringVal (s1 ++ s2) )
|> Leaf.binaryFun
(Leaf.typed Leaf.asString)
(Leaf.typed Leaf.asString)
|> Leaf.field "append"
]
|> Dict.fromList
"\"Hello \" .append name"
|> Leaf.run context
--> Ok (StringVal "Hello World",context)You can also evaluate context sensitive Leaf scripts.
import Leaf exposing (Value(..))
import Dict exposing (Dict)
import Leaf.Core as Core
context : Dict String Field
context =
[ StringVal "World" |> Leaf.field "name"
, (\s2 s1 -> StringVal (s1 ++ s2))
|> Leaf.binaryFun (Leaf.typed Leaf.asString)
(Leaf.typed Leaf.asString)
|> Leaf.field "append"
]
|> Dict.fromList
|> Leaf.addExposed Core.package
"\"Hello \" .append name"
|> Leaf.run context
--> Ok (StringVal "Hello World",context)Internal type for fields
Constructs a context field. Use this in combination with Dict.fromList
Constructs a mutable context field. Use this in combination with Dict.fromList
Turns an Elm function with one argument into a Leaf extension
import Leaf.Core as Core
(Core.isNull >> BoolVal)
|> Leaf.unaryFun Leaf.untyped
|> Leaf.field "isNull"Turns an Elm function with two argument into a Leaf extension
import Leaf.Core as Core
import Leaf exposing (Field)
(\v1 v2 -> Core.equal v1 v2 |> BoolVal)
|> Leaf.binaryFun Leaf.untyped Leaf.untyped
|> Leaf.field "equal"Turns an Elm function with three argument into a Leaf extension
import Leaf.Core as Core
import Leaf exposing (Field)
(\v1 v2 -> Core.equal v1 v2 |> BoolVal)
|> Leaf.binaryFun Leaf.untyped Leaf.untyped
|> Leaf.field "equal"States that an extension function takes an untyped value
import Leaf.Core as Core
(Core.isNull >> BoolVal)
|> Leaf.unaryFun Leaf.untyped
|> Leaf.field "isNull"States that an extension function takes a typed value
import Leaf.Core as Core
Core.if_
|> Leaf.trinaryFun (Leaf.typed Leaf.asBool) Leaf.untyped Leaf.untyped
|> Leaf.field "if"converts a Leaf value into an Elm maybe type
converts a Leaf list into an Elm list type
converts a Leaf functions into an Elm tuple
converts a Leaf extension into an Elm function
Adds a package to a context and applies the naming convention Package::functionName.
Adds a package and exposes all function. This should only be used for essential packages.
import Dict
addExposed
--> Dict.union
A leaf script is internally translated into an Exp