Created
July 2, 2014 06:33
-
-
Save fredyr/ea76338af4a5247bc7cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS_GHC -fno-warn-orphans #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE QuasiQuotes #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Monad (void) | |
import Ivory.Language | |
import Ivory.Compile.C.CmdlineFrontend | |
import Ivory.Compile.C.Modules | |
[ivory| | |
struct Cool { | |
idx :: Stored Uint32 | |
; arr :: Array 10 (Stored Uint32) | |
; value :: Stored IFloat | |
} | |
|] | |
degrees :: (Fractional a) => a -> a | |
degrees x = x * 57.295779513082320876798154814105 | |
computerz :: Def ('[IFloat, IFloat, IFloat] :-> IFloat) | |
computerz = proc "computerz" $ \a b c -> requires (c /=? 0) $ body $ do | |
a' <- assign $ degrees a | |
b' <- assign $ degrees b | |
diff <- assign $ a' - b' | |
ret $ diff / c | |
clearCool :: Def ('[Ref a (Struct "Cool")] :-> ()) | |
clearCool = proc "clear_cool" $ \s -> body $ do | |
x <- deref (s~>idx) | |
store ((s~>arr) ! (0 :: Ix 10)) 12 | |
-- init all values | |
arrayMap (\ix -> store ((s~>arr) ! (ix :: Ix 10)) 4) | |
store (s~>value) 3.1415927 | |
store (s~>idx) (x + 1) | |
cmodule :: Module | |
cmodule = package "Stuff" $ do | |
defStruct (Proxy :: Proxy "Cool") | |
incl computerz | |
incl clearCool | |
runIt :: IO () | |
runIt = void $ runCompiler [cmodule] initialOpts { stdOut = True } |
Author
fredyr
commented
Jul 2, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment