2018-04-09 09:52:10 +02:00
|
|
|
{-# LANGUAGE AllowAmbiguousTypes #-}
|
|
|
|
{-# LANGUAGE ApplicativeDo #-}
|
2018-04-29 02:12:32 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2018-04-09 09:52:10 +02:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
2018-04-11 06:01:48 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2018-04-09 09:52:10 +02:00
|
|
|
{-# LANGUAGE PartialTypeSignatures #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2018-04-18 06:32:20 +02:00
|
|
|
{-# LANGUAGE TupleSections #-}
|
2018-04-09 09:52:10 +02:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2018-04-11 06:01:48 +02:00
|
|
|
{-# OPTIONS_GHC -Wno-missing-signatures #-}
|
2018-04-09 09:52:10 +02:00
|
|
|
{-# OPTIONS_GHC -Wno-orphans #-}
|
2018-04-11 06:01:48 +02:00
|
|
|
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
|
2018-04-09 09:52:10 +02:00
|
|
|
|
|
|
|
module Nix.Exec where
|
|
|
|
|
2019-03-17 22:47:38 +01:00
|
|
|
import Prelude hiding ( putStr
|
|
|
|
, putStrLn
|
|
|
|
, print
|
|
|
|
)
|
2018-11-16 21:28:55 +01:00
|
|
|
|
2018-04-14 04:29:18 +02:00
|
|
|
import Control.Applicative
|
2018-04-09 09:52:10 +02:00
|
|
|
import Control.Monad
|
2019-03-17 22:47:38 +01:00
|
|
|
import Control.Monad.Catch hiding ( catchJust )
|
2018-04-11 06:01:48 +02:00
|
|
|
import Control.Monad.Fix
|
2018-04-21 19:26:42 +02:00
|
|
|
import Control.Monad.Reader
|
2019-03-17 22:47:38 +01:00
|
|
|
import Control.Monad.Trans.Reader ( ReaderT(..) )
|
2018-04-11 06:01:48 +02:00
|
|
|
import Data.Fix
|
2019-03-17 22:47:38 +01:00
|
|
|
import qualified Data.HashMap.Lazy as M
|
2018-04-11 06:01:48 +02:00
|
|
|
import Data.List
|
2019-03-17 22:47:38 +01:00
|
|
|
import qualified Data.List.NonEmpty as NE
|
|
|
|
import Data.Text ( Text )
|
|
|
|
import qualified Data.Text as Text
|
2018-11-17 05:51:18 +01:00
|
|
|
import Data.Text.Prettyprint.Doc
|
2018-04-24 11:14:27 +02:00
|
|
|
import Data.Typeable
|
2018-04-09 09:52:10 +02:00
|
|
|
import Nix.Atoms
|
2019-03-15 06:00:23 +01:00
|
|
|
import Nix.Cited
|
2018-04-14 03:09:12 +02:00
|
|
|
import Nix.Convert
|
2018-04-11 06:01:48 +02:00
|
|
|
import Nix.Effects
|
2019-03-17 22:47:38 +01:00
|
|
|
import Nix.Eval as Eval
|
2018-04-09 09:52:10 +02:00
|
|
|
import Nix.Expr
|
2018-04-24 11:14:27 +02:00
|
|
|
import Nix.Frames
|
2018-04-18 02:25:59 +02:00
|
|
|
import Nix.Options
|
2018-04-09 09:52:10 +02:00
|
|
|
import Nix.Pretty
|
2018-04-24 11:14:27 +02:00
|
|
|
import Nix.Render
|
2018-04-11 06:01:48 +02:00
|
|
|
import Nix.Scope
|
2019-03-16 01:59:38 +01:00
|
|
|
import Nix.String
|
2019-03-22 23:16:01 +01:00
|
|
|
import Nix.String.Coerce
|
2018-04-09 09:52:10 +02:00
|
|
|
import Nix.Thunk
|
|
|
|
import Nix.Utils
|
|
|
|
import Nix.Value
|
2019-03-18 06:44:22 +01:00
|
|
|
import Nix.Value.Equal
|
2019-03-18 19:41:46 +01:00
|
|
|
import Nix.Value.Monad
|
2018-05-13 23:17:55 +02:00
|
|
|
#ifdef MIN_VERSION_pretty_show
|
2018-05-09 23:39:27 +02:00
|
|
|
import qualified Text.Show.Pretty as PS
|
2018-05-13 23:17:55 +02:00
|
|
|
#endif
|
2018-04-21 07:36:40 +02:00
|
|
|
|
2018-04-29 03:38:43 +02:00
|
|
|
#ifdef MIN_VERSION_ghc_datasize
|
2018-04-29 03:29:37 +02:00
|
|
|
#if MIN_VERSION_ghc_datasize(0,2,0) && __GLASGOW_HASKELL__ >= 804
|
2018-04-29 02:12:32 +02:00
|
|
|
import GHC.DataSize
|
|
|
|
#endif
|
2018-04-29 03:38:43 +02:00
|
|
|
#endif
|
2018-04-29 02:12:32 +02:00
|
|
|
|
2019-03-18 02:43:23 +01:00
|
|
|
type MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
= ( HasCitations m (NValue t f m) t
|
2019-03-19 05:47:43 +01:00
|
|
|
, HasCitations1 m (NValue t f m) f
|
|
|
|
, MonadDataContext f m
|
|
|
|
)
|
2019-03-17 22:47:38 +01:00
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
nvConstantP
|
2019-03-18 19:41:46 +01:00
|
|
|
:: MonadCited t f m => Provenance m (NValue t f m) -> NAtom -> NValue t f m
|
2019-03-17 22:47:38 +01:00
|
|
|
nvConstantP p x = addProvenance p (nvConstant x)
|
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
nvStrP
|
|
|
|
:: MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Provenance m (NValue t f m)
|
2019-03-18 06:46:15 +01:00
|
|
|
-> NixString
|
|
|
|
-> NValue t f m
|
2019-03-17 22:47:38 +01:00
|
|
|
nvStrP p ns = addProvenance p (nvStr ns)
|
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
nvPathP
|
2019-03-19 05:47:43 +01:00
|
|
|
:: MonadCited t f m => Provenance m (NValue t f m) -> FilePath -> NValue t f m
|
|
|
|
nvPathP p x = addProvenance p (nvPath x)
|
|
|
|
|
|
|
|
nvListP
|
2019-03-18 06:46:15 +01:00
|
|
|
:: MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Provenance m (NValue t f m)
|
2019-03-19 05:47:43 +01:00
|
|
|
-> [NValue t f m]
|
2019-03-18 06:46:15 +01:00
|
|
|
-> NValue t f m
|
2019-03-17 22:47:38 +01:00
|
|
|
nvListP p l = addProvenance p (nvList l)
|
|
|
|
|
|
|
|
nvSetP
|
2019-03-18 02:43:23 +01:00
|
|
|
:: MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Provenance m (NValue t f m)
|
|
|
|
-> AttrSet (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> AttrSet SourcePos
|
|
|
|
-> NValue t f m
|
|
|
|
nvSetP p s x = addProvenance p (nvSet s x)
|
|
|
|
|
|
|
|
nvClosureP
|
2019-03-18 02:43:23 +01:00
|
|
|
:: MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Provenance m (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> Params ()
|
2019-03-18 19:41:46 +01:00
|
|
|
-> (NValue t f m -> m (NValue t f m))
|
2019-03-17 22:47:38 +01:00
|
|
|
-> NValue t f m
|
|
|
|
nvClosureP p x f = addProvenance p (nvClosure x f)
|
|
|
|
|
|
|
|
nvBuiltinP
|
2019-03-18 02:43:23 +01:00
|
|
|
:: MonadCited t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Provenance m (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> String
|
2019-03-18 19:41:46 +01:00
|
|
|
-> (NValue t f m -> m (NValue t f m))
|
2019-03-17 22:47:38 +01:00
|
|
|
-> NValue t f m
|
|
|
|
nvBuiltinP p name f = addProvenance p (nvBuiltin name f)
|
|
|
|
|
|
|
|
type MonadCitedThunks t f m
|
2019-03-18 22:01:48 +01:00
|
|
|
= ( MonadThunk t m (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
, MonadDataErrorContext t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
, HasCitations m (NValue t f m) t
|
|
|
|
, HasCitations1 m (NValue t f m) f
|
2019-03-17 22:47:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MonadNix e t f m
|
|
|
|
= ( Has e SrcSpan
|
|
|
|
, Has e Options
|
2019-03-18 19:41:46 +01:00
|
|
|
, Scoped (NValue t f m) m
|
2019-03-17 22:47:38 +01:00
|
|
|
, Framed e m
|
|
|
|
, MonadFix m
|
|
|
|
, MonadCatch m
|
|
|
|
, MonadThrow m
|
|
|
|
, Alternative m
|
|
|
|
, MonadEffects t f m
|
|
|
|
, MonadCitedThunks t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
, MonadValue (NValue t f m) m
|
2019-03-17 22:47:38 +01:00
|
|
|
)
|
2018-04-09 09:52:10 +02:00
|
|
|
|
2019-03-15 06:00:23 +01:00
|
|
|
data ExecFrame t f m = Assertion SrcSpan (NValue t f m)
|
2018-04-24 20:12:20 +02:00
|
|
|
deriving (Show, Typeable)
|
|
|
|
|
2019-03-15 06:00:23 +01:00
|
|
|
instance MonadDataErrorContext t f m => Exception (ExecFrame t f m)
|
2018-04-24 20:12:20 +02:00
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
nverr :: forall e t f s m a . (MonadNix e t f m, Exception s) => s -> m a
|
2019-03-15 06:00:23 +01:00
|
|
|
nverr = evalError @(NValue t f m)
|
2018-04-09 09:52:10 +02:00
|
|
|
|
2019-03-17 22:47:38 +01:00
|
|
|
currentPos :: forall e m . (MonadReader e m, Has e SrcSpan) => m SrcSpan
|
2018-05-06 09:40:08 +02:00
|
|
|
currentPos = asks (view hasLens)
|
2018-04-25 22:00:41 +02:00
|
|
|
|
2018-04-25 08:09:43 +02:00
|
|
|
wrapExprLoc :: SrcSpan -> NExprLocF r -> NExprLoc
|
|
|
|
wrapExprLoc span x = Fix (Fix (NSym_ span "<?>") <$ x)
|
|
|
|
|
2019-03-17 23:30:20 +01:00
|
|
|
instance MonadNix e t f m => MonadEval (NValue t f m) m where
|
2019-03-17 22:47:38 +01:00
|
|
|
freeVariable var =
|
|
|
|
nverr @e @t @f
|
|
|
|
$ ErrorCall
|
|
|
|
$ "Undefined variable '"
|
|
|
|
++ Text.unpack var
|
|
|
|
++ "'"
|
|
|
|
|
|
|
|
synHole name = do
|
|
|
|
span <- currentPos
|
|
|
|
scope <- currentScopes
|
|
|
|
evalError @(NValue t f m) $ SynHole $ SynHoleInfo
|
|
|
|
{ _synHoleInfo_expr = Fix $ NSynHole_ span name
|
|
|
|
, _synHoleInfo_scope = scope
|
|
|
|
}
|
|
|
|
|
|
|
|
attrMissing ks Nothing =
|
|
|
|
evalError @(NValue t f m)
|
|
|
|
$ ErrorCall
|
|
|
|
$ "Inheriting unknown attribute: "
|
|
|
|
++ intercalate "." (map Text.unpack (NE.toList ks))
|
|
|
|
|
2019-03-27 05:21:12 +01:00
|
|
|
attrMissing ks (Just s) =
|
2019-03-17 22:47:38 +01:00
|
|
|
evalError @(NValue t f m)
|
|
|
|
$ ErrorCall
|
|
|
|
$ "Could not look up attribute "
|
|
|
|
++ intercalate "." (map Text.unpack (NE.toList ks))
|
|
|
|
++ " in "
|
2019-03-27 05:21:12 +01:00
|
|
|
++ show (prettyNValue s)
|
2019-03-17 22:47:38 +01:00
|
|
|
|
|
|
|
evalCurPos = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span@(SrcSpan delta _) <- currentPos
|
2019-03-18 02:43:23 +01:00
|
|
|
addProvenance @_ @_ @(NValue t f m)
|
2019-03-18 06:46:15 +01:00
|
|
|
(Provenance scope (NSym_ span "__curPos"))
|
2019-03-17 22:47:38 +01:00
|
|
|
<$> toValue delta
|
|
|
|
|
|
|
|
evaledSym name val = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
2019-03-18 02:43:23 +01:00
|
|
|
pure $ addProvenance @_ @_ @(NValue t f m)
|
2019-03-18 06:46:15 +01:00
|
|
|
(Provenance scope (NSym_ span name))
|
|
|
|
val
|
2019-03-17 22:47:38 +01:00
|
|
|
|
|
|
|
evalConstant c = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
pure $ nvConstantP (Provenance scope (NConstant_ span c)) c
|
|
|
|
|
|
|
|
evalString = assembleString >=> \case
|
|
|
|
Just ns -> do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
pure $ nvStrP
|
|
|
|
(Provenance
|
|
|
|
scope
|
|
|
|
(NStr_ span (DoubleQuoted [Plain (hackyStringIgnoreContext ns)]))
|
|
|
|
)
|
|
|
|
ns
|
|
|
|
Nothing -> nverr $ ErrorCall "Failed to assemble string"
|
|
|
|
|
|
|
|
evalLiteralPath p = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
nvPathP (Provenance scope (NLiteralPath_ span p))
|
|
|
|
<$> makeAbsolutePath @t @f @m p
|
|
|
|
|
|
|
|
evalEnvPath p = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
nvPathP (Provenance scope (NEnvPath_ span p)) <$> findEnvPath @t @f @m p
|
|
|
|
|
|
|
|
evalUnary op arg = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
execUnaryOp scope span op arg
|
|
|
|
|
|
|
|
evalBinary op larg rarg = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
execBinaryOp scope span op larg rarg
|
|
|
|
|
|
|
|
evalWith c b = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
(\b -> addProvenance (Provenance scope (NWith_ span Nothing (Just b))) b)
|
|
|
|
<$> evalWithAttrSet c b
|
|
|
|
|
|
|
|
evalIf c t f = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
fromValue c >>= \b -> if b
|
|
|
|
then
|
|
|
|
(\t -> addProvenance
|
|
|
|
(Provenance scope (NIf_ span (Just c) (Just t) Nothing))
|
|
|
|
t
|
|
|
|
)
|
|
|
|
<$> t
|
|
|
|
else
|
|
|
|
(\f -> addProvenance
|
|
|
|
(Provenance scope (NIf_ span (Just c) Nothing (Just f)))
|
|
|
|
f
|
|
|
|
)
|
|
|
|
<$> f
|
|
|
|
|
|
|
|
evalAssert c body = fromValue c >>= \b -> do
|
|
|
|
span <- currentPos
|
|
|
|
if b
|
|
|
|
then do
|
2018-04-22 23:32:55 +02:00
|
|
|
scope <- currentScopes
|
2019-03-17 22:47:38 +01:00
|
|
|
(\b ->
|
|
|
|
addProvenance (Provenance scope (NAssert_ span (Just c) (Just b))) b
|
|
|
|
)
|
|
|
|
<$> body
|
|
|
|
else nverr $ Assertion span c
|
|
|
|
|
|
|
|
evalApp f x = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
|
|
|
addProvenance (Provenance scope (NBinary_ span NApp (Just f) Nothing))
|
2019-03-18 19:41:46 +01:00
|
|
|
<$> (callFunc f =<< defer x)
|
2019-03-17 22:47:38 +01:00
|
|
|
|
|
|
|
evalAbs p k = do
|
|
|
|
scope <- currentScopes
|
|
|
|
span <- currentPos
|
2019-03-19 05:47:43 +01:00
|
|
|
pure $ nvClosureP (Provenance scope (NAbs_ span (Nothing <$ p) Nothing))
|
|
|
|
(void p)
|
|
|
|
(\arg -> snd <$> k (pure arg) (\_ b -> ((), ) <$> b))
|
2019-03-17 22:47:38 +01:00
|
|
|
|
|
|
|
evalError = throwError
|
2018-04-09 09:52:10 +02:00
|
|
|
|
|
|
|
infixl 1 `callFunc`
|
2019-03-17 22:47:38 +01:00
|
|
|
callFunc
|
|
|
|
:: forall e t f m
|
|
|
|
. MonadNix e t f m
|
|
|
|
=> NValue t f m
|
2019-03-18 19:41:46 +01:00
|
|
|
-> NValue t f m
|
2019-03-17 22:47:38 +01:00
|
|
|
-> m (NValue t f m)
|
2019-03-18 19:41:46 +01:00
|
|
|
callFunc fun arg = demand fun $ \fun' -> do
|
2019-03-17 22:47:38 +01:00
|
|
|
frames :: Frames <- asks (view hasLens)
|
|
|
|
when (length frames > 2000) $ throwError $ ErrorCall
|
|
|
|
"Function call stack exhausted"
|
2019-03-18 19:41:46 +01:00
|
|
|
case fun' of
|
2019-03-17 22:47:38 +01:00
|
|
|
NVClosure params f -> do
|
|
|
|
traceM $ "callFunc:NVFunction taking " ++ show params
|
2019-03-18 19:41:46 +01:00
|
|
|
f arg
|
2019-03-17 22:47:38 +01:00
|
|
|
NVBuiltin name f -> do
|
|
|
|
span <- currentPos
|
2019-03-18 19:41:46 +01:00
|
|
|
withFrame Info (Calling @m @t name span) (f arg)
|
2019-03-17 22:47:38 +01:00
|
|
|
s@(NVSet m _) | Just f <- M.lookup "__functor" m -> do
|
|
|
|
traceM "callFunc:__functor"
|
2019-03-18 19:41:46 +01:00
|
|
|
demand f $ (`callFunc` s) >=> (`callFunc` arg)
|
2019-03-17 22:47:38 +01:00
|
|
|
x -> throwError $ ErrorCall $ "Attempt to call non-function: " ++ show x
|
|
|
|
|
|
|
|
execUnaryOp
|
2019-03-18 02:43:23 +01:00
|
|
|
:: (Framed e m, MonadCited t f m, Show t)
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Scopes m (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> SrcSpan
|
|
|
|
-> NUnaryOp
|
|
|
|
-> NValue t f m
|
|
|
|
-> m (NValue t f m)
|
2018-04-22 23:32:55 +02:00
|
|
|
execUnaryOp scope span op arg = do
|
2019-03-17 22:47:38 +01:00
|
|
|
traceM "NUnary"
|
|
|
|
case arg of
|
|
|
|
NVConstant c -> case (op, c) of
|
|
|
|
(NNeg, NInt i ) -> unaryOp $ NInt (-i)
|
|
|
|
(NNeg, NFloat f) -> unaryOp $ NFloat (-f)
|
|
|
|
(NNot, NBool b ) -> unaryOp $ NBool (not b)
|
|
|
|
_ ->
|
|
|
|
throwError
|
|
|
|
$ ErrorCall
|
|
|
|
$ "unsupported argument type for unary operator "
|
|
|
|
++ show op
|
|
|
|
x ->
|
|
|
|
throwError
|
|
|
|
$ ErrorCall
|
|
|
|
$ "argument to unary operator"
|
|
|
|
++ " must evaluate to an atomic type: "
|
|
|
|
++ show x
|
|
|
|
where
|
|
|
|
unaryOp = pure . nvConstantP (Provenance scope (NUnary_ span op (Just arg)))
|
2018-04-09 09:52:10 +02:00
|
|
|
|
|
|
|
execBinaryOp
|
2019-03-17 22:47:38 +01:00
|
|
|
:: forall e t f m
|
2019-03-17 23:30:20 +01:00
|
|
|
. (MonadNix e t f m, MonadEval (NValue t f m) m)
|
2019-03-18 19:41:46 +01:00
|
|
|
=> Scopes m (NValue t f m)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> SrcSpan
|
|
|
|
-> NBinaryOp
|
|
|
|
-> NValue t f m
|
|
|
|
-> m (NValue t f m)
|
|
|
|
-> m (NValue t f m)
|
2018-04-22 23:32:55 +02:00
|
|
|
|
2019-03-18 19:41:46 +01:00
|
|
|
execBinaryOp scope span NOr larg rarg = fromValue larg >>= \l -> if l
|
2019-03-17 22:47:38 +01:00
|
|
|
then orOp Nothing True
|
2019-03-18 19:41:46 +01:00
|
|
|
else rarg >>= \rval -> fromValue @Bool rval >>= orOp (Just rval)
|
2019-03-17 22:47:38 +01:00
|
|
|
where
|
|
|
|
orOp r b = pure $ nvConstantP
|
|
|
|
(Provenance scope (NBinary_ span NOr (Just larg) r))
|
|
|
|
(NBool b)
|
|
|
|
|
2019-03-18 19:41:46 +01:00
|
|
|
execBinaryOp scope span NAnd larg rarg = fromValue larg >>= \l -> if l
|
|
|
|
then rarg >>= \rval -> fromValue @Bool rval >>= andOp (Just rval)
|
2019-03-17 22:47:38 +01:00
|
|
|
else andOp Nothing False
|
|
|
|
where
|
|
|
|
andOp r b = pure $ nvConstantP
|
|
|
|
(Provenance scope (NBinary_ span NAnd (Just larg) r))
|
|
|
|
(NBool b)
|
2018-04-09 09:52:10 +02:00
|
|
|
|
2018-04-22 23:32:55 +02:00
|
|
|
execBinaryOp scope span op lval rarg = do
|
2019-03-17 22:47:38 +01:00
|
|
|
rval <- rarg
|
2019-03-18 19:41:46 +01:00
|
|
|
let bin :: (Provenance m (NValue t f m) -> a) -> a
|
2019-03-17 22:47:38 +01:00
|
|
|
bin f = f (Provenance scope (NBinary_ span op (Just lval) (Just rval)))
|
|
|
|
toBool = pure . bin nvConstantP . NBool
|
|
|
|
case (lval, rval) of
|
|
|
|
(NVConstant lc, NVConstant rc) -> case (op, lc, rc) of
|
|
|
|
(NEq , _, _) -> toBool =<< valueEqM lval rval
|
|
|
|
(NNEq, _, _) -> toBool . not =<< valueEqM lval rval
|
|
|
|
(NLt , l, r) -> toBool $ l < r
|
|
|
|
(NLte, l, r) -> toBool $ l <= r
|
|
|
|
(NGt , l, r) -> toBool $ l > r
|
|
|
|
(NGte, l, r) -> toBool $ l >= r
|
|
|
|
(NAnd, _, _) ->
|
|
|
|
nverr $ ErrorCall "should be impossible: && is handled above"
|
|
|
|
(NOr, _, _) ->
|
|
|
|
nverr $ ErrorCall "should be impossible: || is handled above"
|
|
|
|
(NPlus , l , r ) -> numBinOp bin (+) l r
|
|
|
|
(NMinus, l , r ) -> numBinOp bin (-) l r
|
|
|
|
(NMult , l , r ) -> numBinOp bin (*) l r
|
|
|
|
(NDiv , l , r ) -> numBinOp' bin div (/) l r
|
|
|
|
(NImpl , NBool l, NBool r) -> toBool $ not l || r
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVStr ls, NVStr rs) -> case op of
|
|
|
|
NPlus -> pure $ bin nvStrP (ls `principledStringMappend` rs)
|
|
|
|
NEq -> toBool =<< valueEqM lval rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval rval
|
|
|
|
NLt -> toBool $ ls < rs
|
|
|
|
NLte -> toBool $ ls <= rs
|
|
|
|
NGt -> toBool $ ls > rs
|
|
|
|
NGte -> toBool $ ls >= rs
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVStr _, NVConstant NNull) -> case op of
|
|
|
|
NEq -> toBool False
|
|
|
|
NNEq -> toBool True
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVConstant NNull, NVStr _) -> case op of
|
|
|
|
NEq -> toBool False
|
|
|
|
NNEq -> toBool True
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVSet ls lp, NVSet rs rp) -> case op of
|
|
|
|
NUpdate -> pure $ bin nvSetP (rs `M.union` ls) (rp `M.union` lp)
|
|
|
|
NEq -> toBool =<< valueEqM lval rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval rval
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVSet ls lp, NVConstant NNull) -> case op of
|
|
|
|
NUpdate -> pure $ bin nvSetP ls lp
|
|
|
|
NEq -> toBool =<< valueEqM lval (nvSet M.empty M.empty)
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval (nvSet M.empty M.empty)
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVConstant NNull, NVSet rs rp) -> case op of
|
|
|
|
NUpdate -> pure $ bin nvSetP rs rp
|
|
|
|
NEq -> toBool =<< valueEqM (nvSet M.empty M.empty) rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM (nvSet M.empty M.empty) rval
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(ls@NVSet{}, NVStr rs) -> case op of
|
|
|
|
NPlus ->
|
|
|
|
(\ls2 -> bin nvStrP (ls2 `principledStringMappend` rs))
|
2019-03-22 23:16:01 +01:00
|
|
|
<$> coerceToString callFunc DontCopyToStore CoerceStringy ls
|
2019-03-17 22:47:38 +01:00
|
|
|
NEq -> toBool =<< valueEqM lval rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval rval
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVStr ls, rs@NVSet{}) -> case op of
|
|
|
|
NPlus ->
|
|
|
|
(\rs2 -> bin nvStrP (ls `principledStringMappend` rs2))
|
2019-03-22 23:16:01 +01:00
|
|
|
<$> coerceToString callFunc DontCopyToStore CoerceStringy rs
|
2019-03-17 22:47:38 +01:00
|
|
|
NEq -> toBool =<< valueEqM lval rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval rval
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVList ls, NVList rs) -> case op of
|
|
|
|
NConcat -> pure $ bin nvListP $ ls ++ rs
|
|
|
|
NEq -> toBool =<< valueEqM lval rval
|
|
|
|
NNEq -> toBool . not =<< valueEqM lval rval
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVList ls, NVConstant NNull) -> case op of
|
2019-06-15 20:49:35 +02:00
|
|
|
NEq -> toBool False
|
|
|
|
NNEq -> toBool True
|
2019-03-17 22:47:38 +01:00
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVConstant NNull, NVList rs) -> case op of
|
2019-06-15 20:49:35 +02:00
|
|
|
NEq -> toBool False
|
|
|
|
NNEq -> toBool True
|
2019-03-17 22:47:38 +01:00
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVPath p, NVStr ns) -> case op of
|
|
|
|
NEq -> toBool False -- From eqValues in nix/src/libexpr/eval.cc
|
|
|
|
NNEq -> toBool True
|
|
|
|
NPlus -> bin nvPathP <$> makeAbsolutePath @t @f
|
|
|
|
(p `mappend` Text.unpack (hackyStringIgnoreContext ns))
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
(NVPath ls, NVPath rs) -> case op of
|
|
|
|
NPlus -> bin nvPathP <$> makeAbsolutePath @t @f (ls ++ rs)
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
|
|
|
|
_ -> case op of
|
|
|
|
NEq -> toBool False
|
|
|
|
NNEq -> toBool True
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes lval rval
|
|
|
|
where
|
|
|
|
unsupportedTypes :: Show a => a -> a -> String
|
|
|
|
unsupportedTypes lval rval =
|
|
|
|
"Unsupported argument types for binary operator "
|
|
|
|
++ show op
|
|
|
|
++ ": "
|
|
|
|
++ show lval
|
|
|
|
++ ", "
|
|
|
|
++ show rval
|
|
|
|
|
|
|
|
numBinOp
|
2019-03-18 19:41:46 +01:00
|
|
|
:: (forall r . (Provenance m (NValue t f m) -> r) -> r)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> (forall a . Num a => a -> a -> a)
|
|
|
|
-> NAtom
|
|
|
|
-> NAtom
|
|
|
|
-> m (NValue t f m)
|
|
|
|
numBinOp bin f = numBinOp' bin f f
|
|
|
|
|
|
|
|
numBinOp'
|
2019-03-18 19:41:46 +01:00
|
|
|
:: (forall r . (Provenance m (NValue t f m) -> r) -> r)
|
2019-03-17 22:47:38 +01:00
|
|
|
-> (Integer -> Integer -> Integer)
|
|
|
|
-> (Float -> Float -> Float)
|
|
|
|
-> NAtom
|
|
|
|
-> NAtom
|
|
|
|
-> m (NValue t f m)
|
|
|
|
numBinOp' bin intF floatF l r = case (l, r) of
|
|
|
|
(NInt li, NInt ri ) -> toInt $ li `intF` ri
|
|
|
|
(NInt li, NFloat rf) -> toFloat $ fromInteger li `floatF` rf
|
|
|
|
(NFloat lf, NInt ri ) -> toFloat $ lf `floatF` fromInteger ri
|
|
|
|
(NFloat lf, NFloat rf) -> toFloat $ lf `floatF` rf
|
|
|
|
_ -> nverr $ ErrorCall $ unsupportedTypes l r
|
|
|
|
where
|
|
|
|
toInt = pure . bin nvConstantP . NInt
|
|
|
|
toFloat = pure . bin nvConstantP . NFloat
|
2018-04-11 06:01:48 +02:00
|
|
|
|
2019-03-22 23:16:01 +01:00
|
|
|
-- This function is here, rather than in 'Nix.String', because of the need to
|
|
|
|
-- use 'throwError'.
|
|
|
|
fromStringNoContext :: Framed e m => NixString -> m Text
|
2019-03-17 22:47:38 +01:00
|
|
|
fromStringNoContext ns = case principledGetStringNoContext ns of
|
|
|
|
Just str -> return str
|
|
|
|
Nothing -> throwError $ ErrorCall "expected string with no context"
|
2018-11-18 00:39:45 +01:00
|
|
|
|
2019-03-17 22:47:38 +01:00
|
|
|
addTracing
|
|
|
|
:: (MonadNix e t f m, Has e Options, MonadReader Int n, Alternative n)
|
|
|
|
=> Alg NExprLocF (m a)
|
|
|
|
-> Alg NExprLocF (n (m a))
|
2018-04-21 19:26:42 +02:00
|
|
|
addTracing k v = do
|
2019-03-17 22:47:38 +01:00
|
|
|
depth <- ask
|
|
|
|
guard (depth < 2000)
|
|
|
|
local succ $ do
|
|
|
|
v'@(Compose (Ann span x)) <- sequence v
|
|
|
|
return $ do
|
|
|
|
opts :: Options <- asks (view hasLens)
|
|
|
|
let rendered = if verbose opts >= Chatty
|
2018-05-13 23:17:55 +02:00
|
|
|
#ifdef MIN_VERSION_pretty_show
|
2019-03-17 22:47:38 +01:00
|
|
|
then pretty $ PS.ppShow (void x)
|
2018-05-13 23:17:55 +02:00
|
|
|
#else
|
2019-03-17 22:47:38 +01:00
|
|
|
then pretty $ show (void x)
|
2018-05-13 23:17:55 +02:00
|
|
|
#endif
|
2019-03-17 22:47:38 +01:00
|
|
|
else prettyNix (Fix (Fix (NSym "?") <$ x))
|
|
|
|
msg x = pretty ("eval: " ++ replicate depth ' ') <> x
|
|
|
|
loc <- renderLocation span (msg rendered <> " ...\n")
|
|
|
|
putStr $ show loc
|
|
|
|
res <- k v'
|
|
|
|
print $ msg rendered <> " ...done"
|
|
|
|
return res
|
2018-04-21 19:36:24 +02:00
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
evalExprLoc :: forall e t f m . MonadNix e t f m => NExprLoc -> m (NValue t f m)
|
2018-04-21 19:36:24 +02:00
|
|
|
evalExprLoc expr = do
|
2019-03-17 22:47:38 +01:00
|
|
|
opts :: Options <- asks (view hasLens)
|
|
|
|
if tracing opts
|
|
|
|
then join . (`runReaderT` (0 :: Int)) $ adi
|
|
|
|
(addTracing phi)
|
2019-03-18 19:41:46 +01:00
|
|
|
(raise (addStackFrames @(NValue t f m) . addSourcePositions))
|
2019-03-17 22:47:38 +01:00
|
|
|
expr
|
2019-03-18 19:41:46 +01:00
|
|
|
else adi phi (addStackFrames @(NValue t f m) . addSourcePositions) expr
|
2019-03-17 22:47:38 +01:00
|
|
|
where
|
|
|
|
phi = Eval.eval . annotated . getCompose
|
|
|
|
raise k f x = ReaderT $ \e -> k (\t -> runReaderT (f t) e) x
|
2018-09-09 17:01:09 +02:00
|
|
|
|
2019-03-18 06:46:15 +01:00
|
|
|
exec :: (MonadNix e t f m, MonadInstantiate m) => [String] -> m (NValue t f m)
|
2018-11-17 00:10:50 +01:00
|
|
|
exec args = either throwError evalExprLoc =<< exec' args
|
|
|
|
|
2019-03-09 23:52:20 +01:00
|
|
|
nixInstantiateExpr
|
2019-03-18 06:46:15 +01:00
|
|
|
:: (MonadNix e t f m, MonadInstantiate m) => String -> m (NValue t f m)
|
2018-11-17 00:10:50 +01:00
|
|
|
nixInstantiateExpr s = either throwError evalExprLoc =<< instantiateExpr s
|