{-# LANGUAGE TemplateHaskell #-}
-- | Defines chunk types for use in code generation.
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)


-- not using lenses for now
-- | A 'CodeIdea' must include some code and its name. 
class CodeIdea c where
  -- | Name of the idea.
  codeName  :: c -> String
  -- | Code chunk associated with the idea.
  codeChunk :: c -> CodeChunk

-- | A 'DefiningCodeExpr' must have it's underlying chunk 
--   defined in the CodeExpr language.
class CodeIdea c => DefiningCodeExpr c where
  codeExpr  :: Lens' c CodeExpr

-- | Convert an abbreviation into one deemed 'code-friendly', removing spaces,
--   and replacing special characters with underscores.
--
--   FIXME: This should NOT be treated as a 'getter', but something we cache
--   local to something that has a 'program name'.
programName :: CommonIdea c => c -> String
programName :: forall c. CommonIdea c => c -> String
programName = String -> String
toPlainName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. CommonIdea c => c -> String
abrv

-- | Used when a function name needs to be distinguishable from a variable name.
funcPrefix :: String
funcPrefix :: String
funcPrefix = String
"func_"
 
-- | Details if a piece of code is meant to be a variable or a function.
data VarOrFunc = Var | Func

-- | Basic chunk representation in the code generation context.
-- Contains a QuantityDict and the kind of code (variable or function).
data CodeChunk = CodeC { CodeChunk -> QuantityDict
_qc  :: QuantityDict
                       , CodeChunk -> VarOrFunc
kind :: VarOrFunc  -- TODO: Jason: Once we have function spaces, I believe we won't need to store this
                       }
makeLenses ''CodeChunk

-- | Finds the 'UID' of the 'QuantityDict' used to make the 'CodeChunk'.
instance HasUID      CodeChunk where uid :: Lens' CodeChunk UID
uid = Lens' CodeChunk QuantityDict
qc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds the term ('NP') of the 'QuantityDict' used to make the 'CodeChunk'.
instance NamedIdea   CodeChunk where term :: Lens' CodeChunk NP
term = Lens' CodeChunk QuantityDict
qc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea contained in the 'QuantityDict' used to make the 'CodeChunk'.
instance Idea        CodeChunk where getA :: CodeChunk -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeChunk QuantityDict
qc
-- | Finds the 'Space' of the 'QuantityDict' used to make the 'CodeChunk'.
instance HasSpace    CodeChunk where typ :: Getter CodeChunk Space
typ = Lens' CodeChunk QuantityDict
qc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ
-- | Finds the 'Stage' dependent 'Symbol' of the 'QuantityDict' used to make the 'CodeChunk'.
instance HasSymbol   CodeChunk where symbol :: CodeChunk -> Stage -> Symbol
symbol = forall c. HasSymbol c => c -> Stage -> Symbol
symbol forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeChunk QuantityDict
qc
-- | 'CodeChunk's have a 'Quantity'.
instance Quantity    CodeChunk
-- | Equal if 'UID's are equal.
instance Eq          CodeChunk where CodeChunk
c1 == :: CodeChunk -> CodeChunk -> Bool
== CodeChunk
c2 = (CodeChunk
c1 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (CodeChunk
c2 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid)
-- | Finds the units of the 'QuantityDict' used to make the 'CodeChunk'.
instance MayHaveUnit CodeChunk where getUnit :: CodeChunk -> Maybe UnitDefn
getUnit = forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeChunk QuantityDict
qc

-- | Chunk representing a variable. The @obv@ field represents the object containing 
-- this variable, if it is an object field.
data CodeVarChunk = CodeVC {CodeVarChunk -> CodeChunk
_ccv :: CodeChunk,
                            CodeVarChunk -> Maybe CodeChunk
_obv :: Maybe CodeChunk}
makeLenses ''CodeVarChunk

-- | Finds the 'UID' of the 'CodeChunk' used to make the 'CodeVarChunk'.
instance HasUID      CodeVarChunk where uid :: Lens' CodeVarChunk UID
uid = Lens' CodeVarChunk CodeChunk
ccv forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds the term ('NP') of the 'CodeChunk' used to make the 'CodeVarChunk'.
instance NamedIdea   CodeVarChunk where term :: Lens' CodeVarChunk NP
term = Lens' CodeVarChunk CodeChunk
ccv forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea contained in the 'CodeChunk' used to make the 'CodeVarChunk'.
instance Idea        CodeVarChunk where getA :: CodeVarChunk -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeVarChunk CodeChunk
ccv
-- | Finds the 'Space' of the 'CodeChunk' used to make the 'CodeVarChunk'.
instance HasSpace    CodeVarChunk where typ :: Getter CodeVarChunk Space
typ = Lens' CodeVarChunk CodeChunk
ccv forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ
-- | Finds the 'Stage' dependent 'Symbol' of the 'CodeChunk' used to make the 'CodeVarChunk'.
instance HasSymbol   CodeVarChunk where symbol :: CodeVarChunk -> Stage -> Symbol
symbol = forall c. HasSymbol c => c -> Stage -> Symbol
symbol forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeVarChunk CodeChunk
ccv
-- | 'CodeVarChunk's have a 'Quantity'.
instance Quantity    CodeVarChunk
-- | Equal if 'UID's are equal.
instance Eq          CodeVarChunk where CodeVarChunk
c1 == :: CodeVarChunk -> CodeVarChunk -> Bool
== CodeVarChunk
c2 = (CodeVarChunk
c1 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (CodeVarChunk
c2 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid)
-- | Finds the units of the 'CodeChunk' used to make the 'CodeVarChunk'.
instance MayHaveUnit CodeVarChunk where getUnit :: CodeVarChunk -> Maybe UnitDefn
getUnit = forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Lens' CodeVarChunk CodeChunk
ccv

-- | Chunk representing a function.
newtype CodeFuncChunk = CodeFC {CodeFuncChunk -> CodeChunk
_ccf :: CodeChunk}
makeLenses ''CodeFuncChunk

-- | Finds the 'UID' of the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance HasUID      CodeFuncChunk where uid :: Lens' CodeFuncChunk UID
uid = Iso' CodeFuncChunk CodeChunk
ccf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasUID c => Lens' c UID
uid
-- | Finds the term ('NP') of the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance NamedIdea   CodeFuncChunk where term :: Lens' CodeFuncChunk NP
term = Iso' CodeFuncChunk CodeChunk
ccf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. NamedIdea c => Lens' c NP
term
-- | Finds the idea contained in the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance Idea        CodeFuncChunk where getA :: CodeFuncChunk -> Maybe String
getA = forall c. Idea c => c -> Maybe String
getA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Iso' CodeFuncChunk CodeChunk
ccf
-- | Finds the 'Space' of the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance HasSpace    CodeFuncChunk where typ :: Getter CodeFuncChunk Space
typ = Iso' CodeFuncChunk CodeChunk
ccf forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall c. HasSpace c => Getter c Space
typ
-- | Finds the 'Stage' dependent 'Symbol' of the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance HasSymbol   CodeFuncChunk where symbol :: CodeFuncChunk -> Stage -> Symbol
symbol = forall c. HasSymbol c => c -> Stage -> Symbol
symbol forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Iso' CodeFuncChunk CodeChunk
ccf
-- | 'CodeFuncChunk's have a 'Quantity'.
instance Quantity    CodeFuncChunk
-- | Functions are Callable.
instance Callable    CodeFuncChunk
-- | Equal if 'UID's are equal.
instance Eq          CodeFuncChunk where CodeFuncChunk
c1 == :: CodeFuncChunk -> CodeFuncChunk -> Bool
== CodeFuncChunk
c2 = (CodeFuncChunk
c1 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid) forall a. Eq a => a -> a -> Bool
== (CodeFuncChunk
c2 forall s a. s -> Getting a s a -> a
^. forall c. HasUID c => Lens' c UID
uid)
-- | Finds the units of the 'CodeChunk' used to make the 'CodeFuncChunk'.
instance MayHaveUnit CodeFuncChunk where getUnit :: CodeFuncChunk -> Maybe UnitDefn
getUnit = forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Iso' CodeFuncChunk CodeChunk
ccf

-- FIXME: use show for the UID here? Perhaps need a different implVar function for UIDs
-- Changes a 'CodeVarChunk'\'s space from 'Vect' to 'Array'.
listToArray :: CodeVarChunk -> CodeVarChunk
listToArray :: CodeVarChunk -> CodeVarChunk
listToArray CodeVarChunk
c = Space -> CodeVarChunk
newSpc (CodeVarChunk
c forall s a. s -> Getting a s a -> a
^. forall c. HasSpace c => Getter c 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' (forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ CodeVarChunk
c forall a. HasUID a => a -> String -> UID
+++ String
"_array")
          (CodeVarChunk
c forall s a. s -> Getting a s a -> a
^. forall c. NamedIdea c => Lens' c NP
term) (forall c. Idea c => c -> Maybe String
getA CodeVarChunk
c) (Space -> Space
Array Space
t) (forall c. HasSymbol c => c -> Stage -> Symbol
symbol CodeVarChunk
c Stage
Implementation) (forall u. MayHaveUnit u => u -> Maybe UnitDefn
getUnit CodeVarChunk
c)) 
          VarOrFunc
Var) (CodeVarChunk
c forall s a. s -> Getting a s a -> a
^. Lens' CodeVarChunk (Maybe CodeChunk)
obv)
        newSpc Space
_ = CodeVarChunk
c