using System;
using System.Collections;

namespace Liss
{
    /// 
	/// This class is responsable by save the information for the variables
	/// referenced in a program.
	/// 

	public class Entry
	{
		public VM_Code genCode = new VM_Code();


		public string name;			// the name of identifier, which could be a variable or type
		public string classO;		// the class of identifier - type, var, subprogram...
		public string type;			// the type of identifier - atomic, boolean, array, ...
		public int size;			// the size of variable
       	public int memAdress;		// the value where the memory address of variable begins
		public object initValues;	// the initial value of variable
		public object dim;			// to save the dimensions of a array (p.e. c -> Array size 4,5
									// will be saved {4,5}
		public int level;			// indicate the level of a variable (used on sub-programs)
		public string initCode;		// C code for a variable initialization
		public Entry next;
		public object numArgs;	// number of arguments of a subprogram
		
		public ArrayList glVars;	// List of variables in a subprogram that must be passed as an argument


		// Default constructor.
		public Entry()
		{
		}

		//
		public Entry(string name1,string classO1,string type1,int size1,int memAdress1,object obj1,object dim1,int level1,Entry next1)
		{
			this.name = name1;
			this.classO = classO1;
			this.type = type1;
			this.size = size1;
			this.memAdress = memAdress1;
			this.initValues = obj1;
			this.dim = dim1;
			this.level = level1;
			this.next = next1;
		}

		// Pretty printing of a Entry
		public override string ToString()
		{
			string str ="";

			if (this.level == 0)
				str = this.asString();
			else 
				if (this.next != null)
				{
					Entry p = this.next;
					str = p.ToString();
				}


			return str;
		}

		public string asString()
		{
			string str = "";
            
			if (! this.type.Equals("Set"))
			{
				if (! this.type.Equals("Sequence"))
				{
					if (! this.initValues.GetType().Name.Equals("ArrayList"))
						str = genCode.Pushi((int)this.initValues);
					else
					{
						ArrayList array = (ArrayList) this.initValues;
						for (int k = 0; k < array.Count; k++)
							str += genCode.Pushi((int)array[k]);
					}
				}
				else
					str = this.initValues.ToString();
				str += "\n";
			}
			return str;
		}

		public string asLocal(int count)
		{
			string str = "";

			if (! this.type.Equals("Set"))
			{
				if (! this.initValues.GetType().Name.Equals("ArrayList"))
					str = genCode.Pushi((int)this.initValues);
				else
				{
					ArrayList array = (ArrayList) this.initValues;
					for (int k = 0; k < array.Count; k++)
						str += genCode.Pushi((int)array[k]);
				}
				str += "\n";
			}
			return str;
		}
	}
}
