/*
 *  Object of ALLL registers
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jALLL.jTObjects;

import jTMass.core.jAMass.TAgent;

public class TRegister extends TAtom {

    private int PLevel;
    private int PRegisterNumber;

    // ===== CLONING =====

    @Override
    public TObject cloneObject(){
        TRegister newRegister=new TRegister(PLevel,PRegisterNumber);
	return(newRegister);
    }

    // ===== STRING CONTENT ACCESS =====

    @Override
    public String getContent(){
        String regS="";
        for(int i=0;i<PLevel;i++)
            regS=regS+"'";
        String indS=((Integer)PRegisterNumber).toString();
        if(PRegisterNumber==TConstants._NORegisters+1)
            indS="L";
        if(PRegisterNumber==TConstants._NORegisters+2)
            indS="N";

        return(regS+"$"+indS);//+"(level "+new Integer(PLevel).toString()+")");  //Strange content
    }

    // ===== REGISTER ACCESS =====
	
    public boolean decrease(){
        if(PLevel>0) PLevel--;
            return(PLevel==0);
    }

    @Override
    public TObject substituteRegisters(TAgent agent){
        if(PLevel==0){
            TObject register;
            register=agent.getRegister(PRegisterNumber);
            if((register instanceof TAtom)||(register instanceof TList))
                return(register);
            else
                return(new TAtom("#Wrong Register"));
	}
	else 
            return(this);
    }

    @Override
    public void matureRegisters(){
        if(PLevel>0)
            PLevel--;
    }

    // ===== CONSTRUCTOR =====

    public TRegister(int level, int regNo){
        PRegisterNumber=regNo;
	if(level>0)
            PLevel=level;
	else
            PLevel=0;
    }
	
    public TRegister(){
	PRegisterNumber=0;
	PLevel=-1;
    }

}