Move NSym out of NAtom

An NSym doesn't evaluate to itself and never appears in an NValue, so it
should not be an atom.
This commit is contained in:
Benno Fünfstück 2014-08-18 19:07:34 +02:00
parent 52a97733d2
commit c67dcf7c67
3 changed files with 10 additions and 4 deletions

View file

@ -29,7 +29,7 @@ evalExpr :: NExpr -> NValue -> IO NValue
evalExpr = cata phi
where
phi :: NExprF (NValue -> IO NValue) -> NValue -> IO NValue
phi (NConstant (NSym var)) = \env -> case env of
phi (NSym var) = \env -> case env of
Fix (NVSet s) -> maybe err return $ Map.lookup var s
_ -> error "invalid evaluation environment"
where err = error ("Undefined variable: " ++ show var)

View file

@ -120,6 +120,7 @@ prettyNix = withoutParens . cata phi where
phi (NApp fun arg)
= NixDoc (wrapParens appOp fun <+> wrapParens appOpNonAssoc arg) appOp
phi (NSym name) = simpleExpr $ text (unpack name)
phi (NLet _binds _body) = simpleExpr $ text "let"
phi (NIf cond trueBody falseBody) = leastPrecedence $
group $ nest 2 $ (text "if" <+> withoutParens cond) <$>

View file

@ -31,11 +31,14 @@ cata f = f . fmap (cata f) . outF
cataM :: (Traversable f, Monad m) => (f a -> m a) -> Fix f -> m a
cataM f = f <=< mapM (cataM f) . outF
-- | Atoms are values that evaluate to themselves. This means that they appear in both
-- the parsed AST (in the form of literals) and the evaluated form.
data NAtom
-- | An integer. The c nix implementation currently only supports integers that
-- fit in the range of 'Int64'.
= NInt Integer
| NPath FilePath
| NBool Bool
| NSym Text
| NNull
deriving (Eq, Ord, Generic, Typeable, Data, Show)
@ -43,7 +46,6 @@ atomText :: NAtom -> Text
atomText (NInt i) = pack (show i)
atomText (NPath p) = pack p
atomText (NBool b) = if b then "true" else "false"
atomText (NSym s) = s
atomText NNull = "null"
-- | 'Antiquoted' represents an expression that is either
@ -246,6 +248,9 @@ data NExprF r
| NApp r r
-- language constructs
-- | A 'NSym' is a reference to a variable. For example, @f@ is represented as
-- @NSym "f"@ and @a@ as @NSym "a" in @f a@.
| NSym Text
| NLet [Binding r] r
| NIf r r r
| NWith r r
@ -268,7 +273,7 @@ mkPath :: FilePath -> NExpr
mkPath = Fix . NConstant . NPath
mkSym :: Text -> NExpr
mkSym = Fix . NConstant . NSym
mkSym = Fix . NSym
mkSelector :: Text -> NSelector NExpr
mkSelector = (:[]) . StaticKey