module Text.UxADT
where
import Data.String.Utils (join)
type Variable = String
type Constructor = String
data UxADT =
B Bool
| I Int
| F Float
| D Double
| S String
| V Variable
| C Constructor [UxADT]
| L [UxADT]
| None
deriving (Eq)
class ToUxADT a where
uxadt :: a -> UxADT
instance ToUxADT Bool where
uxadt = B
instance ToUxADT Int where
uxadt = I
instance ToUxADT Float where
uxadt = F
instance ToUxADT Double where
uxadt = D
instance ToUxADT a => ToUxADT [a] where
uxadt l = L $ map uxadt l
instance ToUxADT a => ToUxADT (Maybe a) where
uxadt m = maybe None uxadt m
instance Show UxADT where
show u = to "" u where
to ind u = case u of
B b -> show b
I i -> show i
F f -> show f
D d -> show d
S s -> "\"" ++ s ++ "\""
V v -> "uxadt.V(\"" ++ v ++ "\")"
C c us -> "uxadt.C(\"" ++ c ++ "\", [" ++ join ", " [show u | u <- us] ++ "])"
L us -> "[" ++ join ", " [show u | u <- us] ++ "]"
None -> "uxadt.None"
javaScriptModule :: String -> UxADT -> String
javaScriptModule name u =
"var " ++ name ++ " = (function(){return " ++ show u ++ ";})();"
--eof