// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   CompiledFunctionSet.java

package lt.monarch.function;

import java.lang.reflect.Field;
import java.util.Vector;
import lt.monarch.jasm.*;

// Referenced classes of package lt.monarch.function:
//			NodeCompiler, Variable

public class CompiledFunctionSet
{

	public ClassBuilder classBuilder;
	Vector referencedVariables;
	private boolean completed;

	public void addToString(String s)
	{
		MethodBuilder methodbuilder = new MethodBuilder(classBuilder.constantPool);
		methodbuilder.setName("toString", "()Ljava/lang/String;");
		methodbuilder.setFlags(1);
		methodbuilder.code.reserveArguments(1);
		methodbuilder.code.reserveStack(1);
		methodbuilder.code.append(18);
		methodbuilder.code.append(classBuilder.constantPool.define(s));
		methodbuilder.code.append(176);
		classBuilder.addMethod(methodbuilder);
	}

	public void addInterface(String s)
	{
		classBuilder.addInterface(s);
	}

	public boolean hasMethod(String s, String s1)
	{
		return classBuilder.hasMethod(s, s1);
	}

	public void addMethod(NodeCompiler nodecompiler)
	{
		classBuilder.addMethod(nodecompiler.method);
	}

	int getReferencedVariableIndex(Variable variable)
	{
		int i = referencedVariables.indexOf(variable);
		if(i == -1)
		{
			referencedVariables.addElement(variable);
			i = referencedVariables.size() - 1;
		}
		return i;
	}

	public void complete()
	{
		if(completed)
			return;
		completed = true;
		if(referencedVariables.size() != 0)
		{
			FieldBuilder fieldbuilder = new FieldBuilder(classBuilder.constantPool);
			fieldbuilder.setName("globals", "[Llt/monarch/function/Variable;");
			fieldbuilder.setFlags(9);
			classBuilder.addField(fieldbuilder);
		}
	}

	public Class buildClass()
		throws Exception
	{
		try
		{
			complete();
			Class class1 = classBuilder.buildClass();
			if(referencedVariables.size() != 0)
			{
				Field field = class1.getField("globals");
				Variable avariable[] = new Variable[referencedVariables.size()];
				referencedVariables.copyInto(avariable);
				field.set(null, avariable);
			}
			return class1;
		}
		catch(NoSuchFieldException nosuchfieldexception)
		{
			return null;
		}
		catch(IllegalAccessException illegalaccessexception)
		{
			return null;
		}
		catch(NullPointerException nullpointerexception)
		{
			return null;
		}
	}

	public CompiledFunctionSet()
	{
		completed = false;
		classBuilder = new ClassBuilder("_CompiledFunctionSet_");
		classBuilder.addDefaultConstructor();
		referencedVariables = new Vector();
	}
}
