PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/coredumper/coredump.hs

https://github.com/khskrede/mehh
Haskell | 99 lines | 53 code | 40 blank | 6 comment | 0 complexity | 1dcf733c7bc6b341fab6adbb19e41184 MD5 | raw file
  1. import GHC
  2. --GHC.Paths is available via cabal install ghc-paths
  3. import GHC.Paths ( libdir )
  4. import DynFlags ( defaultDynFlags )
  5. import System.Environment ( getArgs )
  6. import Outputable
  7. import Text.JSON
  8. import Text.JSON.Pretty
  9. import Text.JSON.Generic
  10. import Text.PrettyPrint.HughesPJ
  11. import UniqFM
  12. import Unique
  13. import HscTypes
  14. import CoreSyn
  15. main = do
  16. (inFile:outFile:_) <- getArgs
  17. res <- compile inFile
  18. writeFile outFile $ show $ pp_value $ coreModToJS res
  19. -- Process cabal file
  20. -- Compile Haskell file
  21. compile inFile = runGhc (Just libdir) $ do
  22. sdflags <- getSessionDynFlags
  23. let sdflags' = sdflags
  24. setSessionDynFlags sdflags'
  25. core <- compileToCoreSimplified inFile
  26. return $ (core)
  27. coreModToJS :: CoreModule -> JSValue
  28. coreModToJS (CoreModule name types binds) =
  29. JSObject $ toJSObject $
  30. [( "%module", toJSON $ showSDoc $ ppr name ),
  31. ( "tdefg", typesToJS types),
  32. ( "binds", bindsToJS binds)]
  33. typesToJS :: TypeEnv -> JSValue
  34. typesToJS p = JSObject $ toJSObject $ map func $ ufmToList p
  35. where
  36. func :: (Unique, TyThing) -> (String, JSValue)
  37. -- TODO: Generate proper JSON values
  38. func (a, b) = ( showSDoc $ ppr a, toJSON $ showSDoc $ ppr b)
  39. bindsToJS :: CoreProgram -> JSValue
  40. bindsToJS progs = JSObject $ toJSObject $ map f $ progs
  41. where
  42. f :: CoreBind -> (String, JSValue)
  43. f (NonRec b expr) = ("NonRec" , corebindToJS (b, expr))
  44. f (Rec list) = ("Rec", JSArray $ map corebindToJS list)
  45. corebindToJS :: (CoreBndr, (Expr CoreBndr)) -> JSValue
  46. corebindToJS (a, b) = JSObject $ toJSObject $ [(show a, exprToJS b)]
  47. -- BINDERS
  48. -- EXPRESSIONS
  49. exprToJS :: Expr b -> JSValue
  50. exprToJS (Var a) = toJSON a
  51. exprToJS (Lit a) = toJSON "Lit"
  52. exprToJS (App a b) = toJSON "App"
  53. exprToJS (Lam a b) = JSArray [toJSON "Lam", exprToJS b]
  54. exprToJS (Let a b) = toJSON "Let"
  55. exprToJS (Case a b c d) = toJSON "Case"
  56. exprToJS (Cast a b) = toJSON "Cast"
  57. exprToJS (Tick a b) = toJSON "Tick"
  58. exprToJS (Type a) = toJSON "Type"
  59. exprToJS (Coercion a) = toJSON "Coercion"