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

SCM Repository

[diderot] View of /branches/vis15/src/compiler/simple/simple-pp.sml
ViewVC logotype

View of /branches/vis15/src/compiler/simple/simple-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3431 - (download) (annotate)
Sat Nov 14 14:03:58 2015 UTC (3 years, 11 months ago) by jhr
File size: 13276 byte(s)
  Working on merge; some bug fixing and code cleanup
(* simple-pp.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *
 * Pretty printing for the Simple-AST representation.
 *)

structure SimplePP : sig

    val output : TextIO.outstream * string * Simple.program -> unit

    val outputFunc : TextIO.outstream * string * Simple.func -> unit

  end = struct

    structure PP = TextIOPP
    structure Ty = SimpleTypes
    structure S = Simple

    val indent = PP.Abs 2

    fun ppList ppFn (left, sep, right) (ppStrm, list) = let
          fun sp () = PP.space ppStrm 1
          val string = PP.string ppStrm
          fun pp [] = string right
            | pp [x] = (ppFn(ppStrm, x); string right)
            | pp (x::xs) = (ppFn(ppStrm, x); string sep; sp(); pp xs)
          in
            string left; pp list
          end

  (* print type arguments; we use "#" to denote differentiation arguments, "$" to denote
   * shape arguments, and "%" to denote dimension arguments.
   *)
    fun ppTyArgs (ppStrm, mvs) = let
          val string = PP.string ppStrm
          fun ppTyArg (_, mv) = (case mv
                 of Ty.TY ty => string(Ty.toString ty)
                  | Ty.DIFF k => string("#"^Int.toString k)
                  | Ty.SHAPE shp => string(concat[
                        "$[", String.concatWith "," (List.map Int.toString shp), "]"
                      ])
                  | Ty.DIM d => string("%"^Int.toString d)
                (* end case *))
          in
            ppList ppTyArg ("<", ";", ">") (ppStrm, mvs)
          end

    fun ppVar (ppStrm, x) = PP.string ppStrm (SimpleVar.uniqueNameOf x)

    fun ppVarDecl ppStrm = let
          fun sp () = PP.space ppStrm 1
          val string = PP.string ppStrm
          in
            fn x => (
              PP.openHBox ppStrm;
                case SimpleVar.kindOf x
                 of S.InputVar => (string "input"; sp())
                  | S.StrandOutputVar => (string "output"; sp())
                  | _ => ()
                (* end case *);
                string(Ty.toString(SimpleVar.typeOf x)); sp(); string(SimpleVar.uniqueNameOf x); string ";";
              PP.closeBox ppStrm)
          end

    fun ppExp (ppStrm, e) = let
          fun sp () = PP.space ppStrm 1
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          fun ppIndex (ppStrm, NONE) = PP.string ppStrm ":"
            | ppIndex (ppStrm, SOME i) = var i
          fun pp e = (case e
                 of S.E_Var x => var x
                  | S.E_Lit lit => string (Literal.toString lit)
                  | S.E_Tuple es => ppArgs (ppStrm, es)
                  | S.E_Apply(f, args, _) => (var f; sp(); ppArgs (ppStrm, args))
                  | S.E_Prim(f, [], args, _) => (string(Var.nameOf f); sp(); ppArgs (ppStrm, args))
                  | S.E_Prim(f, mvs, args, _) => (
                      string(Var.nameOf f); ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args))
                  | S.E_Cons es => (
                      ppList ppVar ("[", ",", "]") (ppStrm, es))
                  | S.E_Seq(es, _) => (
                      ppList ppVar ("{", ",", "}") (ppStrm, es))
                  | S.E_Slice(x, indices, _) => (
                      var x;
                      ppList ppIndex ("[", ",", "]") (ppStrm, indices))
                  | S.E_Coerce{srcTy, dstTy, x} => (
                      string "("; string(Ty.toString dstTy); string ")"; var x)
                  | S.E_LoadSeq(ty, nrrd) => (
                      string "loadSeq<"; string(Ty.toString ty); string ">(\"";
                      string(String.toString nrrd); string "\")")
                  | S.E_LoadImage(ty, nrrd, _) => (
                      string "loadImage<"; string(Ty.toString ty); string ">(\"";
                      string(String.toString nrrd); string "\")")
                (* end case *))
          in
            pp e
          end

    and ppArgs (ppStrm, args) = ppList ppVar ("(", ",", ")") (ppStrm, args)

    fun ppBlock (ppStrm, [], S.Block[]) = PP.string ppStrm "{ }"
      | ppBlock (ppStrm, vars, S.Block stms) = let
          fun sp () = PP.space ppStrm 1
          fun nl () = PP.newline ppStrm
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          fun ppStmt stmt = (
                nl();
                case stmt
                 of S.S_Var x => (
                      PP.openHBox ppStrm;
                        string(Ty.toString(SimpleVar.typeOf x)); sp(); var x; string ";";
                      PP.closeBox ppStrm)
                  | S.S_Assign(x, e) => (
                      PP.openHBox ppStrm;
                        var x; sp(); string "="; sp(); ppExp(ppStrm, e); string ";";
                      PP.closeBox ppStrm)
                  | S.S_IfThenElse(x, S.Block[s1], S.Block[]) => (
                      PP.openVBox ppStrm indent;
                        PP.openHBox ppStrm;
                          string "if"; sp(); ppVar(ppStrm, x);
                        PP.closeBox ppStrm;
                        ppStmt s1;
                      PP.closeBox ppStrm)
                  | S.S_IfThenElse(x, blk, S.Block[]) => (
                      PP.openHBox ppStrm;
                        string "if"; sp(); ppVar(ppStrm, x);
                        sp(); ppBlock (ppStrm, [], blk);
                      PP.closeBox ppStrm)
                  | S.S_IfThenElse(x, S.Block[s1], S.Block[s2]) => (
                      PP.openVBox ppStrm indent;
                        PP.openHBox ppStrm;
                          string "if"; sp(); ppVar(ppStrm, x);
                        PP.closeBox ppStrm;
                        ppStmt s1;
                      PP.closeBox ppStrm;
                      nl();
                      PP.openVBox ppStrm indent;
                        string "else";
                        ppStmt s2;
                      PP.closeBox ppStrm)
                  | S.S_IfThenElse(x, blk1, blk2) => (
                      PP.openHBox ppStrm;
                        string "if"; sp(); ppVar(ppStrm, x);
                        sp(); ppBlock (ppStrm, [], blk1);
                      PP.closeBox ppStrm;
                      PP.openHBox ppStrm;
                        sp(); string "else"; sp(); ppBlock (ppStrm, [], blk2);
                      PP.closeBox ppStrm)
                  | S.S_New(strand, args) => (
                      PP.openHBox ppStrm;
                        string "new"; sp(); string(Atom.toString strand); sp();
                        ppArgs (ppStrm, args); string ";";
                      PP.closeBox ppStrm)
                  | S.S_Continue => string "continue;"
                  | S.S_Die => string "die;"
                  | S.S_Stabilize => string "stabilize;"
                  | S.S_Return x => (
                      PP.openHBox ppStrm;
                        string "return"; sp(); ppVar(ppStrm, x); string ";";
                      PP.closeBox ppStrm)
                  | S.S_Print args => (
                      PP.openHBox ppStrm;
                        string "print"; sp(); ppArgs (ppStrm, args); string ";";
                      PP.closeBox ppStrm)
                (* end case *))
          in
            PP.openVBox ppStrm (PP.Abs 0);
              string "{";
              PP.openVBox ppStrm indent;
                List.app (fn vdcl => (nl(); ppVarDecl ppStrm vdcl)) vars;
                List.app ppStmt stms;
              PP.closeBox ppStrm;
              nl(); string "}";
            PP.closeBox ppStrm
          end

    fun ppParams (ppStrm, params) = let
          fun sp () = PP.space ppStrm 1
          val string = PP.string ppStrm
          in
            ppList
              (fn (_, x) => (string(Ty.toString(SimpleVar.typeOf x)); sp(); ppVar (ppStrm, x)))
              ("(", ",", ")")
              (ppStrm, params)
          end

    fun ppFunc ppStrm (S.Func{f, params, body}) = let
          fun sp () = PP.space ppStrm 1
          fun nl () = PP.newline ppStrm
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          in
            PP.openHBox ppStrm;
              string "function"; sp();
              string(Ty.toString(Ty.rngOf(SimpleVar.typeOf f)));
              sp(); var f; sp(); ppParams (ppStrm, params);
            PP.closeBox ppStrm;
            nl();
            ppBlock (ppStrm, [], body);
            nl()
          end

    fun ppInit (ppStrm, S.Initially{isArray, rangeInit, iters, create}) = let
          fun sp () = PP.space ppStrm 1
          fun nl () = PP.newline ppStrm
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          val label = if isArray then "Array" else "Collection"
          fun ppIters [] = let
                val S.C_Create{argInit, name, args} = create
                in
                  ppBlock (ppStrm, [], argInit); nl();
                  PP.openHBox ppStrm;
                    string "new"; sp(); string(Atom.toString name);
                    ppArgs (ppStrm, args); string ";";
                  PP.closeBox ppStrm
                end
            | ppIters ({param, lo, hi} :: iters) = (
                PP.openVBox ppStrm indent;
                  PP.openHBox ppStrm;
                    string "for"; sp();
                    string(Ty.toString(SimpleVar.typeOf param)); sp(); var param;
                    sp(); string "="; sp(); var lo; sp(); string ".."; sp(); var hi;
                  PP.closeBox ppStrm;
                  nl();
                  ppIters iters;
                PP.closeBox ppStrm)
          in
            PP.openVBox ppStrm indent;
              string label; nl();
              ppBlock (ppStrm, [], rangeInit); nl();
              ppIters iters;
            PP.closeBox ppStrm;
            nl()
          end

    fun ppStrand ppStrm (S.Strand{name, params, state, stateInit, methods}) = let
          fun sp () = PP.space ppStrm 1
          fun nl () = PP.newline ppStrm
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          fun ppMethod (S.Method(name, body)) = (
                nl(); string(StrandUtil.nameToString name); nl(); ppBlock (ppStrm, [], body))
          in
            PP.openHBox ppStrm;
              string "strand"; sp(); string(Atom.toString name); sp();
              ppParams (ppStrm, params);
            PP.closeBox ppStrm;
            nl();
            PP.openVBox ppStrm indent;
              string "{";
              ppBlock (ppStrm, state, stateInit);
              List.app ppMethod methods;
            PP.closeBox ppStrm;
            nl();
            string "}";  nl()
          end

    fun ppInput ppStrm = let
          fun sp () = PP.space ppStrm 1
          fun nl () = PP.newline ppStrm
          val string = PP.string ppStrm
          fun var x = ppVar (ppStrm, x)
          fun pp (x, Inputs.INP{name, desc, init, ...}) = (
                PP.openHBox ppStrm;
                  string "input"; sp();
                  string(Ty.toString(SimpleVar.typeOf x)); sp();
                  ppVar (ppStrm, x); string ": ";
                  string name;
                  case desc
                   of SOME desc => string(concat["(\"", desc, "\")"])
                    | NONE => ()
                  (* end case *);
                  case init
                   of SOME init => (
                        sp(); string "="; sp();
                        string(Inputs.initToString init); string ";")
                    | NONE => string ";"
                  (* end case *);
                  nl();
                PP.closeBox ppStrm)
          in
            pp
          end

    fun output (outS, msg, prog) = let
          val S.Program{props, inputDefaults, inputs, globals, globalInit, funcs, strands, init} = prog
          val ppStrm = PP.openOut {dst = outS, wid = 120}
          fun nl () = PP.newline ppStrm
          in
            PP.openVBox ppStrm (PP.Abs 0);
              PP.string ppStrm (concat[
                  "/* Simplified Program (after ", msg, ") start */"
                ]); nl();
              PP.openHBox ppStrm;
                PP.string ppStrm "properties:";
                PP.space ppStrm 1;
                PP.string ppStrm (Properties.propsToString props);
                PP.newline ppStrm;
              PP.closeBox ppStrm;
              ppBlock (ppStrm, [], inputDefaults);
              nl();
              List.app (ppInput ppStrm) inputs;
              List.app (ppFunc ppStrm) funcs;
              ppBlock (ppStrm, globals, globalInit);
              nl();
              ppInit (ppStrm, init);
              List.app (ppStrand ppStrm) strands;
              PP.string ppStrm "/* Program end */"; PP.newline ppStrm;
            PP.closeBox ppStrm;
            PP.closeStream ppStrm
          end

    fun outputFunc (outS, msg, func) = let
          val ppStrm = PP.openOut {dst = outS, wid = 120}
          in
            PP.openVBox ppStrm (PP.Abs 0);
              PP.string ppStrm (concat[
                  "/* ", msg, " */"
                ]); PP.newline ppStrm;
              ppFunc ppStrm func;
            PP.closeBox ppStrm;
            PP.closeStream ppStrm
          end

  end

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