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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2011 - (download) (annotate)
Sun Oct 7 21:14:31 2012 UTC (7 years ago) by jhr
File size: 2601 byte(s)
  Working on inputs
(* cmd-line-inputs.sml
 *
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 *
 * Support for processing static inputs to a program
 *)

structure CmdLineInputs : sig

  (* returns true if the argument has the form of a command-line input specification *)
    val isCmdLineInput : string -> bool

  (* initialize the table from the command-line arguments; returns false if there was
   * an error processing an argument.
   *)
    val initFromArgs : string list -> bool

  (* getInput(name, fromString, optDefault) gets the value of the named input.
   * if the value is not defined in the table and there is a default, then the
   * default is returned.  Otherwise it reads it from standard input
   *)
    val getInput : string * (string -> 'a option) * 'a option -> 'a option

  end = struct

    structure Tbl = AtomTable

    val inputs : string AtomTable.hash_table = AtomTable.mkTable(32, Fail "input table")

    fun notEq #"=" = false | notEq _ = true

    fun isCmdLineInput s = String.isPrefix "-D" s

    fun initFromArgs args = let
	  fun doArg arg = let
		val arg = Substring.full arg
		in
		  if Substring.isPrefix "-D" arg
		    then let
		      val arg = Substring.triml 2 arg
		      val (name, rest) = Substring.splitl notEq arg
		      in
			if (Substring.size name > 0) andalso (Substring.size rest > 1)
			  then (
			    AtomTable.insert inputs
			      (Atom.atom' name, Substring.string(Substring.triml 1 rest));
			    true)
			  else false
		      end
		    else true
		end
	  in
	    AtomTable.clear inputs;
	    List.all doArg args
	  end

    fun getInput (name, fromString : string -> 'a option, optDefault : 'a option) = let
	  fun cvt s = (case fromString s
		 of NONE => (
		      TextIO.output(TextIO.stdErr, concat[
			  "ERROR: value \"", String.toString s, "\" for input ", name,
			  " is invalid\n"
			]);
		      NONE)
		  | someVal => someVal
		(* end case *))
	  val name' = Atom.atom name
	  in
	    case AtomTable.find inputs name'
	     of NONE => (case optDefault
		   of NONE => (
			TextIO.output(TextIO.stdOut, concat[
			    "input value for ", name, ": "
			  ]);
			TextIO.flushOut TextIO.stdOut;
			case TextIO.inputLine TextIO.stdIn
			 of SOME ln => let
			      val ln = String.substring(ln, 0, size ln - 1)
			      in
				AtomTable.insert inputs (name', ln);  (* cache value *)
				cvt ln
			      end
			  | NONE => OS.Process.exit OS.Process.failure
			(* end case *))
		    | someVal => someVal
		  (* end case *))
	      | SOME v => fromString v
	    (* end case *)
	  end

  end

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