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

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/gen/fragments/mkfrags.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/gen/fragments/mkfrags.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4953, Tue Apr 4 03:20:05 2017 UTC revision 4954, Fri Apr 7 21:28:18 2017 UTC
# Line 15  Line 15 
15   *      <input file>    <fragment name>   *      <input file>    <fragment name>
16   *   *
17   * The resulting file (named fragments.sml) will contain a structure with the given   * The resulting file (named fragments.sml) will contain a structure with the given
18   * name; the structure consists of named fragments, each of which is a string literal   * name; the structure consists of named fragments.  Two kinds of input files are
19   * from the specified input file.   * supported.  If the input file has a ".in" suffix, then it is converted to an
20     * SML literal string in the output file.  If it has a ".json" suffix, then it
21     * is parsed as a JSON value (see the SML/NJ JSON library) and the resulting
22     * value in the output will be SML code that defines the corresponding JSON.value
23     * value.
24   *)   *)
25    
26  structure MkFrags : sig  structure MkFrags : sig
# Line 28  Line 32 
32    end = struct    end = struct
33    
34      structure F = Format      structure F = Format
35        structure J = JSON
36    
37    (* load the catalog from the file *)    (* load the catalog from the file *)
38      fun loadCatalog file = let      fun loadCatalog file = let
# Line 94  Line 99 
99                handle ex => (TextIO.closeIn inS; raise ex)                handle ex => (TextIO.closeIn inS; raise ex)
100            end            end
101    
102      fun doFile (outS, fragDir) (srcFile, smlVar) = let      fun doInFile (outS, fragDir) (srcFile, smlVar) = let
103            val text = load (OS.Path.concat (fragDir, srcFile))            val text = load (OS.Path.concat (fragDir, srcFile))
104            fun prf (fmt, items) = TextIO.output(outS, F.format fmt items)            fun prf (fmt, items) = TextIO.output(outS, F.format fmt items)
105            in            in
# Line 106  Line 111 
111              prf ("          \\\"\n", [])              prf ("          \\\"\n", [])
112            end            end
113    
114        fun doJSONFile (outS, fragDir) (srcFile, smlVar) = let
115              val value = JSONParser.parseFile (OS.Path.concat (fragDir, srcFile))
116              fun pr s = TextIO.output(outS, s)
117              fun prf (fmt, items) = pr (F.format fmt items)
118              fun prValue (indent, jv) = (case jv
119                     of J.OBJECT[] => pr "JSON.OBJECT[]"
120                      | J.OBJECT(fld::flds) => let
121                          fun prFld indent (fld, v) = (
122                                prf ("%s(\"%s\", ", [
123                                    F.LEFT(indent, F.STR ""), F.STR fld
124                                  ]);
125                                prValue (indent, v);
126                                pr ")")
127                          in
128                            prf ("JSON.OBJECT[\n", []);
129                            prFld (indent+4) fld;
130                            List.app (fn fld => (pr ",\n"; prFld (indent+4) fld)) flds;
131                            prf ("\n%s]", [F.LEFT(indent+2, F.STR "")])
132                          end
133                      | J.ARRAY[] => pr "JSON.ARRAY[]"
134                      | J.ARRAY(v::vs) => (
135                          prf ("JSON.ARRAY[\n", []);
136                          prValue' (indent+4, v);
137                          List.app (fn v => (pr ",\n"; prValue' (indent+4, v))) vs;
138                          prf ("\n%s]", [F.LEFT(indent+2, F.STR "")]))
139                      | J.NULL => pr "JSON.NULL"
140                      | J.BOOL b => prf ("JSON.BOOL %b", [F.BOOL b])
141                      | J.INT n => prf ("JSON.INT %s", [F.STR(IntInf.toString n)])
142                      | J.FLOAT f => prf ("JSON.REAL %s", [F.STR(Real.toString f)])
143                      | J.STRING s => prf ("JSON.STRING \"%s\"", [F.STR(String.toString s)])
144                    (* end case *))
145              and prValue' (indent, jv) = (
146                    prf ("%s", [F.LEFT(indent, F.STR "")]);
147                    prValue (indent, jv))
148              in
149                pr "\n";
150                prf ("    val %s = ", [F.STR smlVar]);
151                prValue (16, value);
152                pr "\n"
153              end
154    
155        fun doFile arg = let
156              val doInFile = doInFile arg
157              val doJSONFile = doJSONFile arg
158              in
159                fn (srcFile, smlVar) => (case OS.Path.ext srcFile
160                     of SOME "in" => doInFile (srcFile, smlVar)
161                      | SOME "json" => doJSONFile (srcFile, smlVar)
162                      | _ => raise Fail "unexpected/missing file suffix"
163                    (* end case *))
164              end
165    
166      fun mkFragments dir = let      fun mkFragments dir = let
167            val fragDir = OS.Path.concat(dir, "fragments")            val fragDir = OS.Path.concat(dir, "fragments")
168            val catalogFile = OS.Path.concat(fragDir, "CATALOG")            val catalogFile = OS.Path.concat(fragDir, "CATALOG")

Legend:
Removed from v.4953  
changed lines
  Added in v.4954

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