module D3JS.Syntax where
import Data.Text (Text)
import qualified Data.Text as T
import D3JS.Type
import D3JS.Reify
import Control.Monad.RWS
type St r = RWS () Text Int r
getUniqueNum :: St Int
getUniqueNum = do
n <- get
put (n+1)
return n
instance Reifiable (St r) where
reify st =
let (a,s,w) = runRWS st () 0
in w
execute :: Chain () b -> St ()
execute chain = tell $ T.concat [reify chain,";\n"]
idx0 = Index 0 DataParam
idx1 = Index 1 DataParam
_x = Field "x" DataParam
_y = Field "y" DataParam
class Assignable a where
newVar :: St (Var' a)
assign :: Chain () a -> St (Var' a)
assign chain = do
v@(Var' n) <- newVar
tell $ T.concat ["var ",n," = ",reify chain,";\n"]
return v
instance Assignable Data2D where
newVar = newVar' "dat"
instance Assignable (SelData Data2D) where
newVar = newVar' "sel_dat"
instance Assignable Selection where
newVar = newVar' "sel"
newVar' :: Text -> St (Var' a)
newVar' baseName = getUniqueNum >>= (return . Var' . T.append baseName . show')