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
|
implementation module compile
import StdEnv
import frontend
import backendinterface
import CoclSystemDependent
import RWSDebug
:: CoclOptions =
{
moduleName
:: {#Char}
, pathName
:: {#Char}
, errorPath
:: {#Char}
, errorMode
:: Int
, outPath
:: {#Char}
, outMode
:: Int
, searchPaths
:: SearchPaths
}
InitialCoclOptions =
{ moduleName
= ""
, pathName
= ""
, errorPath
= "errors"
, errorMode
= FWriteText
, outPath
= "messages"
, outMode
= FWriteText
, searchPaths
// RWS, voor Maarten +++ = {sp_locations = [], sp_paths = []}
= []
}
compile :: [{#Char}] *Files -> (!Bool, !*Files)
compile args files
= compileModule (parseCommandLine args InitialCoclOptions) args files
parseCommandLine :: [{#Char}] CoclOptions -> CoclOptions
parseCommandLine [] options
= prependModulePath options
where
// RWS +++ hack, both module name and file path should be passed to frontEndInterface
prependModulePath options=:{pathName, searchPaths}
= { options
& moduleName = baseName pathName
// RWS, voor Maarten +++ , searchPaths = {searchPaths & sp_paths = [directoryName pathName : searchPaths.sp_paths]}
, searchPaths = [directoryName pathName : searchPaths]
}
parseCommandLine ["-P", searchPathsString : args] options=:{searchPaths}
// RWS, voor Maarten +++ = parseCommandLine args {options & searchPaths = {searchPaths & sp_paths = splitPaths searchPathsString}}
= parseCommandLine args {options & searchPaths = splitPaths searchPathsString}
parseCommandLine ["-RO", outPath : args] options
= parseCommandLine args {options & outPath = stripQuotes outPath, outMode = FWriteText}
parseCommandLine ["-RAO", outPath : args] options
= parseCommandLine args {options & outPath = stripQuotes outPath, outMode = FAppendText}
parseCommandLine ["-RE", errorPath : args] options
= parseCommandLine args {options & errorPath = stripQuotes errorPath, errorMode = FWriteText}
parseCommandLine ["-RAE", errorPath : args] options
= parseCommandLine args {options & errorPath = stripQuotes errorPath, errorMode = FAppendText}
parseCommandLine [arg : args] options
| arg.[0] == '-'
= parseCommandLine args options
// otherwise
= parseCommandLine args {options & pathName = stripExtension ".icl" (stripQuotes arg)}
stripExtension :: {#Char} {#Char} -> {#Char}
stripExtension extension string
| stringSize >= extensionSize && (string % (stringSize-extensionSize, stringSize-1)) == extension
= string % (0, stringSize-extensionSize-1)
// otherwise
= string
where
stringSize
= size string
extensionSize
= size extension
stripQuotes :: {#Char} -> {#Char}
stripQuotes string
| stringSize > 1 && string.[0] == '"' && string.[stringSize-1] == '"'
= string % (1, stringSize-2)
// otherwise
= string
where
stringSize
= size string
splitPaths :: {#Char} -> [{#Char}]
splitPaths paths
= [path +++ {DirectorySeparator} \\ path <- splitBy PathSeparator paths]
splitBy :: Char {#Char} -> [{#Char}]
splitBy char string
= splitBy` 0 0
where
splitBy` frm to
| to >= stringSize
= [string % (frm, to-1)]
| string.[to] == char
= [string % (frm, to-1) : splitBy` (to+1) (to+1)]
// otherwise
= splitBy` frm (to+1)
stringSize
= size string
baseName :: {#Char} -> {#Char}
baseName path
= last (splitBy DirectorySeparator path)
directoryName :: {#Char} -> {#Char}
directoryName path
= foldr (\p ps -> p +++ {DirectorySeparator} +++ ps) "" (init (splitBy DirectorySeparator path))
compileModule :: CoclOptions [{#Char}] *Files -> (!Bool, !*Files)
compileModule options commandLineArgs files
# (opened, error, files)
= fopen options.errorPath options.errorMode files
| not opened
= abort ("couldn't open error file \"" +++ options.errorPath +++ "\"\n")
# (opened, out, files)
= fopen options.outPath options.outMode files
| not opened
= abort ("couldn't open out file \"" +++ options.outPath +++ "\"\n")
# (io, files)
= stdio files
# (predefSymbols, hashTable) = buildPredefinedSymbols newHashTable
(moduleIdent, hashTable) = putIdentInHashTable options.moduleName IC_Module hashTable
# (predefs, _, files, error, io, out, optionalSyntaxTree)
= frontEndInterface moduleIdent options.searchPaths predefSymbols hashTable files error io out
# (closed, files)
= fclose io files
| not closed
= abort ("couldn't close stdio")
# (closed, files)
= fclose out files
| not closed
= abort ("couldn't close out file \"" +++ options.outPath +++ "\"\n")
# (success, error, files)
= case optionalSyntaxTree of
Yes syntaxTree
-> backEndInterface outputPath (map appendRedirection commandLineArgs) predefs syntaxTree error files
with
appendRedirection arg
= case arg of
"-RE"
-> "-RAE"
"-RO"
-> "-RAO"
arg
-> arg
No
-> (False, error, files)
with
outputPath
// = /* directoryName options.pathName +++ "Clean System Files" +++ {DirectorySeparator} +++ */ baseName options.pathName
= baseName options.pathName
# (closed, files)
= fclose error files
| not closed
= abort ("couldn't close error file \"" +++ options.errorPath +++ "\"\n")
= (success, files)
|