module Language.Drasil.Chunk.CodeBase where

import Database.Drasil (ChunkDB, symbResolve)

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

-- | 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 (c -> QuantityDict
forall q. (Quantity q, MayHaveUnit q) => q -> QuantityDict
qw c
c) VarOrFunc
Var) Maybe CodeChunk
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 (CodeChunk -> CodeFuncChunk) -> CodeChunk -> CodeFuncChunk
forall a b. (a -> b) -> a -> b
$ QuantityDict -> VarOrFunc -> CodeChunk
CodeC (c -> QuantityDict
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 = (UID -> CodeVarChunk) -> [UID] -> [CodeVarChunk]
forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> CodeVarChunk
varResolve ChunkDB
m) ([UID] -> [CodeVarChunk]) -> [UID] -> [CodeVarChunk]
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 = (UID -> CodeVarChunk) -> [UID] -> [CodeVarChunk]
forall a b. (a -> b) -> [a] -> [b]
map (ChunkDB -> UID -> CodeVarChunk
varResolve ChunkDB
m) ([UID] -> [CodeVarChunk]) -> [UID] -> [CodeVarChunk]
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 = QuantityDict -> CodeVarChunk
forall c. (Quantity c, MayHaveUnit c) => c -> CodeVarChunk
quantvar (QuantityDict -> CodeVarChunk) -> QuantityDict -> CodeVarChunk
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 = QuantityDict -> CodeFuncChunk
forall c. (Quantity c, MayHaveUnit c) => c -> CodeFuncChunk
quantfunc (QuantityDict -> CodeFuncChunk) -> QuantityDict -> CodeFuncChunk
forall a b. (a -> b) -> a -> b
$ ChunkDB -> UID -> QuantityDict
symbResolve ChunkDB
m UID
x