Beispiel #1
0
  /*
  Usage: set(<value>, [index]) <var>
   */
  @Override
  public void invoke(Block b, String[] params, Variable receiver)
      throws Utils.InvalidCodeException {
    if (receiver == null)
      throw new Utils.InvalidCodeException("Attempted to set variable but no variable specified.");

    if (receiver.isArray())
      receiver.setValue(Utils.implode(params[0], b), Integer.parseInt(params[1]));
    else receiver.setValue(Utils.implode(params[0], b));
  }
Beispiel #2
0
  /*
  Usage: math(<expression>) <var>
   */
  @Override
  public void invoke(Block b, String[] params, Variable receiver)
      throws Utils.InvalidCodeException {
    if (engine == null) engine = new ScriptEngineManager().getEngineByName("JavaScript");

    if (receiver != null) {
      if (receiver.getType() != Variable.SystemVariableType.INTEGER
          && receiver.getType() != Variable.SystemVariableType.DECIMAL) {
        throw new Utils.InvalidCodeException("Attempted to assign math output to non-number.");
      }

      try {
        if (receiver.getType() == Variable.SystemVariableType.INTEGER) {
          receiver.setValue(Integer.parseInt(engine.eval(Utils.implode(params[0], b)).toString()));
        } else
          receiver.setValue(
              Double.parseDouble(engine.eval(Utils.implode(params[0], b)).toString()));
      } catch (Exception e) {
        throw new Utils.InvalidCodeException("Invalid math expression.");
      }
    }
  }