PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Scotch/Types/Show.hs

http://github.com/bendmorris/scotch
Haskell | 79 lines | 72 code | 7 blank | 0 comment | 22 complexity | f4e2f3d2f2a6ec91833ca9378395bda2 MD5 | raw file
Possible License(s): GPL-3.0
  1. module Scotch.Types.Show where
  2. import Data.List
  3. import Data.List.Utils
  4. import Numeric
  5. import Scotch.Types.Types
  6. formatString [] = []
  7. formatString (h:t) = if h == '"' then "\\\"" ++ formatString t
  8. else h : formatString t
  9. removeBrackets s = if (s !! 0) == '[' && (s !! (l-1)) == ']'
  10. then "(" ++ [s !! n | n <- [1..l-2]] ++ ")"
  11. else s
  12. where l = length s
  13. moduleName s = join "." s
  14. instance Show (Value) where
  15. show (Str s) = "\"" ++ formatString s ++ "\""
  16. show (NumInt n) = show n
  17. show (NumFloat n) = showFFloat Nothing n ""
  18. show (Bit True) = "true"
  19. show (Bit False) = "false"
  20. show (Hash h) = "{" ++ (if length h > 0
  21. then join ", " ["\"" ++ fst i ++ "\": " ++ show (snd i) | j <- h, i <- j]
  22. else "") ++ "}"
  23. show (Lambda ids expr) = "(" ++ (join ", " ids) ++ ") -> " ++ show expr
  24. show (Proc p) = join ", " [show i | i <- p]
  25. show (Thread th) = "thread " ++ show th
  26. show (Null) = "null"
  27. show (Undefined s) = show s
  28. show InvalidValue = "**invalid value**"
  29. instance Show(Expr) where
  30. show (Exception s) = "Exception: " ++ s
  31. show Skip = ""
  32. show (Val v) = show v
  33. show (List l) = show l
  34. show (Take a b) = "take " ++ show a ++ " from " ++ show b
  35. show (HashExpr h) = "{" ++ (if length h > 0
  36. then join ", " [show (fst i) ++ ": " ++ show (snd i) | i <- h]
  37. else "") ++ "}"
  38. show (Concat a b) = "(" ++ show a ++ " : " ++ show b ++ ")"
  39. show (Subs n s) = show s ++ " @" ++ show n
  40. show (Add x y) = "(" ++ show x ++ " + " ++ show y ++ ")"
  41. show (Sub x y) = "(" ++ show x ++ " - " ++ show y ++ ")"
  42. show (Prod (Val (NumInt x)) (Var y)) = show x ++ y
  43. show (Prod (Val (NumInt x)) y) = show x ++ if show y !! 0 == '(' then show y else "(" ++ show y ++ ")"
  44. show (Prod (Val (NumFloat x)) (Val y)) = show x ++ if show y !! 0 == '(' then show y else "(" ++ show y ++ ")"
  45. show (Prod x y) = "(" ++ show x ++ " * " ++ show y ++ ")"
  46. show (Div x y) = "(" ++ show x ++ " / " ++ show y ++ ")"
  47. show (Mod x y) = "(" ++ show x ++ " mod " ++ show y ++ ")"
  48. show (Exp x y) = "(" ++ show x ++ " ^ " ++ show y ++ ")"
  49. show (Eq x y) = "(" ++ show x ++ " == " ++ show y ++ ")"
  50. show (InEq x y) = "(" ++ show x ++ " != " ++ show y ++ ")"
  51. show (Gt x y) = "(" ++ show x ++ " > " ++ show y ++ ")"
  52. show (Lt x y) = "(" ++ show x ++ " < " ++ show y ++ ")"
  53. show (And x y) = "(" ++ show x ++ " & " ++ show y ++ ")"
  54. show (Or x y) = "(" ++ show x ++ " | " ++ show y ++ ")"
  55. show (Not x) = "not " ++ show x
  56. show (Def a (Rule r) Skip) = "rule " ++ show a ++ " =" ++ show (Rule r)
  57. show (Rule r) = join ", " [show i | i <- r]
  58. show (Def a b Skip) = show a ++ " = " ++ show b
  59. show (Def a b c) = "(" ++ show c ++ " where " ++ show a ++ " = " ++ show b ++ ")"
  60. show (EagerDef a b Skip) = show a ++ " := " ++ show b
  61. show (EagerDef a b c) = "(" ++ show c ++ " where " ++ show a ++ " := " ++ show b ++ ")"
  62. show (UseRule r x) = "using " ++ show r ++ " => " ++ show x
  63. show (Var f) = f
  64. show (Call f args) = show f ++ removeBrackets (show args)
  65. show (If (Call (Var "bool") [cond]) x y) = "if " ++ show cond ++ " then " ++ show x ++ " else " ++ show y
  66. show (If cond x y) = "if " ++ show cond ++ " then " ++ show x ++ " else " ++ show y
  67. show (Case c o) = "case " ++ show c ++ " of" ++ tail (foldl (++) "" [", " ++ show (fst i) ++ " -> " ++ show (snd i) | i <- o])
  68. show (For x (Call (Var "list") [y]) z w) = "[for " ++ x ++ " in " ++ show y ++ ", " ++ show z ++ (foldl (++) "" [", " ++ show w' | w' <- w]) ++ "]"
  69. show (For x y z w) = "[for " ++ x ++ " in " ++ show y ++ ", " ++ show z ++ (foldl (++) "" [", " ++ show w' | w' <- w]) ++ "]"
  70. show (Range x y z) = "[" ++ show x ++ ".." ++ show y ++ (if z == (Val (NumInt 1)) then "" else "," ++ show z) ++ "]"
  71. show (Import s []) = "import " ++ moduleName s
  72. show (Import s t) = show (Import s []) ++ " as " ++ moduleName t