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
--- ----------------------------------------------------------------------------
--- This module provides operations for reading TypedFlatCurry files.
---
--- @author  Jan Tikovsky
--- @version December 2018
--- ----------------------------------------------------------------------------
module ReadTFCY where

import Directory    (doesFileExist)
import FileGoodies  (getFileInPath)
import FilePath     ((<.>), (</>), takeFileName)
import ReadShowTerm (readUnqualifiedTerm)

import FlatCurry.Annotated.Types
import System.CurryPath    ( getLoadPathForModule, inCurrySubdir
                           , lookupModuleSourceInLoadPath, stripCurrySuffix )
import System.FrontendExec ( FrontendParams, FrontendTarget (TFCY), addTarget
                           , callFrontendWithParams, defaultParams, setQuiet )

--- Parse a Curry program and return corresponding TypedFlatCurry program
readTFCY :: String -> IO (AProg TypeExpr)
readTFCY progname = readTFCYWithParseOptions progname
  (addTarget TFCY (setQuiet True defaultParams))

--- Parse a Curry program with parsing options and return corresponding
--- TypedFlatCurry program
readTFCYWithParseOptions :: String -> FrontendParams -> IO (AProg TypeExpr)
readTFCYWithParseOptions progname options = do
  mbsrc <- lookupModuleSourceInLoadPath progname
  case mbsrc of
    Nothing -> do -- no source file, try to find FlatCurry file in load path:
      loadpath <- getLoadPathForModule progname
      filename <- getFileInPath (tfcyFile (takeFileName progname)) [""]
                                loadpath
      readTFCYFile filename
    Just (dir,_) -> do
      callFrontendWithParams TFCY options progname
      readTFCYFile (tfcyFile (dir </> takeFileName progname))

--- Transform name of a Curry program into file name of corresponding
--- TypedFlatCurry program
tfcyFile :: String -> String
tfcyFile prog = inCurrySubdir (stripCurrySuffix prog) <.> "tfcy"

--- Reads a TypedFlatCurry program from a file in ".tfcy" format
readTFCYFile :: String -> IO (AProg TypeExpr)
readTFCYFile fn = do
  exafcy <- doesFileExist fn
  if exafcy
   then readExistingTFCY fn
   else do let subdirfilename = inCurrySubdir fn
           exdirfcy <- doesFileExist subdirfilename
           if exdirfcy
            then readExistingTFCY subdirfilename
            else error ("EXISTENCE ERROR: TypedFlatCurry file '" ++ fn ++
                        "' does not exist")
 where
 readExistingTFCY fname = do
   filecontents <- readFile fname
   return $ readUnqualifiedTerm
     ["FlatCurry.Types","FlatCurry.Annotated.Types","Prelude"] filecontents