More WIP
This commit is contained in:
parent
985cd9a32a
commit
e73fc5d196
15
Nix/Eval.hs
15
Nix/Eval.hs
|
@ -21,6 +21,8 @@ import Nix.Atoms
|
||||||
import Nix.Expr
|
import Nix.Expr
|
||||||
import Prelude hiding (mapM, sequence)
|
import Prelude hiding (mapM, sequence)
|
||||||
|
|
||||||
|
import Nix.Parser
|
||||||
|
import Data.Maybe (isJust)
|
||||||
import Debug.Trace
|
import Debug.Trace
|
||||||
|
|
||||||
-- | An 'NValue' is the most reduced form of an 'NExpr' after evaluation
|
-- | An 'NValue' is the most reduced form of an 'NExpr' after evaluation
|
||||||
|
@ -69,6 +71,12 @@ atomText (NBool b) = if b then "true" else "false"
|
||||||
atomText NNull = "null"
|
atomText NNull = "null"
|
||||||
atomText (NUri uri) = uri
|
atomText (NUri uri) = uri
|
||||||
|
|
||||||
|
expr = let Success x = parseNixString "({ x ? 1, y ? x * 3 }: y - x) {}"
|
||||||
|
in x
|
||||||
|
goeval :: MonadFix m => m (NValue m)
|
||||||
|
goeval = evalExpr expr mempty
|
||||||
|
|
||||||
|
|
||||||
buildArgument :: MonadFix m => Params (ValueSet m -> m (NValue m)) -> NValue m -> m (ValueSet m)
|
buildArgument :: MonadFix m => Params (ValueSet m -> m (NValue m)) -> NValue m -> m (ValueSet m)
|
||||||
buildArgument paramSpec arg = case paramSpec of
|
buildArgument paramSpec arg = case paramSpec of
|
||||||
Param name -> return $ Map.singleton name arg
|
Param name -> return $ Map.singleton name arg
|
||||||
|
@ -77,17 +85,16 @@ buildArgument paramSpec arg = case paramSpec of
|
||||||
Map.insert name arg <$> lookupParamSet s
|
Map.insert name arg <$> lookupParamSet s
|
||||||
ParamSet _ _ -> error "Can't yet handle variadic param sets"
|
ParamSet _ _ -> error "Can't yet handle variadic param sets"
|
||||||
where
|
where
|
||||||
go env envAndArgs k def = maybe (error err) id $ mvalueFromEnv <|> mvalueFromDef
|
go env evaledArgs k def = maybe (error err) id $ (mvalueFromEnv <|> mvalueFromDef)
|
||||||
where
|
where
|
||||||
mvalueFromEnv = return <$> Map.lookup k env
|
mvalueFromEnv = return <$> Map.lookup k env
|
||||||
mvalueFromDef = ($ Map.delete k envAndArgs) <$> def
|
mvalueFromDef = ($ evaledArgs) <$> def
|
||||||
err = "Could not find " ++ show k
|
err = "Could not find " ++ show k
|
||||||
lookupParamSet s =
|
lookupParamSet s =
|
||||||
case arg of
|
case arg of
|
||||||
Fix (NVSet env) -> do
|
Fix (NVSet env) -> do
|
||||||
rec
|
rec
|
||||||
evaledArgs <- Map.traverseWithKey (\k v -> traceShow k $ go env envAndArgs k v) s
|
evaledArgs <- Map.traverseWithKey (go env evaledArgs) s
|
||||||
let envAndArgs = env `Map.union` evaledArgs
|
|
||||||
return evaledArgs
|
return evaledArgs
|
||||||
_ -> error "Unexpected function environment"
|
_ -> error "Unexpected function environment"
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ case_function_set_two_arg :: Assertion
|
||||||
case_function_set_two_arg = constantEqualStr "2" "({ a, b ? 3 }: b - a) { a = 1; }"
|
case_function_set_two_arg = constantEqualStr "2" "({ a, b ? 3 }: b - a) { a = 1; }"
|
||||||
|
|
||||||
case_function_set_two_arg_default_scope :: Assertion
|
case_function_set_two_arg_default_scope :: Assertion
|
||||||
case_function_set_two_arg_default_scope = constantEqualStr "2" "({ a ? 1, b ? a * 3 }: b - a) {}"
|
case_function_set_two_arg_default_scope = constantEqualStr "2" "({ x ? 1, y ? x * 3 }: y - x) {}"
|
||||||
|
|
||||||
case_function_default_env :: Assertion
|
case_function_default_env :: Assertion
|
||||||
case_function_default_env = constantEqualStr "2" "let default = 2; in ({ a ? default }: a) {}"
|
case_function_default_env = constantEqualStr "2" "let default = 2; in ({ a ? default }: a) {}"
|
||||||
|
|
Loading…
Reference in a new issue