-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcess.hs
More file actions
58 lines (54 loc) · 1.96 KB
/
Process.hs
File metadata and controls
58 lines (54 loc) · 1.96 KB
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
module Process(process) where
import Text.ParserCombinators.Parsec
import System.Directory
import System.IO
import System.Exit
import Data.List
import Data.Maybe
import Control.Monad
import Lexer
import Preprocess
import ParseFile
import Options
import DataTypes
import PrintFile
getFilePath file foundPathIO newPath = do
foundPath <- foundPathIO
case foundPath of
Just path -> (return . Just) path
Nothing -> do
let newF = newPath ++ "/" ++ file ++ ".spec"
fileExists <- doesFileExist newF
if fileExists
then (return . Just) newF
else return Nothing
process options seenPortsAliasesIO file = do
seenPortsAliases <- seenPortsAliasesIO
filePath <- foldl (getFilePath file) (return Nothing) $ optIncludes options
case filePath of
Nothing -> do
hPutStrLn stderr $ "File " ++ file ++ " not found!"
exitFailure
Just name -> do
input <- readFile name
case (runParser (whiteSpace>>parseFile) () name $ preprocess input) of
Left err -> do
putStrLn $ "Error in file " ++ file
print err
exitFailure
Right elements -> do
let imports = [x | Include x <- elements, isNothing (find (\(file, _, _) -> x == file) seenPortsAliases)]
let ports = [x | x@(Port {}) <- elements]
let aliases = [x | x@(Alias {}) <- elements]
fullPortsAliasesList <- foldl (process options) (return $ (file, ports, aliases):seenPortsAliases) imports
let bsvName = optOutDir options ++ "/" ++ file ++ ".bsv"
output <- printFile file fullPortsAliasesList elements
let writeBsv = writeFile bsvName output
bsvExists <- doesFileExist bsvName
if bsvExists
then do
specModTime <- getModificationTime name
bsvModTime <- getModificationTime bsvName
when (specModTime >= bsvModTime || optForce options) writeBsv
else writeBsv
return fullPortsAliasesList