-- | Extract UIDs from an expression so that they can be looked up in the chunk database and rendered.
module Language.Drasil.Expr.Extract where

import Data.List (nub)

import Language.Drasil.Expr.Lang (Expr(..))
import Language.Drasil.Space (RealInterval(..))
import Language.Drasil.UID (UID)

-- | Generic traverse of all expressions that could lead to names.
eNames :: Expr -> [UID]
eNames :: Expr -> [UID]
eNames (AssocA AssocArithOper
_ [Expr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames [Expr]
l
eNames (AssocB AssocBoolOper
_ [Expr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames [Expr]
l
eNames (C UID
c)                 = [UID
c]
eNames Lit{}                 = []
eNames (FCall UID
f [Expr]
x)           = UID
f forall a. a -> [a] -> [a]
: forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames [Expr]
x
eNames (Case Completeness
_ [(Expr, Expr)]
ls)           = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Expr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Expr, Expr)]
ls forall a. [a] -> [a] -> [a]
++
                               forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Expr -> [UID]
eNames forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(Expr, Expr)]
ls
eNames (UnaryOp UFunc
_ Expr
u)         = Expr -> [UID]
eNames Expr
u
eNames (UnaryOpB UFuncB
_ Expr
u)        = Expr -> [UID]
eNames Expr
u
eNames (UnaryOpVV UFuncVV
_ Expr
u)       = Expr -> [UID]
eNames Expr
u
eNames (UnaryOpVN UFuncVN
_ Expr
u)       = Expr -> [UID]
eNames Expr
u
eNames (ArithBinaryOp ArithBinOp
_ Expr
a Expr
b) = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (BoolBinaryOp BoolBinOp
_ Expr
a Expr
b)  = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (EqBinaryOp EqBinOp
_ Expr
a Expr
b)    = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (LABinaryOp LABinOp
_ Expr
a Expr
b)    = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (OrdBinaryOp OrdBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (VVVBinaryOp VVVBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (VVNBinaryOp VVNBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (NVVBinaryOp NVVBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
b
eNames (Operator AssocArithOper
_ DiscreteDomainDesc Expr Expr
_ Expr
e)      = Expr -> [UID]
eNames Expr
e
eNames (Matrix [[Expr]]
a)            = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames) [[Expr]]
a
eNames (RealI UID
c RealInterval Expr Expr
b)           = UID
c forall a. a -> [a] -> [a]
: RealInterval Expr Expr -> [UID]
eNamesRI RealInterval Expr Expr
b

-- | Generic traversal of everything that could come from an interval to names (similar to 'eNames').
eNamesRI :: RealInterval Expr Expr -> [UID]
eNamesRI :: RealInterval Expr Expr -> [UID]
eNamesRI (Bounded (Inclusive
_, Expr
il) (Inclusive
_, Expr
iu)) = Expr -> [UID]
eNames Expr
il forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames Expr
iu
eNamesRI (UpTo (Inclusive
_, Expr
iu))            = Expr -> [UID]
eNames Expr
iu
eNamesRI (UpFrom (Inclusive
_, Expr
il))          = Expr -> [UID]
eNames Expr
il

-- | Generic traverse of all positions that could lead to 'eNames' without
-- functions.  FIXME : this should really be done via post-facto filtering, but
-- right now the information needed to do this is not available!
eNames' :: Expr -> [UID]
eNames' :: Expr -> [UID]
eNames' (AssocA AssocArithOper
_ [Expr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames' [Expr]
l
eNames' (AssocB AssocBoolOper
_ [Expr]
l)          = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames' [Expr]
l
eNames' (C UID
c)                 = [UID
c]
eNames' Lit{}                 = []
eNames' (FCall UID
_ [Expr]
x)           = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames' [Expr]
x
eNames' (Case Completeness
_ [(Expr, Expr)]
ls)           = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Expr -> [UID]
eNames' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Expr, Expr)]
ls forall a. [a] -> [a] -> [a]
++ 
                                forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Expr -> [UID]
eNames' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(Expr, Expr)]
ls
eNames' (UnaryOp UFunc
_ Expr
u)         = Expr -> [UID]
eNames' Expr
u
eNames' (UnaryOpB UFuncB
_ Expr
u)        = Expr -> [UID]
eNames' Expr
u
eNames' (UnaryOpVV UFuncVV
_ Expr
u)       = Expr -> [UID]
eNames' Expr
u
eNames' (UnaryOpVN UFuncVN
_ Expr
u)       = Expr -> [UID]
eNames' Expr
u
eNames' (ArithBinaryOp ArithBinOp
_ Expr
a Expr
b) = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (BoolBinaryOp BoolBinOp
_ Expr
a Expr
b)  = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (EqBinaryOp EqBinOp
_ Expr
a Expr
b)    = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (LABinaryOp LABinOp
_ Expr
a Expr
b)    = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (OrdBinaryOp OrdBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (VVVBinaryOp VVVBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (VVNBinaryOp VVNBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (NVVBinaryOp NVVBinOp
_ Expr
a Expr
b)   = Expr -> [UID]
eNames' Expr
a forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' Expr
b
eNames' (Operator AssocArithOper
_ DiscreteDomainDesc Expr Expr
_ Expr
e)      = Expr -> [UID]
eNames' Expr
e
eNames' (Matrix [[Expr]]
a)            = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Expr -> [UID]
eNames') [[Expr]]
a
eNames' (RealI UID
c RealInterval Expr Expr
b)           = UID
c forall a. a -> [a] -> [a]
: RealInterval Expr Expr -> [UID]
eNamesRI' RealInterval Expr Expr
b

-- | Generic traversal of everything that could come from an interval to names without functions (similar to 'eNames'').
eNamesRI' :: RealInterval Expr Expr -> [UID]
eNamesRI' :: RealInterval Expr Expr -> [UID]
eNamesRI' (Bounded (Inclusive, Expr)
il (Inclusive, Expr)
iu) = Expr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, Expr)
il) forall a. [a] -> [a] -> [a]
++ Expr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, Expr)
iu)
eNamesRI' (UpTo (Inclusive, Expr)
iu)       = Expr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, Expr)
iu)
eNamesRI' (UpFrom (Inclusive, Expr)
il)     = Expr -> [UID]
eNames' (forall a b. (a, b) -> b
snd (Inclusive, Expr)
il)

---------------------------------------------------------------------------
-- And now implement the exported traversals all in terms of the above

-- | Get dependencies from an equation.  
eDep :: Expr -> [UID]
eDep :: Expr -> [UID]
eDep = forall a. Eq a => [a] -> [a]
nub forall b c a. (b -> c) -> (a -> b) -> a -> c
. Expr -> [UID]
eNames