예제 #1
0
 @Override
 public String visitExpr(@NotNull DiceGameParser.ExprContext ctx) {
   if (ctx.OP != null) {
     return ctx.A.accept(this) + ctx.OP.getText() + ctx.B.accept(this);
   }
   if (ctx.INTEGER != null) {
     return ctx.INTEGER.getText();
   }
   if (ctx.VAR != null) {
     return ctx.VAR.accept(this);
   }
   if (ctx.E != null) {
     return '(' + ctx.E.accept(this) + ')';
   }
   if (ctx.SUM != null) {
     return "sum([dice.value for dice in " + ctx.DOs.accept(this) + "])";
   }
   if (ctx.COUNT != null) {
     if (ctx.DOs != null) {
       return "len(" + ctx.DOs.accept(this) + ")";
     }
     if (ctx.POs != null) {
       return "len(" + ctx.POs.accept(this) + ")";
     }
   }
   return "visitExpr";
 }
예제 #2
0
  @Override
  public String visitLoop(@NotNull DiceGameParser.LoopContext ctx) {

    if (ctx.FORLOOP != null) {
      StringBuilder forLoop = new StringBuilder();
      if (ctx.POs != null) {
        forLoop.append("for self." + ctx.VAR.getText() + " in " + ctx.POs.accept(this) + ":\n");
      }
      if (ctx.DOs != null) {
        forLoop.append("for self." + ctx.VAR.getText() + " in " + ctx.DOs.accept(this) + ":\n");
      }
      forLoop.append(indent(ctx.ACTION.accept(this)));
      return forLoop.toString();
    }
    if (ctx.NLOOP != null) {
      StringBuilder loop = new StringBuilder();
      loop.append("for i in range(" + ctx.VALUE.getText() + "):\n");
      loop.append(indent(ctx.ACTION.accept(this)));
      return loop.toString();
    }
    return "visitLoop";
  }