summaryrefslogtreecommitdiff
path: root/.gitlab/hello.hs
blob: 74ab771132db408c940f98fe2636dc1fe30d850c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# OPTIONS_GHC -Wall -Wno-missing-fields #-}

import GHC hiding (parseModule)
import GHC.Data.StringBuffer
import GHC.Driver.Config.Parser
import GHC.Parser
import GHC.Parser.Lexer
import GHC.Platform
import GHC.Plugins
import GHC.Settings
import GHC.Settings.Config
import System.Mem.Weak

fakeSettings :: Settings
fakeSettings =
  Settings
    { sGhcNameVersion =
        GhcNameVersion
          { ghcNameVersion_programName =
              "ghc",
            ghcNameVersion_projectVersion =
              cProjectVersion
          },
      sFileSettings =
        FileSettings {},
      sToolSettings = ToolSettings {},
      sTargetPlatform =
        genericPlatform,
      sPlatformMisc = PlatformMisc {}
    }

fakeDynFlags :: DynFlags
fakeDynFlags = defaultDynFlags fakeSettings

parse :: DynFlags -> String -> IO (Located (HsModule GhcPs))
parse dflags src = do
  let buf = stringToStringBuffer src
  let loc = mkRealSrcLoc (mkFastString "Main.hs") 1 1
  case unP parseModule (initParserState (initParserOpts dflags) buf loc) of
    PFailed _ -> fail "parseModule failed"
    POk _ rdr_module -> pure rdr_module

main :: IO ()
main = do
  _ <- mkWeak runGhc runGhc Nothing
  m <- parse fakeDynFlags "main = putStrLn \"hello world\""
  putStrLn $ showSDoc fakeDynFlags $ ppr m