view LightClone/Source/Program.cpp @ 53:8cefb65577cc

Clean up world states
author koryspansel
date Fri, 30 Sep 2011 15:23:16 -0700
parents 2caa7c7e2cb5
children 95677f648a2c
line wrap: on
line source

/*
 * Program
 */

#include "Program.h"

/*
 * Program
 */
Program::Program()
{
}

/*
 * Initialize
 */
ErrorCode Program::Initialize()
{
	return Clear(), Error_Success;
}

/*
 * Terminate
 */
void Program::Terminate()
{
}

/*
 * SetAction
 */
void Program::SetAction(uint32 nFunction, uint32 nIndex, uint32 nAction)
{
	if(nFunction < FunctionCount && nIndex < Function::MaximumInstructionCount)
	{
		kFunction[nFunction].nInstruction[nIndex] = nAction;
	}
}

/*
 * Clear
 */
void Program::Clear()
{
	for(uint32 i = 0; i < FunctionCount; ++i)
	{
		for(uint32 j = 0; j < Function::MaximumInstructionCount; ++j)
		{
			kFunction[i].nInstruction[j] = Action_Default;
		}
	}
}

/*
 * Upload
 */
ErrorCode Program::Upload(VirtualMachine& kMachine) const
{
	ErrorCode eCode = Error_Success;

	kMachine.Reset();
	kMachine.ClearMemory();
	//kMachine.RemoveAllFunctions();

	//uint32 nSize = kMachine.GetMemorySize();
	//uint8* pData = kMachine.GetMemoryPointer();

	//for(uint32 i = 0; i < FunctionCount && eCode == Error_Success; ++i)
	//{
	//	eCode = Compile(i, pData, nSize);
	//}

	return eCode;
}

/*
 * Compile
 */
ErrorCode Program::Compile(uint32 nIndex, uint8* pData, uint32 nSize) const
{
	ErrorCode eCode = Error_Success;

	for(uint32 i = 0; i < Function::MaximumInstructionCount && eCode == Error_Success; ++i)
	{
		// encode user-defined actions
		eCode = EncodeAction(kFunction[nIndex].nInstruction[i], pData, nSize);
	}

	if(eCode == Error_Success)
	{
		if(nSize > 0)
		{
			// add implicit return statement
			*pData++ = Instruction_End;
		}
		else
		{
			eCode = Error_Fail;
		}
	}

	return eCode;
}

/*
 * EncodeAction
 */
ErrorCode Program::EncodeAction(uint32 nAction, uint8*& pData, uint32& nSize) const
{
	if(Action_Forward <= nAction && nAction <= Action_Light)
	{
		if(nSize >= 2)
		{
			*pData++ = Instruction_Action;
			*pData++ = nAction;

			return nSize -= 2, Error_Success;
		}
	}
	else

	if(Action_FunctionA <= nAction && nAction <= Action_FunctionB)
	{
		if(nSize >= 2)
		{
			*pData++ = Instruction_Call;
			*pData++ = nAction - Action_FunctionA + 1;

			return nSize -= 2, Error_Success;
		}
	}
	else

	{
		if(nSize >= 1)
		{
			*pData++ = Instruction_None;

			return nSize -= 1, Error_Success;
		}
	}

	return Error_Fail;
}