package code.instr;

import code.space.Computation;
import code.space.Space;
import code.stuff.Logger;
import code.stuff.Tracer;
import code.symbols.DataSymbol;
import code.term.Term;
import code.term.Variable;

/**
 * Load in the machine register a subterm of the top of the pre-redex
 * stack.  The subterm is identified by a <I>position</I>, possibly
 * null sequence of naturals.
 *
 * @author Sergio Antoy
 * @since June 17, 2003
 */

public class Load implements Instruction {

    /**
     * The position of the subterm of top of pre-redex stack
     * that should be loaded in the "current" register.
     */
    private final byte[] argumentNumber;


    public Load(byte argumentNumber) {
        this.argumentNumber = new byte[]{argumentNumber};
    }

    public Load(byte[] argumentNumber) {
        this.argumentNumber = argumentNumber;
    }

    public void execute(Computation computation) {
        Term term = computation.getTerm();
        Term tmp = term;
        for (int i = 0; i < argumentNumber.length; i++)
            tmp = tmp.getArgument(argumentNumber[i]);
        Space.instance.current = tmp;
        if (Tracer.instruction) {
            Logger.log(computation.getIdString() + ": Load: ");
            for (int i = 0; i < argumentNumber.length; i++) {
                Logger.log("" + argumentNumber[i]);
                Logger.log(",");
            }
            Logger.log("");
            Logger.logln(" from " + term + " gives " + tmp);
        }
    }

    public String printAsTxtLoadable() {
        String s = new String("Load");
        for (int i = 0; i < argumentNumber.length; ++i) {
            s = s + " " + argumentNumber[i];
        }
        return s;
    }
}
