{-# LANGUAGE TemplateHaskell #-}
module Language.Drasil.Chunk.CodeVar where
import Data.Char (isSpace)
import Control.Lens ((^.), view, makeLenses, Lens')
import Language.Drasil.Classes (CommonIdea(abrv), Quantity, Idea(getA), NamedIdea(..), Callable)
import Language.Drasil.Chunk.Quantity (QuantityDict, implVar')
import Language.Drasil.Space (HasSpace(..), Space(..))
import Language.Drasil.Symbol (HasSymbol(symbol))
import Language.Drasil.UID (HasUID(uid), (+++))
import Language.Drasil.Chunk.UnitDefn (MayHaveUnit(getUnit))
import Language.Drasil.Stages (Stage(..))
import Language.Drasil.CodeExpr.Lang (CodeExpr)
import Utils.Drasil (toPlainName)
class CodeIdea c where
codeName :: c -> String
codeChunk :: c -> CodeChunk
class CodeIdea c => DefiningCodeExpr c where
codeExpr :: Lens' c CodeExpr
programName :: CommonIdea c => c -> String
programName :: forall c. CommonIdea c => c -> String
programName = String -> String
toPlainName (String -> String) -> (c -> String) -> c -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace) (String -> String) -> (c -> String) -> c -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> String
forall c. CommonIdea c => c -> String
abrv
funcPrefix :: String
funcPrefix :: String
funcPrefix = String
"func_"
data VarOrFunc = Var | Func
data CodeChunk = CodeC { CodeChunk -> QuantityDict
_qc :: QuantityDict
, CodeChunk -> VarOrFunc
kind :: VarOrFunc
}
makeLenses ''CodeChunk
instance HasUID CodeChunk where uid :: Getter CodeChunk UID
uid = (QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk
Lens' CodeChunk QuantityDict
qc ((QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk)
-> ((UID -> f UID) -> QuantityDict -> f QuantityDict)
-> (UID -> f UID)
-> CodeChunk
-> f CodeChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UID -> f UID) -> QuantityDict -> f QuantityDict
forall c. HasUID c => Getter c UID
Getter QuantityDict UID
uid
instance NamedIdea CodeChunk where term :: Lens' CodeChunk NP
term = (QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk
Lens' CodeChunk QuantityDict
qc ((QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk)
-> ((NP -> f NP) -> QuantityDict -> f QuantityDict)
-> (NP -> f NP)
-> CodeChunk
-> f CodeChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NP -> f NP) -> QuantityDict -> f QuantityDict
forall c. NamedIdea c => Lens' c NP
Lens' QuantityDict NP
term
instance Idea CodeChunk where getA :: CodeChunk -> Maybe String
getA = QuantityDict -> Maybe String
forall c. Idea c => c -> Maybe String
getA (QuantityDict -> Maybe String)
-> (CodeChunk -> QuantityDict) -> CodeChunk -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting QuantityDict CodeChunk QuantityDict
-> CodeChunk -> QuantityDict
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting QuantityDict CodeChunk QuantityDict
Lens' CodeChunk QuantityDict
qc
instance HasSpace CodeChunk where typ :: Getter CodeChunk Space
typ = (QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk
Lens' CodeChunk QuantityDict
qc ((QuantityDict -> f QuantityDict) -> CodeChunk -> f CodeChunk)
-> ((Space -> f Space) -> QuantityDict -> f QuantityDict)
-> (Space -> f Space)
-> CodeChunk
-> f CodeChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Space -> f Space) -> QuantityDict -> f QuantityDict
forall c. HasSpace c => Getter c Space
Getter QuantityDict Space
typ
instance HasSymbol CodeChunk where symbol :: CodeChunk -> Stage -> Symbol
symbol = QuantityDict -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol (QuantityDict -> Stage -> Symbol)
-> (CodeChunk -> QuantityDict) -> CodeChunk -> Stage -> Symbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting QuantityDict CodeChunk QuantityDict
-> CodeChunk -> QuantityDict
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting QuantityDict CodeChunk QuantityDict
Lens' CodeChunk QuantityDict
qc
instance Quantity CodeChunk
instance Eq CodeChunk where CodeChunk
c1 == :: CodeChunk -> CodeChunk -> Bool
== CodeChunk
c2 = (CodeChunk
c1 CodeChunk -> Getting UID CodeChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeChunk UID
forall c. HasUID c => Getter c UID
Getter CodeChunk UID
uid) UID -> UID -> Bool
forall a. Eq a => a -> a -> Bool
== (CodeChunk
c2 CodeChunk -> Getting UID CodeChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeChunk UID
forall c. HasUID c => Getter c UID
Getter CodeChunk UID
uid)
instance MayHaveUnit CodeChunk where getUnit :: CodeChunk -> Maybe UnitDefn
getUnit = QuantityDict -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit (QuantityDict -> Maybe UnitDefn)
-> (CodeChunk -> QuantityDict) -> CodeChunk -> Maybe UnitDefn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting QuantityDict CodeChunk QuantityDict
-> CodeChunk -> QuantityDict
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting QuantityDict CodeChunk QuantityDict
Lens' CodeChunk QuantityDict
qc
data CodeVarChunk = CodeVC {CodeVarChunk -> CodeChunk
_ccv :: CodeChunk,
CodeVarChunk -> Maybe CodeChunk
_obv :: Maybe CodeChunk}
makeLenses ''CodeVarChunk
instance HasUID CodeVarChunk where uid :: Getter CodeVarChunk UID
uid = (CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk
Lens' CodeVarChunk CodeChunk
ccv ((CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk)
-> ((UID -> f UID) -> CodeChunk -> f CodeChunk)
-> (UID -> f UID)
-> CodeVarChunk
-> f CodeVarChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UID -> f UID) -> CodeChunk -> f CodeChunk
forall c. HasUID c => Getter c UID
Getter CodeChunk UID
uid
instance NamedIdea CodeVarChunk where term :: Lens' CodeVarChunk NP
term = (CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk
Lens' CodeVarChunk CodeChunk
ccv ((CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk)
-> ((NP -> f NP) -> CodeChunk -> f CodeChunk)
-> (NP -> f NP)
-> CodeVarChunk
-> f CodeVarChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NP -> f NP) -> CodeChunk -> f CodeChunk
forall c. NamedIdea c => Lens' c NP
Lens' CodeChunk NP
term
instance Idea CodeVarChunk where getA :: CodeVarChunk -> Maybe String
getA = CodeChunk -> Maybe String
forall c. Idea c => c -> Maybe String
getA (CodeChunk -> Maybe String)
-> (CodeVarChunk -> CodeChunk) -> CodeVarChunk -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeVarChunk CodeChunk
-> CodeVarChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeVarChunk CodeChunk
Lens' CodeVarChunk CodeChunk
ccv
instance HasSpace CodeVarChunk where typ :: Getter CodeVarChunk Space
typ = (CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk
Lens' CodeVarChunk CodeChunk
ccv ((CodeChunk -> f CodeChunk) -> CodeVarChunk -> f CodeVarChunk)
-> ((Space -> f Space) -> CodeChunk -> f CodeChunk)
-> (Space -> f Space)
-> CodeVarChunk
-> f CodeVarChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Space -> f Space) -> CodeChunk -> f CodeChunk
forall c. HasSpace c => Getter c Space
Getter CodeChunk Space
typ
instance HasSymbol CodeVarChunk where symbol :: CodeVarChunk -> Stage -> Symbol
symbol = CodeChunk -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol (CodeChunk -> Stage -> Symbol)
-> (CodeVarChunk -> CodeChunk) -> CodeVarChunk -> Stage -> Symbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeVarChunk CodeChunk
-> CodeVarChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeVarChunk CodeChunk
Lens' CodeVarChunk CodeChunk
ccv
instance Quantity CodeVarChunk
instance Eq CodeVarChunk where CodeVarChunk
c1 == :: CodeVarChunk -> CodeVarChunk -> Bool
== CodeVarChunk
c2 = (CodeVarChunk
c1 CodeVarChunk -> Getting UID CodeVarChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeVarChunk UID
forall c. HasUID c => Getter c UID
Getter CodeVarChunk UID
uid) UID -> UID -> Bool
forall a. Eq a => a -> a -> Bool
== (CodeVarChunk
c2 CodeVarChunk -> Getting UID CodeVarChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeVarChunk UID
forall c. HasUID c => Getter c UID
Getter CodeVarChunk UID
uid)
instance MayHaveUnit CodeVarChunk where getUnit :: CodeVarChunk -> Maybe UnitDefn
getUnit = CodeChunk -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit (CodeChunk -> Maybe UnitDefn)
-> (CodeVarChunk -> CodeChunk) -> CodeVarChunk -> Maybe UnitDefn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeVarChunk CodeChunk
-> CodeVarChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeVarChunk CodeChunk
Lens' CodeVarChunk CodeChunk
ccv
newtype CodeFuncChunk = CodeFC {CodeFuncChunk -> CodeChunk
_ccf :: CodeChunk}
makeLenses ''CodeFuncChunk
instance HasUID CodeFuncChunk where uid :: Getter CodeFuncChunk UID
uid = (CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk
Iso' CodeFuncChunk CodeChunk
ccf ((CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk)
-> ((UID -> f UID) -> CodeChunk -> f CodeChunk)
-> (UID -> f UID)
-> CodeFuncChunk
-> f CodeFuncChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UID -> f UID) -> CodeChunk -> f CodeChunk
forall c. HasUID c => Getter c UID
Getter CodeChunk UID
uid
instance NamedIdea CodeFuncChunk where term :: Lens' CodeFuncChunk NP
term = (CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk
Iso' CodeFuncChunk CodeChunk
ccf ((CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk)
-> ((NP -> f NP) -> CodeChunk -> f CodeChunk)
-> (NP -> f NP)
-> CodeFuncChunk
-> f CodeFuncChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NP -> f NP) -> CodeChunk -> f CodeChunk
forall c. NamedIdea c => Lens' c NP
Lens' CodeChunk NP
term
instance Idea CodeFuncChunk where getA :: CodeFuncChunk -> Maybe String
getA = CodeChunk -> Maybe String
forall c. Idea c => c -> Maybe String
getA (CodeChunk -> Maybe String)
-> (CodeFuncChunk -> CodeChunk) -> CodeFuncChunk -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeFuncChunk CodeChunk
-> CodeFuncChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeFuncChunk CodeChunk
Iso' CodeFuncChunk CodeChunk
ccf
instance HasSpace CodeFuncChunk where typ :: Getter CodeFuncChunk Space
typ = (CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk
Iso' CodeFuncChunk CodeChunk
ccf ((CodeChunk -> f CodeChunk) -> CodeFuncChunk -> f CodeFuncChunk)
-> ((Space -> f Space) -> CodeChunk -> f CodeChunk)
-> (Space -> f Space)
-> CodeFuncChunk
-> f CodeFuncChunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Space -> f Space) -> CodeChunk -> f CodeChunk
forall c. HasSpace c => Getter c Space
Getter CodeChunk Space
typ
instance HasSymbol CodeFuncChunk where symbol :: CodeFuncChunk -> Stage -> Symbol
symbol = CodeChunk -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol (CodeChunk -> Stage -> Symbol)
-> (CodeFuncChunk -> CodeChunk) -> CodeFuncChunk -> Stage -> Symbol
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeFuncChunk CodeChunk
-> CodeFuncChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeFuncChunk CodeChunk
Iso' CodeFuncChunk CodeChunk
ccf
instance Quantity CodeFuncChunk
instance Callable CodeFuncChunk
instance Eq CodeFuncChunk where CodeFuncChunk
c1 == :: CodeFuncChunk -> CodeFuncChunk -> Bool
== CodeFuncChunk
c2 = (CodeFuncChunk
c1 CodeFuncChunk -> Getting UID CodeFuncChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeFuncChunk UID
forall c. HasUID c => Getter c UID
Getter CodeFuncChunk UID
uid) UID -> UID -> Bool
forall a. Eq a => a -> a -> Bool
== (CodeFuncChunk
c2 CodeFuncChunk -> Getting UID CodeFuncChunk UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID CodeFuncChunk UID
forall c. HasUID c => Getter c UID
Getter CodeFuncChunk UID
uid)
instance MayHaveUnit CodeFuncChunk where getUnit :: CodeFuncChunk -> Maybe UnitDefn
getUnit = CodeChunk -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit (CodeChunk -> Maybe UnitDefn)
-> (CodeFuncChunk -> CodeChunk) -> CodeFuncChunk -> Maybe UnitDefn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting CodeChunk CodeFuncChunk CodeChunk
-> CodeFuncChunk -> CodeChunk
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting CodeChunk CodeFuncChunk CodeChunk
Iso' CodeFuncChunk CodeChunk
ccf
listToArray :: CodeVarChunk -> CodeVarChunk
listToArray :: CodeVarChunk -> CodeVarChunk
listToArray CodeVarChunk
c = Space -> CodeVarChunk
newSpc (CodeVarChunk
c CodeVarChunk -> Getting Space CodeVarChunk Space -> Space
forall s a. s -> Getting a s a -> a
^. Getting Space CodeVarChunk Space
forall c. HasSpace c => Getter c Space
Getter CodeVarChunk Space
typ)
where newSpc :: Space -> CodeVarChunk
newSpc (Vect Space
t) = CodeChunk -> Maybe CodeChunk -> CodeVarChunk
CodeVC (QuantityDict -> VarOrFunc -> CodeChunk
CodeC (String
-> NP
-> Maybe String
-> Space
-> Symbol
-> Maybe UnitDefn
-> QuantityDict
implVar' (UID -> String
forall a. Show a => a -> String
show (UID -> String) -> UID -> String
forall a b. (a -> b) -> a -> b
$ CodeVarChunk
c CodeVarChunk -> String -> UID
forall a. HasUID a => a -> String -> UID
+++ String
"_array")
(CodeVarChunk
c CodeVarChunk -> Getting NP CodeVarChunk NP -> NP
forall s a. s -> Getting a s a -> a
^. Getting NP CodeVarChunk NP
forall c. NamedIdea c => Lens' c NP
Lens' CodeVarChunk NP
term) (CodeVarChunk -> Maybe String
forall c. Idea c => c -> Maybe String
getA CodeVarChunk
c) (Space -> Space
Array Space
t) (CodeVarChunk -> Stage -> Symbol
forall c. HasSymbol c => c -> Stage -> Symbol
symbol CodeVarChunk
c Stage
Implementation) (CodeVarChunk -> Maybe UnitDefn
forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit CodeVarChunk
c))
VarOrFunc
Var) (CodeVarChunk
c CodeVarChunk
-> Getting (Maybe CodeChunk) CodeVarChunk (Maybe CodeChunk)
-> Maybe CodeChunk
forall s a. s -> Getting a s a -> a
^. Getting (Maybe CodeChunk) CodeVarChunk (Maybe CodeChunk)
Lens' CodeVarChunk (Maybe CodeChunk)
obv)
newSpc Space
_ = CodeVarChunk
c