never executed always true always false
1 {-# LANGUAGE DeriveGeneric, DeriveDataTypeable, DeriveFunctor, DeriveTraversable, DeriveFoldable #-}
2
3 module Conjure.Language.Expression.Op.Substring where
4
5 import Conjure.Prelude
6 import Conjure.Language.Expression.Op.Internal.Common
7
8 import qualified Data.Aeson as JSON -- aeson
9 import qualified Data.Aeson.KeyMap as KM
10
11 import qualified Data.Vector as V -- vector
12
13
14 data OpSubstring x = OpSubstring x x
15 deriving (Eq, Ord, Show, Data, Functor, Traversable, Foldable, Typeable, Generic)
16
17 instance Serialize x => Serialize (OpSubstring x)
18 instance Hashable x => Hashable (OpSubstring x)
19 instance ToJSON x => ToJSON (OpSubstring x) where toJSON = genericToJSON jsonOptions
20 instance FromJSON x => FromJSON (OpSubstring x) where parseJSON = genericParseJSON jsonOptions
21
22 instance BinaryOperator (OpSubstring x) where
23 opLexeme _ = L_substring
24
25 instance (TypeOf x, Pretty x) => TypeOf (OpSubstring x) where
26 typeOf p@(OpSubstring a b) = do
27 tya <- typeOf a
28 tyb <- typeOf b
29 case (tya, tyb) of
30 (TypeSequence{}, TypeSequence{}) -> return TypeBool
31 _ -> raiseTypeError $ vcat [ pretty p
32 , "Unexpected types for operands:"
33 , " - " <> pretty tya
34 , " - " <> pretty tyb
35 ]
36
37 instance SimplifyOp OpSubstring x where
38 simplifyOp _ = na "simplifyOp{OpSubstring}"
39
40 instance Pretty x => Pretty (OpSubstring x) where
41 prettyPrec prec op@(OpSubstring a b) = prettyPrecBinOp prec [op] a b
42
43 instance VarSymBreakingDescription x => VarSymBreakingDescription (OpSubstring x) where
44 varSymBreakingDescription (OpSubstring a b) = JSON.Object $ KM.fromList
45 [ ("type", JSON.String "OpSubstring")
46 , ("children", JSON.Array $ V.fromList
47 [ varSymBreakingDescription a
48 , varSymBreakingDescription b
49 ])
50 ]