package jazz.circuit.esterel;

//////////////////////////////////////////////////////////////////////////////
//
//               Pretty-printing of Pure Esterel programs
//
//////////////////////////////////////////////////////////////////////////////

import jazz.util.List;

// Indentation
class I {
  static dynamic newline: String = "\n";
  static indent(): String;
}

I.indent() = format("%s  ", newline);

// Module
toString@Module() = format("module %s:%s%s%s  %a%s.", name, I.newline,
                           i, o, s, I.newline)
{
  dynamic (List.lparen = "") {
    dynamic (List.rparen = "") {
      dynamic (List.separator = ", ") {
        i = inputs.length() == 0 ? "" : format("  input %a;\n", inputs);
        o = outputs.length() == 0 ? "" : format("  output %a;\n", outputs);
      }
    }
  }
  dynamic (I.newline = I.indent()) {
    s = stmt.toString();
  }
}


// Statement
toString@NothingStmt() = "nothing";
toString@HaltStmt() = "halt";
toString@EmitStmt() = format("emit %s", sig);
toString@SequenceStmt() = format("%a;%s%a", stmt1, I.newline, stmt2);
toString@LoopStmt() = format("loop%s  %s%send", I.newline, s, I.newline)
{
  dynamic (I.newline = I.indent()) {
    s = stmt.toString();
  }
}

toString@PresentStmt() = format("present %s then%s  %a%selse%s  %a%send",
                                sig, I.newline, s1, I.newline,
                                I.newline, s2, I.newline)
{
  dynamic (I.newline = I.indent()) {
    s1 = stmt1.toString();
    s2 = stmt2.toString();
  }
}

toString@WatchStmt() = format("do%s  %a%swatching %s",
                              I.newline, s, I.newline, sig)
{
  dynamic (I.newline = I.indent()) {
    s = stmt.toString();
  }
}

toString@ParallelStmt() = format("[%s  %a%s||%s  %a%s]",
                                 I.newline, s1, I.newline,
                                 I.newline, s2, I.newline)
{
  dynamic (I.newline = I.indent()) {
    s1 = stmt1.toString();
    s2 = stmt2.toString();
  }
}

toString@TrapStmt() = format("trap %s in%s  %a%send", sig,
                             I.newline, s, I.newline)
{
  dynamic (I.newline = I.indent()) {
    s = stmt.toString();
  }
}

toString@ExitStmt() = format("exit %s", sig);
toString@SignalStmt() = format("signal %s in%s  %a%send", sig,
                               I.newline, s, I.newline)
{
  dynamic (I.newline = I.indent()) {
    s = stmt.toString();
  }
}