Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/inputs/cmd-line-inputs.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/inputs/cmd-line-inputs.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (view) (download)
Original Path: trunk/src/compiler/common/inputs.sml

1 : jhr 230 (* inputs.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 230 * All rights reserved.
5 :     *
6 :     * Support for processing static inputs to a program
7 :     *)
8 :    
9 :     structure Inputs : sig
10 :    
11 : jhr 340 (* returns true if the argument has the form of a command-line input specification *)
12 :     val isCmdLineInput : string -> bool
13 :    
14 : jhr 230 (* initialize the table from the command-line arguments; returns false if there was
15 :     * an error processing an argument.
16 :     *)
17 :     val initFromArgs : string list -> bool
18 :    
19 :     (* getInput(name, fromString, optDefault) gets the value of the named input.
20 :     * if the value is not defined in the table and there is a default, then the
21 :     * default is returned. Otherwise it reads it from standard input
22 :     *)
23 :     val getInput : string * (string -> 'a option) * 'a option -> 'a option
24 :    
25 :     end = struct
26 :    
27 :     structure Tbl = AtomTable
28 :    
29 :     val inputs : string AtomTable.hash_table = AtomTable.mkTable(32, Fail "input table")
30 :    
31 :     fun notEq #"=" = false | notEq _ = true
32 :    
33 : jhr 340 fun isCmdLineInput s = String.isPrefix "-D" s
34 :    
35 : jhr 230 fun initFromArgs args = let
36 :     fun doArg arg = let
37 :     val arg = Substring.full arg
38 :     in
39 :     if Substring.isPrefix "-D" arg
40 :     then let
41 :     val arg = Substring.triml 2 arg
42 :     val (name, rest) = Substring.splitl notEq arg
43 :     in
44 :     if (Substring.size name > 0) andalso (Substring.size rest > 1)
45 :     then (
46 :     AtomTable.insert inputs
47 :     (Atom.atom' name, Substring.string(Substring.triml 1 rest));
48 :     true)
49 :     else false
50 :     end
51 :     else true
52 :     end
53 :     in
54 :     AtomTable.clear inputs;
55 :     List.all doArg args
56 :     end
57 :    
58 :     fun getInput (name, fromString : string -> 'a option, optDefault : 'a option) = let
59 :     fun cvt s = (case fromString s
60 :     of NONE => (
61 :     TextIO.output(TextIO.stdErr, concat[
62 :     "ERROR: value \"", String.toString s, "\" for input ", name,
63 :     " is invalid\n"
64 :     ]);
65 :     NONE)
66 :     | someVal => someVal
67 :     (* end case *))
68 :     val name' = Atom.atom name
69 :     in
70 :     case AtomTable.find inputs name'
71 :     of NONE => (case optDefault
72 :     of NONE => (
73 :     TextIO.output(TextIO.stdOut, concat[
74 :     "input value for ", name, ": "
75 :     ]);
76 :     TextIO.flushOut TextIO.stdOut;
77 :     case TextIO.inputLine TextIO.stdIn
78 : jhr 235 of SOME ln => let
79 :     val ln = String.substring(ln, 0, size ln - 1)
80 :     in
81 :     AtomTable.insert inputs (name', ln); (* cache value *)
82 :     cvt ln
83 :     end
84 : jhr 230 | NONE => OS.Process.exit OS.Process.failure
85 :     (* end case *))
86 :     | someVal => someVal
87 :     (* end case *))
88 :     | SOME v => fromString v
89 :     (* end case *)
90 :     end
91 :    
92 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0