Пример #1
0
  @Override
  public String visitGameend(@NotNull DiceGameParser.GameendContext ctx) {
    if (ctx.MULTI != null) {
      return "print('Die Spieler '+','.join([self.name for self in game.players if "
          + ctx.COND.accept(this)
          + "])+' haben gewonnen!')";
    }

    if (ctx.SINGLE != null) {
      return "print([self.name for self in game.players if "
          + ctx.COND.accept(this)
          + "][0]+' hat gewonnen!')";
    }

    if (ctx.MOST != null) {
      return "print(str([player.name for player in game.players if player."
          + ctx.VAR.getText()
          + " == (max([self."
          + ctx.VAR.getText()
          + " for self in game.players]))][0]) +' hat gewonnen!')";
    }

    if (ctx.LEAST != null) {
      return "print(str([player.name for player in game.players if player."
          + ctx.VAR.getText()
          + " == (min([self."
          + ctx.VAR.getText()
          + " for self in game.players]))][0]) +' hat gewonnen!')";
    }

    return "visitGameend";
  }
Пример #2
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";
 }
Пример #3
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";
  }
Пример #4
0
  @Override
  public String visitVariable(@NotNull DiceGameParser.VariableContext ctx) {
    if (ctx.VAR != null) {
      return "self." + ctx.VAR.getText();
    }
    if (ctx.DO != null) {
      return ctx.DO.accept(this) + ".value";
    }
    if (ctx.PO != null) {
      return ctx.PO.accept(this);
    }
    if (ctx.INST != null) {
      return ctx.INST.accept(this) + "." + ctx.IVAR.getText();
    }

    return "visitVariable";
  }