1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
-- |
-- Module : Language.C.Smart
-- Copyright : (c) 2010-2011 Harvard University
-- (c) 2011-2013 Geoffrey Mainland
-- License : BSD-style
-- Maintainer : mainland@drexel.edu
{-# LANGUAGE CPP #-}
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Language.C.Smart where
import Language.C.Quote.C
import Language.C.Syntax as C
#if !MIN_VERSION_template_haskell(2,7,0)
import qualified Data.Loc
import qualified Language.C.Syntax
#endif /* !MIN_VERSION_template_haskell(2,7,0) */
instance Enum Exp where
toEnum n = [cexp|$int:n|]
fromEnum [cexp|$int:n|] = fromIntegral n
fromEnum [cexp|$uint:n|] = fromIntegral n
fromEnum [cexp|$lint:n|] = fromIntegral n
fromEnum [cexp|$ulint:n|] = fromIntegral n
fromEnum _ =
error "fromEnum: non-integer constant C expressions"
instance Num C.Exp where
e1 + e2 = [cexp|$exp:e1 + $exp:e2|]
e1 * e2 = [cexp|$exp:e1 * $exp:e2|]
e1 - e2 = [cexp|$exp:e1 - $exp:e2|]
negate e = [cexp|-$exp:e|]
abs e = [cexp|abs($exp:e)|]
signum e = [cexp|$exp:e > 0 ? 1 : ($exp:e < 0 ? -1 : 0)|]
fromInteger n = [cexp|$int:n|]
instance Real C.Exp where
toRational [cexp|$float:n|] = toRational n
toRational [cexp|$double:n|] = toRational n
toRational [cexp|$ldouble:n|] = toRational n
toRational _ =
error "fromEnum: non-rational constant C expressions"
instance Integral C.Exp where
e1 `quotRem` e2 = ([cexp|$exp:e1 / $exp:e2|], [cexp|$exp:e1 % $exp:e2|])
toInteger [cexp|$int:n|] = n
toInteger [cexp|$uint:n|] = n
toInteger [cexp|$lint:n|] = n
toInteger [cexp|$ulint:n|] = n
toInteger _ =
error "fromInteger: non-integer constant C expressions"
instance Fractional C.Exp where
e1 / e2 = [cexp|$exp:e1 / $exp:e2|]
recip e = [cexp|1 / $exp:e|]
fromRational n = [cexp|$double:(fromRational n)|]
instance Floating C.Exp where
pi = [cexp|3.141592653589793238|]
exp e = [cexp|exp($exp:e)|]
sqrt e = [cexp|sqrt($exp:e)|]
log e = [cexp|log($exp:e)|]
e1 ** e2 = [cexp|pow($exp:e1, $exp:e2)|]
logBase e1 e2 = [cexp|log($exp:e2)/log($exp:e1)|]
sin e = [cexp|sin($exp:e)|]
tan e = [cexp|tan($exp:e)|]
cos e = [cexp|cos($exp:e)|]
asin e = [cexp|asin($exp:e)|]
atan e = [cexp|atan($exp:e)|]
acos e = [cexp|acos($exp:e)|]
sinh e = [cexp|sinh($exp:e)|]
tanh e = [cexp|tanh($exp:e)|]
cosh e = [cexp|cosh($exp:e)|]
asinh e = [cexp|asinh($exp:e)|]
atanh e = [cexp|atanh($exp:e)|]
acosh e = [cexp|acosh($exp:e)|]
infix 4 ===
(===) :: C.Exp -> C.Exp -> C.Stm
e1 === e2 = [cstm|$exp:e1 = $exp:e2;|]
infix 4 +=
(+=) :: C.Exp -> C.Exp -> C.Stm
e1 += e2 = [cstm|$exp:e1 += $exp:e2;|]
|