Runs Next: ObjectsUp: The SugarCubes Tool Box Previous: Behavior Environments

Runs

Run  instructions are the way to call behaviors. They extends UnaryInstruction and haveparameters, whose types are defined in the ParamTypes  interface:

public interface ParamTypes
{

  final public byte IN_PARAM      = 1; // input
  final public byte OUT_PARAM     = 2; // output
  final public byte INOUT_PARAM   = 3; // inputoutput
  final public byte LOCAL_PARAM   = 4; // local
}

At creation, an object of class Run copies its actual parameter vector in the array paramList.

The bindParams method checks that there is the same number of actual and formal parameters, and if it is the case, for each parameter, it changes the instruction body into an InputDecl, and OutputDecl, or an InOutDecl, according to the parameter type.

The activation method first tests that the number of runs created during current instant is not exceeded, then it gets the actual behavior and binds the parameters. Finally, it executes the body.

public class Run extends UnaryInstruction implements ParamTypes
{
  public String name;
  private Param  [] formalParams = null;
  private String [] actualParams = null;
  /** Number of formal parameters (initially 0). */
  private int len = 0;

  private boolean bindingDone = false;

  public Run(String name) { 
    this.name = name; body = new Nothing(); 
  }

  public Run(String name,Vector paramList) 
  { 
    this.name = name;
    body = new Nothing(); 
    if (paramList != null){
      len = paramList.size();
      actualParams = new String[len];
      paramList.copyInto(actualParams);
    }
  }

  public void reset(){ super.reset(); bindingDone = false; }

  final public String toString(){ ... }

  public boolean equals(Instruction inst) { ... }

  private void bindParams(Machine machine)
  {
    int formalLen = 
      formalParams == null ? 0 : formalParams.length;
    if (len != formalLen){
      System.out.println(
         "bad arg number (expected: "+formalLen+")"); 
      body = new Nothing();
      return;
    }
    for (int i = 0; i<len; i++){
      String internal = formalParams[i].name;
      int kind = formalParams[i].kind;

      if (kind == IN_PARAM){
        body = new InputDecl(internal,actualParams[i],body);
      }else if (kind == OUT_PARAM){
        body = new OutputDecl(internal,actualParams[i],body);
      }else if (kind == INOUT_PARAM){
        body = new InOutDecl(internal,actualParams[i],body);
      }
    }
  }

  final protected byte activation(Machine mach)
  {
    RsiMachine machine = (RsiMachine)mach;
    if (!bindingDone){
      if (machine.numberOfRuns++ >= machine.maxNumberOfRuns){
        System.out.println(
            "too much runs in the same instant (max is " + 
              machine.maxNumberOfRuns + ")");
        return STOP;
      }
      Behavior beh = machine.behavEnv.behavNamed(name);
      if (beh == null) return TERM;
      bindingDone = true;

      body = (Instruction)beh.body().clone();

      formalParams = beh.formalParams();
      bindParams(machine);
    }
    return body.activ(machine);
  }
}