-- | Defines functions to render 'Space's into printable 'P.Expr's.
module Language.Drasil.Printing.Import.Space where

import Language.Drasil (Space(..))

import qualified Language.Drasil.Printing.AST as P
import Language.Drasil.Printing.PrintingInformation (PrintingInformation)

import Data.List (intersperse)
import Data.List.NonEmpty (toList)

-- | Render a 'Space'.
space :: PrintingInformation -> Space -> P.Expr
space :: PrintingInformation -> Space -> Expr
space PrintingInformation
_  Space
Integer        = Ops -> Expr
P.MO Ops
P.Integer
space PrintingInformation
_  Space
Rational       = Ops -> Expr
P.MO Ops
P.Rational
space PrintingInformation
_  Space
Real           = Ops -> Expr
P.MO Ops
P.Real
space PrintingInformation
_  Space
Natural        = Ops -> Expr
P.MO Ops
P.Natural
space PrintingInformation
_  Space
Boolean        = Ops -> Expr
P.MO Ops
P.Boolean
space PrintingInformation
_  Space
Char           = String -> Expr
P.Ident String
"Char"
space PrintingInformation
_  Space
String         = String -> Expr
P.Ident String
"String"
space PrintingInformation
_  (Vect Space
_)       = String -> Expr
forall a. HasCallStack => String -> a
error String
"Vector space not translated"
space PrintingInformation
_  Matrix {}      = String -> Expr
forall a. HasCallStack => String -> a
error String
"Matrix space not translated"
space PrintingInformation
_  (Array Space
_)      = String -> Expr
forall a. HasCallStack => String -> a
error String
"Array space not translated"
space PrintingInformation
_  (Actor String
s)      = String -> Expr
P.Ident String
s
space PrintingInformation
_ (Set Space
_ )        = String -> Expr
forall a. HasCallStack => String -> a
error String
"Set space not translated"
space PrintingInformation
_  Space
Void           = String -> Expr
forall a. HasCallStack => String -> a
error String
"Void not translated"
space PrintingInformation
sm (Function NonEmpty Space
i Space
t) = [Expr] -> Expr
P.Row ([Expr] -> Expr) -> [Expr] -> Expr
forall a b. (a -> b) -> a -> b
$
  Expr -> [Expr] -> [Expr]
forall a. a -> [a] -> [a]
intersperse (Ops -> Expr
P.MO Ops
P.Cross) ((Space -> Expr) -> [Space] -> [Expr]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Space -> Expr
space PrintingInformation
sm) ([Space] -> [Expr]) -> [Space] -> [Expr]
forall a b. (a -> b) -> a -> b
$ NonEmpty Space -> [Space]
forall a. NonEmpty a -> [a]
toList NonEmpty Space
i) [Expr] -> [Expr] -> [Expr]
forall a. [a] -> [a] -> [a]
++  -- AxBxC...xY
  [Ops -> Expr
P.MO Ops
P.RArrow, PrintingInformation -> Space -> Expr
space PrintingInformation
sm Space
t]                                -- -> Z