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 3291 - (view) (download)

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

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