{-# LANGUAGE OverloadedStrings #-} module Language.Fiddle.Internal.UnitInterface where import Data.Aeson import Data.Text import Data.Word import Language.Fiddle.Internal.Scopes (Scope) import Language.Fiddle.Types (SourceSpan) data Annotated a = Annotated { sourceSpan :: SourceSpan, docComment :: Text, internal :: a } deriving (Eq, Ord, Show) -- | Contains a datastructure which represents a FiddleUnit. -- -- These datastructures contain the exported symobls of a fiddle unit and it's -- direct dependencies. data UnitInterface where UnitInterface :: { rootScope :: Scope String (Annotated ExportedValue), dependencies :: [FilePath] } -> UnitInterface deriving (Eq, Ord, Show) instance Semigroup UnitInterface where (<>) (UnitInterface s d) (UnitInterface s1 d1) = UnitInterface (s <> s1) (d <> d1) instance Monoid UnitInterface where mempty = UnitInterface mempty mempty data ExportedValue where ExportedBitsType :: {exportBitsTypeSize :: Word32} -> ExportedValue ExportedObjType :: {exportObjTypeSize :: Word32} -> ExportedValue deriving (Show, Eq, Ord) instance (ToJSON a) => ToJSON (Annotated a) where toJSON (Annotated span doc internal) = object [ "sourceSpan" .= span, "docComment" .= doc, "internal" .= internal ] instance (FromJSON a) => FromJSON (Annotated a) where parseJSON = withObject "Annotated" $ \v -> Annotated <$> v .: "sourceSpan" <*> v .: "docComment" <*> v .: "internal" instance ToJSON UnitInterface where toJSON (UnitInterface rootScope dependencies) = object [ "rootScope" .= rootScope, "dependencies" .= dependencies ] instance FromJSON UnitInterface where parseJSON = withObject "UnitInterface" $ \v -> UnitInterface <$> v .: "rootScope" <*> v .: "dependencies" instance ToJSON ExportedValue where toJSON (ExportedBitsType size) = object [ "type" .= String "ExportedBitsType", "size" .= size ] toJSON (ExportedObjType size) = object [ "type" .= String "ExportedObjType", "size" .= size ] instance FromJSON ExportedValue where parseJSON = withObject "ExportedValue" $ \v -> do typ <- v .: "type" case typ of String "ExportedBitsType" -> ExportedBitsType <$> v .: "size" String "ExportedObjType" -> ExportedObjType <$> v .: "size" _ -> fail "Unknown ExportedValue type"