// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/Executable.java
// public interface Executable extends Node {
// 
//     /**
//      * Get function arity.
//      * @return    function arity
//      */
//     int getArity();
//     
//     /**
//      * Get module defining the function.
//      * @return   module defining the function 
//      */
//     Module getModule();
//     
//     /**
//      * Get function name.
//      * @return    function name
//      */
//     String getFunctionName();
// 
//     /**
//      * Whether this function is a continuation doing pattern matching over
//      * a type constructor assumed to be on top of the stack. This allows us 
//      * to implement the &quot;return in registers&quot; convention.
//      *  
//      * @return    
//      *   <code>true</code> if this function is a tycon pattern 
//      *   matching continuation, <code>false</code> otherwise
//      */
//     boolean isMatcher();
//     
//     /**
//      * Sometimes, functions can be entered in a faster way.
//      * A typical case is when we statically know that the
//      * number of arguments for a function matches its arity.
//      *  
//      * @param ctx    {@link ExecutionContext} where the function will execute
//      */
//     void fastEnter(ExecutionContext ctx);
// }
// 
// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/NApp.java
// public class NApp implements Node {
// 
//     /*
//      * Nodes in the applicacion. This follows the same convention as
//      * the runtime stack, that is: top of the stack is at the right.
//      * 
//      * So, MkApp(n), with stack [...,xn,xn-1,...,x1] gives: 
//      *    NApp[xn,xn-1,...,x1]
//      *    
//      * Array index increases to the right, which means:
//      *    this.nodes[0] = xn, this.nodes[n-1] = x1. 
//      *    
//      *  In general:
//      *     this.nodes[i] = x{n-i}, i in [0,n-1]
//      */
//     private Node[] nodes;
//     
//     /**
//      * Default constructor.
//      */
//     public NApp() {
//         this(new Node[0]);
//     }
//     
//     /**
//      * Construct a new instance with the given nodes.
//      * @param nodes    nodes in the NApp
//      */
//     public NApp(Node[] nodes) {
//         this.nodes = nodes;
//     }
// 
//     /**
//      * Get child nodes for this napp.
//      * @return    child nodes
//      */
//     public Node[] getNodes() {
//         return this.nodes;
//     }
// 
//     /**
//      * Set nodes packed by this application.
//      * @param nodes    nodes to pack
//      */
//     public void pack(Node[] nodes) {
//         this.nodes = nodes;
//     }
//     
//     /* (non-Javadoc)
//      * @see org.bluejelly.nodes.Node#enter(bluejelly.runtime.ExecutionContext)
//      */
//     @Override
//     public void enter(ExecutionContext ctx) {
//         --ctx.sp;
//         ctx.stackCheck(this.nodes.length);
//         for (Node n: this.nodes) {
//             ctx.s[++ctx.sp] = n;
//         }
//     }
// 
//     @Override
//     public String toString() {
//         return new StringBuffer()
//             .append("NApp:")
//             .append(Arrays.toString(this.nodes))
//             .toString();
//     }
// }
// 
// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/Node.java
// public interface Node {
//     
//     /**
//      * Enter this node, evaluating it to normal form.
//      * @param ctx    {@link ExecutionContext} where this node is executing
//      */
//     void enter(ExecutionContext ctx);
//     
// }


import bluejelly.runtime.nodes.Executable;
import bluejelly.runtime.nodes.NApp;
import bluejelly.runtime.nodes.Node;
/*
 * The Bluejelly project, Copyright 2012.
 *
 * This source code is distributed under the terms of 
 * the BSD license, see the LICENSE file for details.
 */

package bluejelly.runtime;


/**
 * Continuation marker. Whenever found on the stack, it will jump
 * to the function associated to this continuation.
 * 
 * @author ppedemon
 */
public class ContMarker extends Marker {

    // Continuation
    private Executable cont;
    
    /**
     * Construct a continuation marker for the given {@link ExecutionContext}
     * @param ctx     associated context
     * @param cont    continuation code {@link Node}
     */
    public ContMarker(ExecutionContext ctx, Executable cont) {
        super(ctx);
        this.cont = cont;
    }

    /**
     * Is the code for this continuation a tycon matcher?
     * @return    whether the code for this continuation is a tycon matcher
     */
    public boolean isMatcher() {
        return cont.isMatcher();
    }
    
    /* 
     * So, an evaluation routine finished with a partial application. 
     * We must pack the whole stack frame on a NApp node, and schedule 
     * a jump to the continuation.
     * 
     * (non-Javadoc)
     * @see bluejelly.runtime.Marker#signalPap()
     */
    @Override
    protected void signalPap() {
        int n = this.ctx.sp - this.ctx.bp;
        Node[] nodes = this.ctx.collect(n);
// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/Executable.java
// public interface Executable extends Node {
// 
//     /**
//      * Get function arity.
//      * @return    function arity
//      */
//     int getArity();
//     
//     /**
//      * Get module defining the function.
//      * @return   module defining the function 
//      */
//     Module getModule();
//     
//     /**
//      * Get function name.
//      * @return    function name
//      */
//     String getFunctionName();
// 
//     /**
//      * Whether this function is a continuation doing pattern matching over
//      * a type constructor assumed to be on top of the stack. This allows us 
//      * to implement the &quot;return in registers&quot; convention.
//      *  
//      * @return    
//      *   <code>true</code> if this function is a tycon pattern 
//      *   matching continuation, <code>false</code> otherwise
//      */
//     boolean isMatcher();
//     
//     /**
//      * Sometimes, functions can be entered in a faster way.
//      * A typical case is when we statically know that the
//      * number of arguments for a function matches its arity.
//      *  
//      * @param ctx    {@link ExecutionContext} where the function will execute
//      */
//     void fastEnter(ExecutionContext ctx);
// }
// 
// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/NApp.java
// public class NApp implements Node {
// 
//     /*
//      * Nodes in the applicacion. This follows the same convention as
//      * the runtime stack, that is: top of the stack is at the right.
//      * 
//      * So, MkApp(n), with stack [...,xn,xn-1,...,x1] gives: 
//      *    NApp[xn,xn-1,...,x1]
//      *    
//      * Array index increases to the right, which means:
//      *    this.nodes[0] = xn, this.nodes[n-1] = x1. 
//      *    
//      *  In general:
//      *     this.nodes[i] = x{n-i}, i in [0,n-1]
//      */
//     private Node[] nodes;
//     
//     /**
//      * Default constructor.
//      */
//     public NApp() {
//         this(new Node[0]);
//     }
//     
//     /**
//      * Construct a new instance with the given nodes.
//      * @param nodes    nodes in the NApp
//      */
//     public NApp(Node[] nodes) {
//         this.nodes = nodes;
//     }
// 
//     /**
//      * Get child nodes for this napp.
//      * @return    child nodes
//      */
//     public Node[] getNodes() {
//         return this.nodes;
//     }
// 
//     /**
//      * Set nodes packed by this application.
//      * @param nodes    nodes to pack
//      */
//     public void pack(Node[] nodes) {
//         this.nodes = nodes;
//     }
//     
//     /* (non-Javadoc)
//      * @see org.bluejelly.nodes.Node#enter(bluejelly.runtime.ExecutionContext)
//      */
//     @Override
//     public void enter(ExecutionContext ctx) {
//         --ctx.sp;
//         ctx.stackCheck(this.nodes.length);
//         for (Node n: this.nodes) {
//             ctx.s[++ctx.sp] = n;
//         }
//     }
// 
//     @Override
//     public String toString() {
//         return new StringBuffer()
//             .append("NApp:")
//             .append(Arrays.toString(this.nodes))
//             .toString();
//     }
// }
// 
// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/nodes/Node.java
// public interface Node {
//     
//     /**
//      * Enter this node, evaluating it to normal form.
//      * @param ctx    {@link ExecutionContext} where this node is executing
//      */
//     void enter(ExecutionContext ctx);
//     
// }


// Path: bluejelly-runtime/src/main/java/bluejelly/runtime/ContMarker.java
import bluejelly.runtime.nodes.Executable;
import bluejelly.runtime.nodes.NApp;
import bluejelly.runtime.nodes.Node;

/*
 * The Bluejelly project, Copyright 2012.
 *
 * This source code is distributed under the terms of 
 * the BSD license, see the LICENSE file for details.
 */

package bluejelly.runtime;


/**
 * Continuation marker. Whenever found on the stack, it will jump
 * to the function associated to this continuation.
 * 
 * @author ppedemon
 */
public class ContMarker extends Marker {

    // Continuation
    private Executable cont;
    
    /**
     * Construct a continuation marker for the given {@link ExecutionContext}
     * @param ctx     associated context
     * @param cont    continuation code {@link Node}
     */
    public ContMarker(ExecutionContext ctx, Executable cont) {
        super(ctx);
        this.cont = cont;
    }

    /**
     * Is the code for this continuation a tycon matcher?
     * @return    whether the code for this continuation is a tycon matcher
     */
    public boolean isMatcher() {
        return cont.isMatcher();
    }
    
    /* 
     * So, an evaluation routine finished with a partial application. 
     * We must pack the whole stack frame on a NApp node, and schedule 
     * a jump to the continuation.
     * 
     * (non-Javadoc)
     * @see bluejelly.runtime.Marker#signalPap()
     */
    @Override
    protected void signalPap() {
        int n = this.ctx.sp - this.ctx.bp;
        Node[] nodes = this.ctx.collect(n);
        NApp napp = new NApp(nodes);