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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
module CPC.Main (main) where


import CMeta ( mkCMeta
             , metaCreate
             , BoxLabel(..)
             , metaName
             , readMeta'
             , getBoxes )
import IO ( hPutStr
          , stdout
          , stderr
          , hPutStrLn )
import FileGoodies (stripSuffix, fileSuffix)
import FilePath ( takeBaseName
                , takeDirectory
                , (<.>)
                , replaceBaseName
                , replaceExtension
                , (</>)
                , splitFileName )
import Directory ( getCurrentDirectory
                 , getDirectoryContents
                 , removeFile
                 , createDirectoryIfMissing
                 , doesFileExist
                 , getHomeDirectory )
import Distribution ( callFrontendWithParams
                    , defaultParams
                    , stripCurrySuffix
                    , addCurrySubdir
                    , FrontendTarget(..)
                    , installDir)
import CData                       (mkMap)
import CPC.Report                  (report)
import CPC.Files                   (cpcSubdir, inCpcSubdir, addInstSuffix
                                   , hasInstSuffix )
import CPC.Pretty                  (showDProg)
import CPC.Instrumentation         ( startTransform )
import CPC.Transform               (transMain)
import CTrack
import AnsiiToHtml                 (convertToHTML)
import ArityMap                    (buildArityMap)
import Unsafe                      (unsafePerformIO)
import ReadShowTerm                (readQTerm, readUnqualifiedTerm)
import System                      (exitWith, getArgs, system)
import ACSpans.ExtendAbstractCurry (eProg)
import ACSpans.AddSpans            (apModule)
import ACSpans.SortSplit           (sortSplitModule)
import ACSpans.SpanUtils           (filterTS)
import AbstractCurry.Files         (readCurry)
import AbstractCurry.Select        (imports)
import IOExts                      (evalCmd)
import ACSpans.Pretty              (showCProg)
import ACSpans.Select              (progName, imports)
import ACSpans.AbstractCurrySpan   (CurryProg)
import ACSpans.Transform           (renameCurryModule)
import qualified FiniteMap as FMA  (fmToList, lookupFM, FM, emptyFM)
import qualified FMInt as FMI      (fmToList)
import GetOpt
import AnsiCodes
import List                        (nub)

import CPC.PackageConfig           (packagePath)

-- The path for imported run-time libraries (CLib, CTrack,...)
cpcImportPath :: String
cpcImportPath = packagePath </> "src"

main:: IO ()
main = do
  (args, files) <- getArgs >>= parse
  putStrLn cpcBanner
  putStrLn $ texcol"Setted Flags: " ++ show args
  putStrLn $ texcol"Given Files: " ++ show (files)
  cpc args files

cpc:: [Flag] -> [String] -> IO()
cpc _ [] = putStrLn "Use cpc --help for instructions."
cpc args (file:files) = do

  let paths = map ((</> cpcSubdir) . fst . splitFileName) files
  createDirectoryIfMissing False cpcSubdir
  mapIO_ (createDirectoryIfMissing False) paths

  let mainFiles = filter isMainFlag args
      (mainFile, imps)  = case mainFiles of
                           []          -> (file,files)
                           [Main name] -> ((stripCurrySuffix name),(file:files))
                           _           -> error "Error: CaseMain"

  when (Clean `elem` args) $ clean' file
  arityMap <- if (Optim `elem` args)
                then buildArityMap (mainFile:imps)
                else return (FMA.emptyFM (<))
  print arityMap

  putStrLn $ texcol"Starting transformation for all given files..."
  (_, spanProg, insProg, lastI, newProgName, _, datas)
    <- handlingTrans mainFile imps 0 True args arityMap

  let spanProgMain = if any isMainFlag args then [] else [spanProg]
  (_, spanProgs) <- case imps of
                              []     -> return (lastI, spanProgMain)
                              (_:_)  -> handlingImports' (lastI, spanProgMain)
                                        args imps arityMap
  putStrLn $ texcolr "Transformations: Done."
 {-                                       
  putStrLn "Writing cpix..."
  writeFile (inCpcSubdir $ replaceExtension mainFile "cpix") (show lastId)-}



  putStrLn $ texcol "Reading cpcrc..."
  homeDir  <- getHomeDirectory
  putStrLn $ texcolr "Reading cpcrc: Done."

  putStrLn $ texcol ("Compile instrumented program: " ++ newProgName)
  (x,y,z) <- evalCmd (installDir </> "bin" </> "curry")
               [ ":set", "path", cpcImportPath
               , ":load", replaceExtension newProgName "curry"
               , ":save", ":quit"] ""


  putStrLn y
  putStrLn z
  putStrLn $ texcolr "Compiling: Done."

  putStrLn $ texcol "Evaluate instrumented program..."
  let binProgName = "." </> stripCurrySuffix newProgName
  system binProgName
  removeFile binProgName
  putStrLn $ texcolr "Evaluation: Done."

  putStrLn $ texcol "Calculating desired options..."
  when (TReport `elem` args) $ mapIO_ (report mainFile) (file:files)
  when (SpanProg `elem` args) $ mapIO_ print $ spanProgs
  when (Ticks `elem` args) $ print (FMI.fmToList $
                                    eticks (unsafePerformIO (readTrack $
                                                             getTrackName
                                                             mainFile)))
  when (Evaluate `elem` args) $  putStrLn "In Work."
  when (MetaInfo `elem` args) $ mapIO_
                                (print .
                                 getBoxes . unsafePerformIO
                                          . readMeta' . (metaName ""))
                                (file:files)
  when (Pretty `elem` args) $  mapIO_ (prettyPrint mainFile) $
                                      (reverse spanProgs)
  when (HTML `elem` args) $ mapIO_ (convertToHTML)
                                   (map (ansiProg mainFile)
                                        (reverse spanProgs))


isMainFlag :: Flag -> Bool
isMainFlag f = case f of
                    Main _ -> True
                    _      -> False

prettyPrint:: String -> ACSpans.AbstractCurrySpan.CurryProg -> IO()
prettyPrint file spanProg = let (ansi,_) = ansiProg file spanProg
                            in  hPutStr stdout $ "\n" ++ ansi ++ "\n"

ansiProg:: String -> ACSpans.AbstractCurrySpan.CurryProg -> (String,String)
ansiProg file spanProg = let cdatas = mkMap (progName spanProg) file
                             ansi   = showDProg (unsafePerformIO cdatas)
                                                 spanProg
                         in (ansi,(progName spanProg))

handlingTrans :: String -> [String] -> Int -> Bool -> [Flag]
              -> FMA.FM (String, String) Int
              -> IO ([String]
                    , ACSpans.AbstractCurrySpan.CurryProg
                    , ACSpans.AbstractCurrySpan.CurryProg
                    , Int, String, String
                    , IO (FMA.FM ((Prelude.Int, Prelude.Int)
                                 ,(Prelude.Int, Prelude.Int))
                                 [CMeta.BoxLabel]))

handlingTrans file files offset rename args arMap = do

         callFrontendWithParams TOKS defaultParams file
         callFrontendWithParams CY defaultParams file
         callFrontendWithParams ACY defaultParams file

         tokStr          <- readFile $ inCurrySubdir
                                     $ replaceExtension file "tokens"
         astStr          <- readFile $ inCurrySubdir
                                     $ replaceExtension file "cy"
         prog            <- readCurry file

         let toks     = filterTS (readQTerm tokStr)
             ast      = sortSplitModule $
                          readUnqualifiedTerm ["Prelude", "ACSpans.AST"] astStr
             spanAST  = fst $ apModule ast toks
             spanProg = eProg prog spanAST
             impChoice= case files of
                             [] -> []
                             xs -> map (repl . stripCurrySuffix) xs -- robust
             (insMain,(lastId,boxes)) = startTransform offset spanProg
                                                       impChoice rename
                                                       ((any isMainFlag args)
                                                         && offset==0)
                                                       arMap
             insProg = if offset == 0 then transMain insMain file else insMain
             newProg = showCProg $ if rename
                                     then renameCurryModule
                                            (addInstSuffix (progName insProg))
                                            insProg
                                     else insProg
             cmeta = mkCMeta boxes
             datas = mkMap file file
             newProgName =  replaceBaseName file
                              (addInstSuffix (takeBaseName file))
             repl s      = map (\c -> if c == '/' then '.' else c) s

         putStrLn $ texcol ("Writing " ++ newProgName ++ ".curry...")
         writeFile (replaceExtension newProgName "curry") newProg
         putStrLn $ texcolr("Writing " ++ newProgName ++ ".curry: Done.")

         putStrLn $ texcol("Writing " ++ file ++ ".cmeta...")
         metaCreate file cmeta
         putStrLn $ texcolr("Writing " ++ file ++ ".cmeta: Done.")

         return (impChoice,spanProg,insProg
                ,lastId,newProgName, newProg, datas)

handlingImports':: (Int, [CurryProg]) -> [Flag] -> [String]
                -> (FMA.FM (String,String) Int) -> IO (Int, [CurryProg])
handlingImports' (offset, spanProgs) args files arM =
  do -- files
    let choice  = case files of
                       []     -> []
                       (x:xs) -> (x:xs)
    handlingImports (offset, spanProgs) choice args arM

handlingImports:: (Int, [CurryProg]) -> [String] -> [Flag]
               -> (FMA.FM (String,String) Int) -> IO (Int, [CurryProg])
handlingImports (offset, spanProgs) (imp:imprts) args arM =
  do
    (_,spanProg,_,newId,_,_,_) <- handlingTrans imp [] offset
                                                True args arM
    handlingImports (newId, spanProg:spanProgs) imprts args arM
handlingImports (offset, spanProgs) [] _ _ =
  do
    return (offset, spanProgs)


data Flag =
     TReport               -- -t
   | Evaluate              -- -e
   | SpanProg              -- -s
   | Ticks                 -- -x
   | MetaInfo              -- -m
   | Main String           -- --main
   | Pretty                -- -p
   | Optim                 -- -o
   | Clean                 -- -c
   | HTML                  -- -h
   | Help                  -- -help



flags :: [GetOpt.OptDescr Flag]
flags =
       [Option ['t'] []       (NoArg TReport)
            "Print a statistical report of the given files."
       ,Option ['s'] []       (NoArg SpanProg)
            "Print the calculated files with span information."
       ,Option ['e'] []       (NoArg Evaluate)
            "Run the instrumented code and collects ticks data."
       ,Option ['x'] []       (NoArg Ticks)
            "Print the collected ticks."
       ,Option ['m'] []       (NoArg MetaInfo)
           "Print the collected meta information."
       ,Option ['p'] []       (NoArg Pretty)
           "Pretty print the files with colored ticks."
       ,Option ['o'] []       (NoArg Optim)
           "Optimize ticks."
       ,Option ['h'] []       (NoArg HTML)
           "Build a HTML colored output."
       ,Option ['c'] []       (NoArg Clean)
           "Clean old data files before execution."
       ,Option [] ["main"]    (ReqArg Main "MAIN")
           "Do not instrument module MAIN for program coverage."
       ,Option []    ["help"] (NoArg Help)
           "Print this help message"
       ]

inCurrySubdir :: String -> String
inCurrySubdir fn = let (path, file) = splitFileName fn
                   in addCurrySubdir path </> file

parse:: [String] -> IO ([Flag], [String])
parse argv = case getOpt Permute flags argv of
        (args,fs,[]) -> do
            let files = if null fs then ["-"] else fs
            if Help `elem` args
              then do putStrLn cpcBanner
                      hPutStrLn stderr (usageInfo header flags)
                      exitWith 0
              else return (nub args, files)

        (_,_,errs)   -> do
            hPutStrLn stderr (concat errs ++ usageInfo header flags)
            exitWith 1
 where header = "Usage: cpc -option [modules]"

clean :: String -> IO ()
clean dir = do
  contents <- getDirectoryContents dir
  sequenceIO_ (map removeFile [dir++file| file <- contents
                                        , (fileSuffix file == "ctrack")
                                       || (fileSuffix file == "cmeta") ])
  sequenceIO_ (map removeFile [dir++file| file <- contents
                                        , hasInstSuffix (takeBaseName file)])
  putStrLn "Cleaned."

clean' :: String -> IO ()
clean' file = do
  contents <- getDirectoryContents ".cpc"
  mapIO_ removeFile [ ".cpc/"++fil | fil <- contents
                                   , takeBaseName fil == file ]
  return ()

-------------------------------------------------------------------------------
texcol:: String -> String
texcol = blue

texcolr:: String -> String
texcolr = green

cpcBanner:: String
cpcBanner =
  texcol "  ___   ___    ___ \n" ++
  texcol " /  _) |   \\  /  _)\n" ++
  texcol "(  (_  | __/ (  (_ \n" ++
  texcol " \\___) |_|    \\___)\n"