Пример #1
0
  public Expression substitute(String var, Expression replacement) {
    Vector<Expression> new_operands = new Vector<Expression>();
    for (Expression o : operands) {
      new_operands.add(o.substitute(var, replacement));
    }

    return new Application(operator.substitute(var, replacement), new_operands);
  }
Пример #2
0
  public Expression oneStep() {
    if (operator.reducible()) {
      return new Application(operator.oneStep(), operands);
    } else if (isOperandsReducible()) {
      return new Application(operator, reduceOperandsByOneStep());
    } else if (allOperandsAreValues()) {
      if (operator instanceof RecFun) {
        RecFun operator = ((RecFun) this.operator);
        Expression body = operator.body;

        // Substitute occurrences of function name with the function
        body = body.substitute(operator.funVar, operator);

        // When we are here, we are sure that the form is
        // (fun ... end Op1 ... OpN)
        Vector<String> formals = operator.formals;

        for (int i = 0; i < formals.size(); i++) {
          body = body.substitute(formals.get(i), operands.get(i));
        }

        return body;
      } else if (operator instanceof Fun) {
        Fun operator = ((Fun) this.operator);
        Expression body = operator.body;

        // When we are here, we are sure that the form is
        // (fun ... end Op1 ... OpN)
        Vector<String> formals = operator.formals;

        for (int i = 0; i < formals.size(); i++) {
          body = body.substitute(formals.get(i), operands.get(i));
        }

        return body;
      } else {
        return this;
      }
    }
    return this;
  }