hnix/src/Nix/Atoms.hs

37 lines
1 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DeriveAnyClass #-}
2018-04-07 21:02:50 +02:00
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Nix.Atoms where
import Codec.Serialise
2018-04-09 11:07:40 +02:00
import Control.DeepSeq
2018-04-07 21:02:50 +02:00
import Data.Data
2018-04-17 20:59:54 +02:00
import Data.Hashable
2018-04-07 21:02:50 +02:00
import Data.Text (Text, pack)
import GHC.Generics
-- | 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,
Serialise, Hashable)
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)
atomText (NFloat f) = pack (show f)
atomText (NBool b) = if b then "true" else "false"
atomText NNull = "null"