Пример #1
0
  /**
   * Function to evaluate a script/program
   *
   * @param script
   * @return
   */
  public String interpret(final String script, final Client client) {
    // System.out.println("PGM: <" + script + ">");
    // System.out.println("pArg: " + script);

    System.out.println("Interpret: " + script);

    if (script.startsWith("{") && script.endsWith("}")) {
      if (script.indexOf(":") != -1) {
        String work = script.substring(1, script.length() - 1);
        // String work = script.replace("{", "").replace("}", "");

        System.out.println("work: " + work);

        String[] temp = work.split(":", 2);

        String functionName = temp[0];
        List<String> params = null;

        System.out.println("Function: " + functionName);

        if (temp.length > 1) {
          params = Utils.mkList(temp[1].split(","));

          fixParams(params); // sort of fixes the params

          System.out.println("Params: " + params);

          int index = 0;

          // whenever the function called isn't 'if' or 'with', we want to evaluate all parameters
          // as we get them
          if (!functionName.equals("if") && !functionName.equals("with")) {
            for (String param : params) {
              if (param.startsWith("{") && param.endsWith("}")) {
                params.set(index, interpret(param, client));
              }
              index++;
            }
          }

          System.out.println("Evaluate: <" + functionName + "> with " + params);
          // return evaluate(functionName, params.toArray(new String[params.size()]), client);
          String result = evaluate(functionName, params.toArray(new String[params.size()]), client);
          System.out.println("Result: " + result);
          return result;
        } else {
          return "Incomplete function statement, no parameters!";
        }
      } else {
        return evaluate(script, new String[0], client);
      }
    }

    return "Invalid Script!";
  }