module Hylogen.Program where
import Data.Reify
import Data.Monoid
import System.IO.Unsafe
import Hylogen.Expr
import Hylogen.Types.Vec (Vec4)
newtype Id = Id Int
instance Show Id where
show (Id h) = "_" <> show h
data Statement = NewAssign (Unique, ExprMonoF Unique)
getExpr :: Statement -> ExprMonoF Unique
getExpr (NewAssign (_, expr)) = expr
instance Show Statement where
show (NewAssign (i, expr@(TreeF (_, ty, _, _) _)))
= mconcat [ show ty, " ", show . Id $ i, " = ", show . (Id<$>) $ expr, ";"]
newtype Function = Function [Statement]
instance Show Function where
show (Function xs) = unlines [ "void main() {"
, assignments
, " gl_FragColor = _1;"
, "}"
]
where
assignments = mconcat $ (<> "\n") . (" "<>) . show <$> reverse xs
monoToProgram :: ExprMono -> Function
monoToProgram v = unsafePerformIO $ do
Graph nodes _ <- reifyGraph v
return . Function $ NewAssign <$> nodes
type Program = Function
toProgram :: Vec4 -> Program
toProgram = monoToProgram . toMono