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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
module FlatCurry.TypeAnnotated.Files where
import Data.List ( isSuffixOf )
import Data.Maybe ( isNothing )
import System.FilePath ( takeFileName, (</>), (<.>) )
import System.Directory ( doesFileExist, getFileWithSuffix )
import System.FrontendExec ( FrontendParams, FrontendTarget (..), defaultParams
, setQuiet
, callFrontend, callFrontendWithParams )
import System.CurryPath ( lookupModuleSourceInLoadPath, getLoadPathForModule
, inCurrySubdir, stripCurrySuffix )
import ReadShowTerm ( readUnqualifiedTerm, showTerm )
import FlatCurry.Annotated.Types
typeAnnotatedFlatCurryFileName :: String -> String
typeAnnotatedFlatCurryFileName prog =
inCurrySubdir (stripCurrySuffix prog) <.> "afcy"
typeAnnotatedFlatCurryFilePath :: String -> IO String
typeAnnotatedFlatCurryFilePath mname = do
mbsrc <- lookupModuleSourceInLoadPath mname
case mbsrc of
Nothing -> error $ "Curry source file for module '" ++ mname ++
"' not found!"
Just (dir,_) -> return (typeAnnotatedFlatCurryFileName (dir </> mname))
readTypeAnnotatedFlatCurry :: String -> IO (AProg TypeExpr)
readTypeAnnotatedFlatCurry progname =
readTypeAnnotatedFlatCurryWithParseOptions progname
(setQuiet True defaultParams)
readTypeAnnotatedFlatCurryWithParseOptions :: String -> FrontendParams
-> IO (AProg TypeExpr)
readTypeAnnotatedFlatCurryWithParseOptions progname options = do
mbsrc <- lookupModuleSourceInLoadPath progname
case mbsrc of
Nothing -> do
loadpath <- getLoadPathForModule progname
filename <- getFileWithSuffix
(typeAnnotatedFlatCurryFileName (takeFileName progname))
[""] loadpath
readTypeAnnotatedFlatCurryFile filename
Just (dir,_) -> do
callFrontendWithParams TAFCY options progname
readTypeAnnotatedFlatCurryFile
(typeAnnotatedFlatCurryFileName (dir </> takeFileName progname))
readTypeAnnotatedFlatCurryFile :: String -> IO (AProg TypeExpr)
readTypeAnnotatedFlatCurryFile filename = do
filecontents <- readTypeAnnotatedFlatCurryFileRaw filename
return (readUnqualifiedTerm ["FlatCurry.Annotated.Types", "FlatCurry.Types",
"Prelude"]
filecontents)
where
readTypeAnnotatedFlatCurryFileRaw fname = do
exafcy <- doesFileExist fname
if exafcy
then readFile fname
else error $ "EXISTENCE ERROR: Typed FlatCurry file '" ++
fname ++ "' does not exist"
writeTypeAnnotatedFlatCurry :: AProg TypeExpr -> IO ()
writeTypeAnnotatedFlatCurry prog@(AProg mname _ _ _ _) = do
fname <- typeAnnotatedFlatCurryFilePath mname
writeTypeAnnotatedFlatCurryFile fname prog
writeTypeAnnotatedFlatCurryFile :: String -> AProg TypeExpr -> IO ()
writeTypeAnnotatedFlatCurryFile file prog = writeFile file (showTerm prog)
|