Skip to content

Commit

Permalink
WIP: start to add type info for builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
johnchildren committed May 20, 2018
1 parent 9617c90 commit 7065a91
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import Nix.Parser
import Nix.Render
import Nix.Scope
import Nix.Thunk
import Nix.Type.Type as T
import Nix.Utils
import Nix.Value
import Nix.XML
Expand All @@ -104,14 +105,15 @@ builtins = do

fullBuiltinsList = map go <$> builtinsList
where
go b@(Builtin TopLevel _) = b
go (Builtin Normal (name, builtin)) =
Builtin TopLevel ("__" <> name, builtin)
go b@(Builtin TopLevel _ _) = b
go (Builtin Normal (name, builtin) _ p) =
Builtin TopLevel ("__" <> name, builtin) p

data BuiltinType = Normal | TopLevel
data Builtin m = Builtin
{ _kind :: BuiltinType
, mapping :: (Text, NThunk m)
, _type :: Type
}

valueThunk :: forall e m. MonadNix e m => NValue m -> NThunk m
Expand All @@ -123,20 +125,21 @@ force' = force ?? pure
builtinsList :: forall e m. MonadNix e m => m [ Builtin m ]
builtinsList = sequence [
do version <- toValue ("2.0" :: Text)
pure $ Builtin Normal ("nixVersion", version)
pure $ Builtin Normal ("nixVersion", version) typeString

, do version <- toValue (5 :: Int)
pure $ Builtin Normal ("langVersion", version)

, add0 Normal "nixPath" nixPath
, add TopLevel "abort" throw_ -- for now
, add2 Normal "add" add_
, add2 Normal "all" all_
, add2 Normal "any" any_
, add Normal "attrNames" attrNames
, add Normal "attrValues" attrValues
, add TopLevel "baseNameOf" baseNameOf
, add2 Normal "catAttrs" catAttrs
pure $ Builtin Normal ("langVersion", version) typeInt

, add0 Normal "nixPath" nixPath typePath
, add TopLevel "abort" throw_ typeNull -- throw_ for now
, add2 Normal "add" add_ (TMany [typeInt :~> typeInt, typeInt :~> typeFloat, typeFloat :~> typeInt, typeFloat :~> typeFloat])
, add2 Normal "all" all_ (typeFun [TVar (TV "a"), typeBool] :~> T.TList [TVar (TV "a")] :~> typeBool)
, add2 Normal "any" any_ (typeFun [TVar (TV "a"), typeBool] :~> T.TList [TVar (TV "a")] :~> typeBool)
, add Normal "attrNames" attrNames (T.TSet True (AttrSet (TVar (TV "a"))) :~> T.TList [typeString])
, add Normal "attrValues" attrValues (T.TSet True (AttrSet (TVar (TV "a"))) :~> T.TList [AttrSet (TVar (TV "a"))])
, add TopLevel "baseNameOf" baseNameOf (typeString :~> typeString)
{-
, add2 Normal "catAttrs" catAttrs ()
, add2 Normal "compareVersions" compareVersions_
, add Normal "concatLists" concatLists
, add' Normal "concatStringsSep" (arity2 Text.intercalate)
Expand Down Expand Up @@ -249,23 +252,24 @@ builtinsList = sequence [
, add Normal "unsafeDiscardStringContext" unsafeDiscardStringContext
, add2 Normal "unsafeGetAttrPos" unsafeGetAttrPos
, add Normal "valueSize" getRecursiveSize
-}
]
where
wrap t n f = Builtin t (n, f)
wrap t n p f = Builtin t (n, f) p

arity1 f = Prim . pure . f
arity2 f = ((Prim . pure) .) . f

mkThunk n = thunk . withFrame Info
(ErrorCall $ "While calling builtin " ++ Text.unpack n ++ "\n")

add0 t n v = wrap t n <$> mkThunk n v
add t n v = wrap t n <$> mkThunk n (builtin (Text.unpack n) v)
add2 t n v = wrap t n <$> mkThunk n (builtin2 (Text.unpack n) v)
add3 t n v = wrap t n <$> mkThunk n (builtin3 (Text.unpack n) v)
add0 t n v p = wrap t n p <$> mkThunk n v
add t n v p = wrap t n p <$> mkThunk n (builtin (Text.unpack n) v)
add2 t n v p = wrap t n p <$> mkThunk n (builtin2 (Text.unpack n) v)
add3 t n v p = wrap t n p <$> mkThunk n (builtin3 (Text.unpack n) v)

add' :: ToBuiltin m a => BuiltinType -> Text -> a -> m (Builtin m)
add' t n v = wrap t n <$> mkThunk n (toBuiltin (Text.unpack n) v)
add' :: ToBuiltin m a => BuiltinType -> Text -> a -> Type -> m (Builtin m)
add' t n v p = wrap t n p <$> mkThunk n (toBuiltin (Text.unpack n) v)

-- Primops

Expand Down

0 comments on commit 7065a91

Please sign in to comment.