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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/common/inputs.sml
ViewVC logotype

Annotation of /trunk/src/compiler/common/inputs.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 235 - (view) (download)

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

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