(* tree-il-pp.sml * * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu) * All rights reserved. * * Printing for the TreeIL *) structure TreeILPP : sig val statement : TextIO.outstream * TreeIL.stm -> unit val block : TextIO.outstream * TreeIL.block -> unit val program : TextIO.outstream * TreeIL.program -> unit end = struct structure IL = TreeIL structure Op = IL.Op structure Var = IL.Var structure Ty = IL.Ty fun indent (outS, i) = TextIO.output(outS, StringCvt.padLeft #" " i "") fun incIndent (outS, i) = (outS, i+2) fun decIndent (outS, i) = (outS, Int.max(0, i-2)) fun pr ((outS, _), s) = TextIO.output(outS, s) fun prl (out, l) = pr(out, concat l) fun prln (out, l) = (indent out; prl(out, l)) fun descToString NONE = "" | descToString (SOME desc) = String.toString desc fun expToString e = let fun argsToS (lp, args, rp, l) = let fun argToS ([], l) = l | argToS ([e], l) = toS (e, l) | argToS (e::es, l) = toS(e, "," :: argToS(es, l)) in lp :: argToS(args, rp :: l) end and toS (IL.E_State x, l) = IL.stateVarToString x :: l | toS (IL.E_Var x, l) = (case (Var.kind x) of IL.VK_Local => Var.name x :: l | _ => "::" :: Var.name x :: l (* end case *)) | toS (IL.E_Lit lit, l) = Literal.toString lit :: l | toS (IL.E_Op(rator, args), l) = Op.toString rator :: argsToS ("(", args, ")", l) | toS (IL.E_Apply(f, args), l) = MathFuns.toString f :: argsToS ("(", args, ")", l) | toS (IL.E_Cons(ty, args), l) = "<" :: Ty.toString ty :: ">" :: argsToS ("{", args, "}", l) in String.concat (toS (e, [])) end fun argsToString (prefix, es) = String.concat[ prefix, "(", String.concatWith "," (List.map expToString es), ")" ] fun ppVarDecl out x = prln (out, [Ty.toString(Var.ty x), " ", Var.name x, ";\n"]) fun ppStrand out (IL.Strand{name, params, state, stateInit, methods}) = let val out' = incIndent out fun ppParams [] = () | ppParams [x] = prl(out, [Ty.toString (Var.ty x), " ", Var.name x]) | ppParams (x::r) = ( prl(out, [Ty.toString (Var.ty x), " ", Var.name x, ","]); ppParams r) fun ppSVarDecl (IL.SV{varying, output, name, ty, ...}) = let val v = if varying then "varying " else "" val out = if output then "output " else "" in prln (out', [v, out, Ty.toString ty, " ", name, ";\n"]) end in prln (out, ["strand ", Atom.toString name, " ("]); ppParams params; pr(out, ") {\n"); List.app ppSVarDecl state; prln (out', ["init () "]); ppBlock(out', stateInit); pr (out', "\n"); List.app (ppMethod out') methods; prln (out, ["}\n"]) end and ppMethod out (IL.Method{name, body}) = ( prln (out, [StrandUtil.nameToString name, " () "]); ppBlock (out, body); pr (out, "\n")) and ppBlock (out, IL.Block{locals, body}) = let val out' = incIndent out in pr (out, "{\n"); List.app (ppVarDecl out') locals; List.app (fn stm => ppStm(out', stm)) body; indent out; pr (out, "}") end and ppStm (out, stm) = (case stm of IL.S_Comment text => let val out = decIndent out in List.app (fn s => prln(out, ["// ", s, "\n"])) text end | IL.S_Assign([], e) => prln(out, [expToString e, ";\n"]) | IL.S_Assign([x], e) => prln(out, [Var.name x, " = ", expToString e, ";\n"]) | IL.S_Assign(x::xs, e) => ( prln(out, ["(", Var.name x]); List.app (fn x => prl(out, [",", Var.name x])) xs; prl (out, [") = ", expToString e, ";\n"])) | IL.S_IfThen(cond, blk) => ( prln (out, ["if (", expToString cond, ") "]); ppBlock (out, blk); pr (out, "\n")) | IL.S_IfThenElse(cond, blk1, blk2) => ( prln (out, ["if (", expToString cond, ") "]); ppBlock (out, blk1); pr (out, " else "); ppBlock (out, blk2); pr (out, "\n")) (* | IL.S_For(x, e1, e2, blk) => ( prln (out, [ "for (", Ty.toString(Var.ty x), " ", Var.name x, " = ", expToString e1, " ..", expToString e2, ") " ]); ppBlock (out, blk); pr (out, "\n")) *) | IL.S_New(strand, args) => prln (out, [argsToString("new "^Atom.toString strand, args), ";\n"]) | IL.S_Save([x], rhs) => prln (out, [IL.stateVarToString x, " = ", expToString rhs, ";\n"]) | IL.S_Save(x::xs, e) => ( prln(out, ["(", IL.stateVarToString x]); List.app (fn x => prl(out, [",", IL.stateVarToString x])) xs; prl (out, [") = ", expToString e, ";\n"])) (* DEPRECATED | IL.S_LoadImage(x, dim, exp) => prln (out, [ Var.name x, " = load<", Int.toString dim, "> (", expToString exp, ");\n" ]) *) | IL.S_Input(x, name, desc, NONE) => prln (out, [ Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"", String.toString name, "\",\"", descToString desc, "\");\n" ]) | IL.S_Input(x, name, desc, SOME dflt) => prln (out, [ Var.name x, " = input<", Ty.toString(Var.ty x), "> (\"", String.toString name, "\",\"", descToString desc, "\",", expToString dflt, ");\n" ]) | IL.S_InputNrrd(x, name, desc, NONE) => prln (out, [ Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"", String.toString name, "\",\"", descToString desc, "\");\n" ]) | IL.S_InputNrrd(x, name, desc, SOME dflt) => prln (out, [ Var.name x, " = input-nrrd<", Ty.toString(Var.ty x), "> (\"", String.toString name, "\",\"", descToString desc, "\",\"", String.toString dflt, "\");\n" ]) | IL.S_Exit es => prln (out, [argsToString("exit", es), ";\n"]) (* return functions for methods *) | IL.S_Active => prln (out, ["active;\n"]) | IL.S_Stabilize => prln (out, ["stabilize;\n"]) | IL.S_Die => prln (out, ["die;\n"]) (* end case *)) fun statement (outS, stm) = ppStm((outS, 0), stm) fun block (outS, blk) = (ppBlock ((outS, 0), blk); pr ((outS, 0), "\n")) fun ppInitially (out, {isArray, iterPrefix, iters, createPrefix, strand, args}) = let fun ppBlock (out, IL.Block{locals, body}, inside) = let val out' = incIndent out in pr (out, "{\n"); List.app (ppVarDecl out') locals; List.app (fn stm => ppStm(out', stm)) body; inside out'; indent out; pr (out, "}") end fun ppCreate out = let fun ppNew out = prln(out, [argsToString("new "^Atom.toString strand, args), ";\n"]) in indent out; ppBlock (out, createPrefix, ppNew); pr(out, "\n") end fun ppIters out = let fun ppIter (out, []) = ppCreate out | ppIter (out, (i, lo, hi)::iters) = ( prln(out, ["for ", Var.name i, " = ", expToString lo, " .. ", expToString hi, "\n"]); ppIter (incIndent out, iters)) in ppIter (out, iters) end in indent out; pr(out, if isArray then "ARRAY " else "COLLECTION "); ppBlock (out, iterPrefix, ppIters); pr (out, "\n") end fun program (outS, IL.Program{props, globals, inputInit, globalInit, strands, initially}) = let val out = (outS, 0) val out' = incIndent out in pr(out, "//***** PROPERTIES *****\n"); case props of [] => prln(out', ["none\n"]) | _ => prln(out', [String.concatWith " " (List.map StrandUtil.propToString props), "\n"]) (* end case *); prln(out, ["//***** GLOBALS *****\n"]); List.app (ppVarDecl out') globals; prln(out, ["//***** INPUT INIT *****\n"]); indent out'; ppBlock (out', inputInit); pr (out, "\n"); prln(out, ["//***** GLOBAL INIT *****\n"]); indent out'; ppBlock (out', globalInit); pr (out, "\n"); prln(out, ["//***** STRANDS *****\n"]); List.app (ppStrand out) strands; prln(out, ["//***** INITIALLY *****\n"]); ppInitially (out', initially) end end
Click to toggle
does not end with </html> tag
does not end with </body> tag
The output has ended thus: nds; prln(out, ["//***** INITIALLY *****\n"]); ppInitially (out', initially) end end