module Language.Drasil.Chunk.CodeBase where

import Database.Drasil (ChunkDB, symbResolve)

import Language.Drasil
import Language.Drasil.CodeExpr.Development

import Data.List (nub)

-- | Construct a 'CodeVarChunk' from a 'Quantity'.
quantvar :: (Quantity c, MayHaveUnit c) => c -> CodeVarChunk
quantvar :: forall c. (Quantity c, MayHaveUnit c) => c -> CodeVarChunk
quantvar c
c = CodeChunk -> Maybe CodeChunk -> CodeVarChunk
CodeVC (QuantityDict -> VarOrFunc -> CodeChunk
CodeC (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw c
c) VarOrFunc
Var) forall a. Maybe a
Nothing

-- | Construct a 'CodeFuncChunk' from a 'Quantity'.
quantfunc :: (Quantity c, MayHaveUnit c) => c -> CodeFuncChunk
quantfunc :: forall c. (Quantity c, MayHaveUnit c) => c -> CodeFuncChunk
quantfunc c
c = CodeChunk -> CodeFuncChunk
CodeFC forall a b. (a -> b) -> a -> b
$ QuantityDict -> VarOrFunc -> CodeChunk
CodeC (forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw c
c) VarOrFunc
Func

-- | Get a list of 'CodeChunk's from an equation.
codevars :: CodeExpr -> ChunkDB -> [CodeVarChunk]
codevars :: CodeExpr -> ChunkDB -> [CodeVarChunk]
codevars CodeExpr
e ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> CodeVarChunk
varResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ CodeExpr -> [UID]
eDep CodeExpr
e

-- | Get a list of 'CodeChunk's from an equation (no functions).
codevars' :: CodeExpr -> ChunkDB -> [CodeVarChunk]
codevars' :: CodeExpr -> ChunkDB -> [CodeVarChunk]
codevars' CodeExpr
e ChunkDB
m = forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> CodeVarChunk
varResolve ChunkDB
m) forall a b. (a -> b) -> a -> b
$ forall a. Eq a => [a] -> [a]
nub forall a b. (a -> b) -> a -> b
$ CodeExpr -> [UID]
eDep' CodeExpr
e

-- | Make a 'CodeVarChunk' from a 'UID' in the 'ChunkDB'.
varResolve :: ChunkDB -> UID -> CodeVarChunk
varResolve :: ChunkDB -> UID -> CodeVarChunk
varResolve  ChunkDB
m UID
x = forall c. (Quantity c, MayHaveUnit c) => c -> CodeVarChunk
quantvar forall a b. (a -> b) -> a -> b
$ ChunkDB -> UID -> QuantityDict
symbResolve ChunkDB
m UID
x

-- | Make a 'CodeFuncChunk' from a 'UID' in the 'ChunkDB'.
funcResolve :: ChunkDB -> UID -> CodeFuncChunk
funcResolve :: ChunkDB -> UID -> CodeFuncChunk
funcResolve ChunkDB
m UID
x = forall c. (Quantity c, MayHaveUnit c) => c -> CodeFuncChunk
quantfunc forall a b. (a -> b) -> a -> b
$ ChunkDB -> UID -> QuantityDict
symbResolve ChunkDB
m UID
x