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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Curry.Files where

import Directory       ( doesFileExist )
import FileGoodies     ( getFileInPath, lookupFileInPath )
import FilePath        ( takeFileName, (</>), (<.>) )
import ReadShowTerm    ( readUnqualifiedTerm ) -- for faster reading

import System.CurryPath    ( lookupModuleSourceInLoadPath, getLoadPathForModule
                           , inCurrySubdir, stripCurrySuffix )
import System.FrontendExec ( FrontendParams, FrontendTarget (..), defaultParams
                           , setQuiet, callFrontend, callFrontendWithParams
                           )

import Curry.Types

-- | Reads the short-AST from a specified module
readShortAST :: String -> IO (Module ())
readShortAST progname =
  readShortASTWithParseOptions progname (setQuiet True defaultParams)

-- | Reads the AST from a specified module
readFullAST :: String -> IO (Module ())
readFullAST progname =
  readFullASTWithParseOptions progname (setQuiet True defaultParams)

-- | Reads the short-AST with further options from a specified module
readShortASTWithParseOptions :: String -> FrontendParams -> IO (Module ())
readShortASTWithParseOptions progname options = do
  mbsrc <- lookupModuleSourceInLoadPath progname
  case mbsrc of
    Nothing -> do -- no source file, try to find shortAST file in load path
      loadpath <- getLoadPathForModule progname
      filename <- getFileInPath (shortASTFileName (takeFileName progname))
                                [""] loadpath
      readASTFile filename
    Just (dir,_) -> do
      callFrontendWithParams SAST options progname
      readASTFile (shortASTFileName (dir </> takeFileName progname))

-- | Reads the AST with further options from a specified module
readFullASTWithParseOptions :: String -> FrontendParams -> IO (Module ())
readFullASTWithParseOptions progname options = do
  mbsrc <- lookupModuleSourceInLoadPath progname
  case mbsrc of
    Nothing -> do -- no source file, try to find AST file in load path
      loadpath <- getLoadPathForModule progname
      filename <- getFileInPath (fullASTFileName (takeFileName progname))
                                [""] loadpath
      readASTFile filename
    Just (dir,_) -> do
      callFrontendWithParams AST options progname
      readASTFile (fullASTFileName (dir </> takeFileName progname))

-- | Get the short-AST filename of a Curry programm
shortASTFileName :: String -> String
shortASTFileName prog = inCurrySubdir (stripCurrySuffix prog) <.> "sast"

-- | Get the AST filename of a Curry programm
fullASTFileName :: String -> String
fullASTFileName prog = inCurrySubdir (stripCurrySuffix prog) <.> "ast"

-- | Reads the AST from a specified file
readASTFile :: String -> IO (Module ())
readASTFile filename = do
  filecontents <- readShortASTFileRaw filename
  -- read AST file...
  -- ...with generated Read class instances (slow!):
  --return (read filecontents)
  -- ...with built-in generic read operation (faster):
  return (readUnqualifiedTerm ["Curry.Types", "Curry.Ident", "Curry.Position",
                               "Curry.Span", "Curry.SpanInfo", "Prelude"]
                              filecontents)

-- | Reads the text from a specified file containing an AST
readShortASTFileRaw :: String -> IO String
readShortASTFileRaw filename = do
  extfcy <- doesFileExist filename
  if extfcy
   then readFile filename
   else do let subdirfilename = inCurrySubdir filename
           exdirtfcy <- doesFileExist subdirfilename
           if exdirtfcy
            then readFile subdirfilename
            else error ("EXISTENCE ERROR: AST file '" ++ filename ++
                        "' does not exist")