forked from codeon/cmlex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sml
64 lines (57 loc) · 2.39 KB
/
main.sml
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
functor MainFun (structure Parser : PARSER
structure Codegen : CODEGEN
val extension : string) =
struct
exception Error = Process.Error
fun main infile outfile =
let
val ins = TextIO.openIn infile
val program =
Parser.parse (Stream.fromTextInstream ins)
handle exn =>
(
TextIO.closeIn ins;
raise exn
)
val () = TextIO.closeIn ins
in
Codegen.writeProgram outfile (Process.process program)
end
exception Quit of string
fun mainCmd name (_, args) =
let
(* Parse arguments *)
val infile: string option ref = ref NONE
val outfile: string option ref = ref NONE
fun loop [] = ()
| loop ("-o" :: file :: args) =
if isSome (!outfile) then raise Quit "too many output files"
else (outfile := SOME file; loop args)
| loop (file :: args) =
if isSome (!infile) then raise Quit "too many input files"
else (infile := SOME file; loop args)
val () = loop args
(* Validate arguments *)
val infile =
case (!infile) of
NONE => raise Quit "not enough input files"
| SOME file => file
val outfile =
case (!outfile) of
NONE => OS.Path.joinBaseExt {base = infile, ext = SOME extension}
| SOME file => file
in
main infile outfile; OS.Process.success
end handle Process.Error => OS.Process.failure
| Parser.Error => OS.Process.failure
| Codegen.Error => OS.Process.failure
| Quit msg =>
(print ("Error: " ^ msg ^ "\n\
\Usage: " ^ name ^ " file.cmlex [-o file." ^ extension ^ "]\n\
\(Default output file is file.cmlex." ^ extension ^ ")\n")
; OS.Process.failure)
| exn =>
(print ("Failed with exception: " ^ exnName exn ^ "\n")
; print ("[" ^ exnMessage exn ^ "]\n")
; OS.Process.failure)
end