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

SCM Repository

[diderot] View of /trunk/src/compiler/simplify/simple-pp.sml
ViewVC logotype

View of /trunk/src/compiler/simplify/simple-pp.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 229 - (download) (annotate)
Wed Aug 4 15:46:08 2010 UTC (8 years, 11 months ago) by jhr
File size: 6305 byte(s)
  Added type field to E_Input in Simple AST.
(* simple-pp.sml
 *
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
 * All rights reserved.
 *
 * Pretty printing for the Simple-AST representation.
 *)

structure SimplePP : sig

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

  end = struct

    structure PP = TextIOPP
    structure TU = TypeUtil
    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 Types.TYPE tv => string(TU.toString(TU.resolve tv))
		  | Types.DIFF dv => string("#"^TU.diffToString(TU.resolveDiff dv))
		  | Types.SHAPE sv => string("$"^TU.shapeToString(TU.resolveShape sv))
		  | Types.DIM dv => string("%"^TU.dimToString(TU.resolveDim dv))
		(* end case *))
	  in
	    ppList ppTyArg ("<", ";", ">") (ppStrm, mvs)
	  end

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

    fun ppVarDecl ppStrm = let
	  fun sp () = PP.space ppStrm 1
	  val string = PP.string ppStrm
	  in
	    fn x => (
	      PP.openHBox ppStrm;
		case Var.kindOf x
		 of S.InputVar => (string "input"; sp())
		  | S.ActorOutputVar => (string "output"; sp())
		  | _ => ()
		(* end case *);
		string(TU.toString(#2(Var.typeOf x))); sp(); string(Var.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 = string(Var.nameOf x)
	  fun pp e = (case e
		 of S.E_Var x => string(Var.uniqueNameOf 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_Apply(f, mvs, args, _) => (
		      var f; ppTyArgs (ppStrm, mvs); sp(); ppArgs (ppStrm, args))
		  | S.E_Cons es => (
		      ppList ppVar ("[", ",", "]") (ppStrm, es))
		  | S.E_Input(ty, argName, NONE) =>
		      string(concat["input(\"", argName, "\")"])
		  | S.E_Input(ty, argName, SOME default) => (
		      string "inputWithDefault"; string "(";
		      string (concat["\"", argName, "\""]); string ","; sp();
		      var default; string ")")
		  | S.E_Field fld => (
		      string "field"; sp(); string(FieldDef.toString fld))
		  | S.E_LoadImage info => (
		      string "load"; sp(); string(ImageInfo.toString info))
		(* end case *))
	  in
	    pp e
	  end

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

    fun 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 = string(Var.uniqueNameOf x)
	  fun ppStmt stmt = (
		nl();
		case stmt
		 of 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;
			nl();
			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;
			nl();
			ppStmt s1;
		      PP.closeBox ppStrm;
		      nl();
		      PP.openVBox ppStrm indent;
			string "else"; nl();
			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;
			string "else"; sp(); ppBlock (ppStrm, [], blk2);
		      PP.closeBox ppStrm)
		  | S.S_New(actor, args) => (
		      PP.openHBox ppStrm;
			string "new"; sp(); string(Atom.toString actor); sp();
			ppArgs (ppStrm, args); string ";";
		      PP.closeBox ppStrm)
		  | S.S_Die => string "die;"
		  | S.S_Stabilize => string "stabilize;"
		(* 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 ppActor ppStrm (S.Actor{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 = string(Var.nameOf x)
	  fun ppMethod (S.Method(name, body)) = (
		nl(); string(Atom.toString name); nl(); ppBlock (ppStrm, [], body))
	  in
	    PP.openHBox ppStrm;
	      string "actor"; sp(); string(Atom.toString name); sp();
	      ppList (fn (_, x) => (string(TU.toString(#2(Var.typeOf x))); sp(); var x))
		("(", ",", ")") (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 output (outS, S.Program{globals, staticInit, globalInit, actors}) = let
	  val ppStrm = PP.openOut {dst = outS, wid = 120}
	  fun nl () = PP.newline ppStrm
	  in
	    PP.openVBox ppStrm (PP.Abs 0);
	      PP.string ppStrm "/* Simplified Program start */"; nl();
	      case staticInit
	       of S.Block[] => ppBlock (ppStrm, globals, globalInit)
		| _ => (
		    ppBlock (ppStrm, globals, staticInit); nl();
		    ppBlock (ppStrm, [], globalInit))
	      (* end case *);
	      nl();
	      List.app (ppActor ppStrm) actors;
	      PP.string ppStrm "/* Program end */"; PP.newline ppStrm;
	    PP.closeBox ppStrm;
	    PP.closeStream ppStrm
	  end

  end

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