copilot 3.6 → 3.7
raw patch · 3 files changed
+97/−6 lines, 3 filesdep ~copilot-c99dep ~copilot-coredep ~copilot-languagenew-component:exe:structsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: copilot-c99, copilot-core, copilot-language, copilot-libraries, copilot-theorem
API changes (from Hackage documentation)
Files
- CHANGELOG +4/−0
- copilot.cabal +18/−6
- examples/Structs.hs +75/−0
CHANGELOG view
@@ -1,3 +1,7 @@+2022-01-07+ * Version bump (3.7). (#287)+ * Add example with nested structs. (#275)+ 2021-11-07 * Version bump (3.6). (#264) * Fix missing dependency in example executable. (#252)
copilot.cabal view
@@ -1,5 +1,5 @@ name: copilot-version: 3.6+version: 3.7 cabal-version: >= 1.10 license: BSD3 license-file: LICENSE@@ -50,11 +50,11 @@ , directory >= 1.3 && < 1.4 , filepath >= 1.4 && < 1.5 - , copilot-core >= 3.6 && < 3.7- , copilot-theorem >= 3.6 && < 3.7- , copilot-language >= 3.6 && < 3.7- , copilot-libraries >= 3.6 && < 3.7- , copilot-c99 >= 3.6 && < 3.7+ , copilot-core >= 3.7 && < 3.8+ , copilot-theorem >= 3.7 && < 3.8+ , copilot-language >= 3.7 && < 3.8+ , copilot-libraries >= 3.7 && < 3.8+ , copilot-c99 >= 3.7 && < 3.8 exposed-modules: Language.Copilot, Language.Copilot.Main@@ -182,6 +182,18 @@ executable heater main-is: Heater.hs+ hs-source-dirs: examples+ build-depends: base >= 4.9 && < 5+ , copilot+ , copilot-c99+ default-language: Haskell2010+ if flag(examples)+ buildable: True+ else+ buildable: False++executable structs+ main-is: Structs.hs hs-source-dirs: examples build-depends: base >= 4.9 && < 5 , copilot
+ examples/Structs.hs view
@@ -0,0 +1,75 @@+-- | An example showing how specifications involving structs (in particular,+-- nested structs) are compiled to C using copilot-c99.++{-# LANGUAGE DataKinds #-}++module Main where++import qualified Prelude as P+import Control.Monad (void, forM_)++import Language.Copilot+import Copilot.Compile.C99+++-- | Definition for `Volts`.+data Volts = Volts+ { numVolts :: Field "numVolts" Word16+ , flag :: Field "flag" Bool+ }++-- | `Struct` instance for `Volts`.+instance Struct Volts where+ typename _ = "volts"+ toValues volts = [ Value Word16 (numVolts volts)+ , Value Bool (flag volts)+ ]++-- | `Volts` instance for `Typed`.+instance Typed Volts where+ typeOf = Struct (Volts (Field 0) (Field False))++data Battery = Battery+ { temp :: Field "temp" Word16+ , volts :: Field "volts" (Array 10 Volts)+ , other :: Field "other" (Array 10 (Array 5 Word32))+ }++-- | `Battery` instance for `Struct`.+instance Struct Battery where+ typename _ = "battery"+ toValues battery = [ Value typeOf (temp battery)+ , Value typeOf (volts battery)+ , Value typeOf (other battery)+ ]++-- | `Battery` instance for `Typed`. Note that `undefined` is used as an+-- argument to `Field`. This argument is never used, so `undefined` will never+-- throw an error.+instance Typed Battery where+ typeOf = Struct (Battery (Field 0) (Field undefined) (Field undefined))++spec :: Spec+spec = do+ let battery :: Stream Battery+ battery = extern "battery" Nothing++ -- Check equality, indexing into nested structs and arrays. Note that this is+ -- trivial by equality.+ trigger "equalitySameIndex"+ ((((battery#volts) .!! 0)#numVolts) == (((battery#volts) .!! 0)#numVolts))+ [arg battery]++ -- Same as previous example, but get a different array index (so should be+ -- false).+ trigger "equalityDifferentIndices"+ ((((battery#other) .!! 2) .!! 3) == (((battery#other) .!! 2) .!! 4))+ [arg battery]+++main :: IO ()+main = do+ spec' <- reify spec++ -- Compile the specific to C.+ compile "structs" spec'