package ghcvm.runtime.apply;

import ghcvm.runtime.stg.StgClosure;
import ghcvm.runtime.stg.StgContext;
import ghcvm.runtime.stg.AbstractArgumentStack;
import ghcvm.runtime.stg.SimpleArgumentStack;

public abstract class StgFun extends StgClosure {

    public abstract int getArity();

    @Override
    public StgClosure getEvaluated() { return this; }

    @Override
    public void apply(StgContext context, Void v) {
        int arity = getArity();
        if (arity == 1) {
            enter(context);
        } else {
            StgPAP pap = new StgPAP(arity - 1, this);
            context.R(1, pap);
        }
    }

    @Override
    public void apply(StgContext context, StgClosure p) {
        int arity = getArity();
        if (arity == 1) {
            context.R(2, p);
            enter(context);
        } else {
            AbstractArgumentStack stack = new SimpleArgumentStack();
            stack.R(1, p);
            StgPAP pap = new StgPAP(arity - 1, this, stack);
            context.R(1, pap);
        }
    }
}

@@ -1027,11 +1027,496 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public void apply(StgContext context, StgClosure p1, StgClosure p2) %7B%0A        int arity = getArity();%0A        if (arity == 2) %7B%0A            context.R(2, p1);%0A            context.R(3, p2);%0A            enter(context);%0A        %7D else %7B%0A            AbstractArgumentStack stack = new SimpleArgumentStack();%0A            stack.R(1, p1);%0A            stack.R(2, p2);%0A            StgPAP pap = new StgPAP(arity - 2, this, stack);%0A            context.R(1, pap);%0A        %7D%0A    %7D
 %0A%7D%0A

Implement ap_pp for StgFun