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
------------------------------------------------------------------------------
--- This library defines I/O actions to read and write
--- annotated FlatCurry programs.
---
--- @author Michael Hanus
--- @version December 2020
------------------------------------------------------------------------------

module FlatCurry.Annotated.Files where

import System.CurryPath    ( lookupModuleSourceInLoadPath
                           , inCurrySubdir, stripCurrySuffix )
import System.Directory    ( doesFileExist )
import System.FilePath     ( (</>), (<.>) )

import FlatCurry.Annotated.Types

--- Transforms a name of a Curry program (with or without suffix ".curry"
--- or ".lcurry") into the name of the file containing the
--- corresponding annotated FlatCurry program.
annotatedFlatCurryFileName :: String -> String
annotatedFlatCurryFileName prog =
  inCurrySubdir (stripCurrySuffix prog) <.> "afcy"

--- Gets the standard annotated FlatCurry file location
--- for a given Curry module name.
--- The Curry source program must exist in the Curry load path,
--- otherwise an error is raised.
annotatedFlatCurryFilePath :: String -> IO String
annotatedFlatCurryFilePath mname = do
  mbsrc <- lookupModuleSourceInLoadPath mname
  case mbsrc of
    Nothing      -> error $ "Curry source file for module '" ++ mname ++
                            "' not found!"
    Just (dir,_) -> return (annotatedFlatCurryFileName (dir </> mname))

--- where the file name is provided as the argument.
readAnnotatedFlatCurryFile :: Read a => String -> IO (AProg a)
readAnnotatedFlatCurryFile filename = do
  filecontents <- readAnnotatedFlatCurryFileRaw filename
  return (read filecontents)
 where
  readAnnotatedFlatCurryFileRaw fname = do
    exafcy <- doesFileExist fname
    if exafcy
      then readFile fname
      else do
        let subdirfilename = inCurrySubdir fname
        exdirafcy <- doesFileExist subdirfilename
        if exdirafcy
          then readFile subdirfilename
          else error $ "EXISTENCE ERROR: Annotated FlatCurry file '" ++
                       fname ++ "' does not exist"

--- Writes an annotated FlatCurry program into a file in `.afcy` format.
--- The file is written in the standard location for intermediate files,
--- i.e., in the 'annotatedFlatCurryFileName' relative to the directory of the
--- Curry source program (which must exist!).
writeAnnotatedFlatCurry :: Show a => AProg a -> IO ()
writeAnnotatedFlatCurry prog@(AProg mname _ _ _ _) = do
  fname <- annotatedFlatCurryFilePath mname
  writeAnnotatedFlatCurryFile fname prog

--- Writes an annotated FlatCurry program into a file in ".afcy" format.
--- The first argument must be the name of the target file
--- (with suffix `.afcy`).
writeAnnotatedFlatCurryFile :: Show a => String -> AProg a -> IO ()
writeAnnotatedFlatCurryFile file prog = writeFile file (show prog)