comparison src/compiler.sml @ 270:b9b02613c0c2

Parsing jobs
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 10:31:16 -0400
parents bacd0ba869e1
children 42dfb0d61cf0
comparison
equal deleted inserted replaced
269:fac9fae654e2 270:b9b02613c0c2
33 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens) 33 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
34 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData 34 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
35 structure Lex = Lex 35 structure Lex = Lex
36 structure LrParser = LrParser) 36 structure LrParser = LrParser)
37 37
38 type job = string list 38 type job = {
39 database : string option,
40 sources : string list
41 }
39 42
40 type ('src, 'dst) phase = { 43 type ('src, 'dst) phase = {
41 func : 'src -> 'dst, 44 func : 'src -> 'dst,
42 print : 'dst -> Print.PD.pp_desc 45 print : 'dst -> Print.PD.pp_desc
43 } 46 }
71 SOME v, 74 SOME v,
72 (name, elapsed) :: pmap) 75 (name, elapsed) :: pmap)
73 end 76 end
74 } 77 }
75 78
76 fun op o (tr1 : ('a, 'b) transform, tr2 : ('b, 'c) transform) = { 79 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
77 func = fn input => case #func tr1 input of 80 func = fn input => case #func tr1 input of
78 NONE => NONE 81 NONE => NONE
79 | SOME v => #func tr2 v, 82 | SOME v => #func tr2 v,
80 print = #print tr2, 83 print = #print tr2,
81 time = fn (input, pmap) => let 84 time = fn (input, pmap) => let
185 | _ => absyn 188 | _ => absyn
186 end 189 end
187 handle LrParser.ParseError => [], 190 handle LrParser.ParseError => [],
188 print = SourcePrint.p_file} 191 print = SourcePrint.p_file}
189 192
193 fun p_job {database, sources} =
194 let
195 open Print.PD
196 open Print
197 in
198 box [case database of
199 NONE => string "No database."
200 | SOME db => string ("Database: " ^ db),
201 newline,
202 string "Sources:",
203 p_list string sources,
204 newline]
205 end
206
207 fun trim s =
208 let
209 val (_, s) = Substring.splitl Char.isSpace s
210 val (s, _) = Substring.splitr Char.isSpace s
211 in
212 s
213 end
214
215 val parseUrp = {
216 func = fn filename =>
217 let
218 val dir = OS.Path.dir filename
219 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
220
221 fun readSources acc =
222 case TextIO.inputLine inf of
223 NONE => rev acc
224 | SOME line =>
225 let
226 val acc = if CharVector.all Char.isSpace line then
227 acc
228 else
229 let
230 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
231 (String.explode line))
232 val fname = OS.Path.concat (dir, fname)
233 in
234 fname :: acc
235 end
236 in
237 readSources acc
238 end
239
240 fun read database =
241 case TextIO.inputLine inf of
242 NONE => {database = database, sources = []}
243 | SOME "\n" => {database = database, sources = readSources []}
244 | SOME line =>
245 let
246 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
247 val cmd = Substring.string (trim cmd)
248 val arg = Substring.string (trim arg)
249 in
250 case cmd of
251 "database" =>
252 (case database of
253 NONE => ()
254 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
255 read (SOME arg))
256 | _ => (ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
257 read database)
258 end
259 in
260 read NONE
261 before TextIO.closeIn inf
262 end,
263 print = p_job
264 }
265
266 val toParseJob = transform parseUrp "parseJob"
267
190 fun capitalize "" = "" 268 fun capitalize "" = ""
191 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE) 269 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
192 270
193 val parse = { 271 val parse = {
194 func = fn fnames => 272 func = fn {database, sources = fnames} =>
195 let 273 let
196 fun nameOf fname = capitalize (OS.Path.file fname) 274 fun nameOf fname = capitalize (OS.Path.file fname)
197 275
198 fun parseOne fname = 276 fun parseOne fname =
199 let 277 let
228 end handle Empty => ds 306 end handle Empty => ds
229 end, 307 end,
230 print = SourcePrint.p_file 308 print = SourcePrint.p_file
231 } 309 }
232 310
233 val toParse = transform parse "parse" 311 val toParse = transform parse "parse" o toParseJob
234 312
235 val elaborate = { 313 val elaborate = {
236 func = fn file => let 314 func = fn file => let
237 val basis = #func parseUrs "lib/basis.urs" 315 val basis = #func parseUrs "lib/basis.urs"
238 in 316 in
239 Elaborate.elabFile basis ElabEnv.empty file 317 Elaborate.elabFile basis ElabEnv.empty file
240 end, 318 end,
241 print = ElabPrint.p_file ElabEnv.empty 319 print = ElabPrint.p_file ElabEnv.empty
242 } 320 }
243 321
244 val toElaborate = toParse o transform elaborate "elaborate" 322 val toElaborate = transform elaborate "elaborate" o toParse
245 323
246 val explify = { 324 val explify = {
247 func = Explify.explify, 325 func = Explify.explify,
248 print = ExplPrint.p_file ExplEnv.empty 326 print = ExplPrint.p_file ExplEnv.empty
249 } 327 }
250 328
251 val toExplify = toElaborate o transform explify "explify" 329 val toExplify = transform explify "explify" o toElaborate
252 330
253 val corify = { 331 val corify = {
254 func = Corify.corify, 332 func = Corify.corify,
255 print = CorePrint.p_file CoreEnv.empty 333 print = CorePrint.p_file CoreEnv.empty
256 } 334 }
257 335
258 val toCorify = toExplify o transform corify "corify" 336 val toCorify = transform corify "corify" o toExplify
259 337
260 val shake = { 338 val shake = {
261 func = Shake.shake, 339 func = Shake.shake,
262 print = CorePrint.p_file CoreEnv.empty 340 print = CorePrint.p_file CoreEnv.empty
263 } 341 }
264 342
265 val toShake1 = toCorify o transform shake "shake1" 343 val toShake1 = transform shake "shake1" o toCorify
266 344
267 val tag = { 345 val tag = {
268 func = Tag.tag, 346 func = Tag.tag,
269 print = CorePrint.p_file CoreEnv.empty 347 print = CorePrint.p_file CoreEnv.empty
270 } 348 }
271 349
272 val toTag = toShake1 o transform tag "tag" 350 val toTag = transform tag "tag" o toShake1
273 351
274 val reduce = { 352 val reduce = {
275 func = Reduce.reduce, 353 func = Reduce.reduce,
276 print = CorePrint.p_file CoreEnv.empty 354 print = CorePrint.p_file CoreEnv.empty
277 } 355 }
278 356
279 val toReduce = toTag o transform reduce "reduce" 357 val toReduce = transform reduce "reduce" o toTag
280 358
281 val specialize = { 359 val specialize = {
282 func = Specialize.specialize, 360 func = Specialize.specialize,
283 print = CorePrint.p_file CoreEnv.empty 361 print = CorePrint.p_file CoreEnv.empty
284 } 362 }
285 363
286 val toSpecialize = toReduce o transform specialize "specialize" 364 val toSpecialize = transform specialize "specialize" o toReduce
287 365
288 val toShake2 = toSpecialize o transform shake "shake2" 366 val toShake2 = transform shake "shake2" o toSpecialize
289 367
290 val monoize = { 368 val monoize = {
291 func = Monoize.monoize CoreEnv.empty, 369 func = Monoize.monoize CoreEnv.empty,
292 print = MonoPrint.p_file MonoEnv.empty 370 print = MonoPrint.p_file MonoEnv.empty
293 } 371 }
294 372
295 val toMonoize = toShake2 o transform monoize "monoize" 373 val toMonoize = transform monoize "monoize" o toShake2
296 374
297 val mono_opt = { 375 val mono_opt = {
298 func = MonoOpt.optimize, 376 func = MonoOpt.optimize,
299 print = MonoPrint.p_file MonoEnv.empty 377 print = MonoPrint.p_file MonoEnv.empty
300 } 378 }
301 379
302 val toMono_opt1 = toMonoize o transform mono_opt "mono_opt1" 380 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
303 381
304 val untangle = { 382 val untangle = {
305 func = Untangle.untangle, 383 func = Untangle.untangle,
306 print = MonoPrint.p_file MonoEnv.empty 384 print = MonoPrint.p_file MonoEnv.empty
307 } 385 }
308 386
309 val toUntangle = toMono_opt1 o transform untangle "untangle" 387 val toUntangle = transform untangle "untangle" o toMono_opt1
310 388
311 val mono_reduce = { 389 val mono_reduce = {
312 func = MonoReduce.reduce, 390 func = MonoReduce.reduce,
313 print = MonoPrint.p_file MonoEnv.empty 391 print = MonoPrint.p_file MonoEnv.empty
314 } 392 }
315 393
316 val toMono_reduce = toUntangle o transform mono_reduce "mono_reduce" 394 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
317 395
318 val mono_shake = { 396 val mono_shake = {
319 func = MonoShake.shake, 397 func = MonoShake.shake,
320 print = MonoPrint.p_file MonoEnv.empty 398 print = MonoPrint.p_file MonoEnv.empty
321 } 399 }
322 400
323 val toMono_shake = toMono_reduce o transform mono_shake "mono_shake1" 401 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
324 402
325 val toMono_opt2 = toMono_shake o transform mono_opt "mono_opt2" 403 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
326 404
327 val cjrize = { 405 val cjrize = {
328 func = Cjrize.cjrize, 406 func = Cjrize.cjrize,
329 print = CjrPrint.p_file CjrEnv.empty 407 print = CjrPrint.p_file CjrEnv.empty
330 } 408 }
331 409
332 val toCjrize = toMono_opt2 o transform cjrize "cjrize" 410 val toCjrize = transform cjrize "cjrize" o toMono_opt2
333 411
334 fun compileC {cname, oname, ename} = 412 fun compileC {cname, oname, ename} =
335 let 413 let
336 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname 414 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname
337 val link = "gcc -pthread -O3 clib/urweb.o " ^ oname ^ " clib/driver.o -o " ^ ename 415 val link = "gcc -pthread -O3 clib/urweb.o " ^ oname ^ " clib/driver.o -o " ^ ename