hnix/src/Nix/Atoms.hs

54 lines
1.3 KiB
Haskell
Raw Normal View History

{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
2018-04-07 21:02:50 +02:00
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Nix.Atoms where
#ifdef MIN_VERSION_serialise
import Codec.Serialise
#endif
import Control.DeepSeq
import Data.Data
import Data.Hashable
import Data.Text ( Text
, pack
)
import GHC.Generics
2018-04-07 21:02:50 +02:00
-- | 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'.
2018-04-09 11:07:40 +02:00
= NInt Integer
2018-04-07 21:02:50 +02:00
-- | A floating point number
2018-04-09 11:07:40 +02:00
| NFloat Float
2018-04-07 21:02:50 +02:00
-- | Booleans.
2018-04-09 11:07:40 +02:00
| NBool Bool
2018-04-07 21:02:50 +02:00
-- | Null values. There's only one of this variant.
| NNull
deriving (Eq, Ord, Generic, Typeable, Data, Show, Read, NFData,
Hashable)
#ifdef MIN_VERSION_serialise
instance Serialise NAtom
#endif
2018-04-09 11:07:40 +02:00
2018-04-07 21:02:50 +02:00
-- | Translate an atom into its nix representation.
atomText :: NAtom -> Text
atomText (NInt i) = pack (show i)
2018-04-07 21:02:50 +02:00
atomText (NFloat f) = pack (show f)
atomText (NBool b) = if b then "true" else "false"
2018-04-07 21:02:50 +02:00
atomText NNull = "null"