diff 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 diff
--- a/LightClone/Source/Program.cpp	Tue Sep 27 13:30:10 2011 -0700
+++ b/LightClone/Source/Program.cpp	Fri Sep 30 15:23:16 2011 -0700
@@ -60,17 +60,15 @@
 
 	kMachine.Reset();
 	kMachine.ClearMemory();
-	kMachine.RemoveAllFunctions();
+	//kMachine.RemoveAllFunctions();
 
-	for(uint32 i = 0; i < FunctionCount && eCode == Error_Success; ++i)
-	{
-		kMachine.AddFunction(i, kFunction[i].CalculateSize());
+	//uint32 nSize = kMachine.GetMemorySize();
+	//uint8* pData = kMachine.GetMemoryPointer();
 
-		uint32 nSize = kMachine.GetFunctionSize(i);
-		uint8* pData = kMachine.GetFunctionMemory(i);
-
-		eCode = Compile(i, pData, nSize);
-	}
+	//for(uint32 i = 0; i < FunctionCount && eCode == Error_Success; ++i)
+	//{
+	//	eCode = Compile(i, pData, nSize);
+	//}
 
 	return eCode;
 }
@@ -80,33 +78,67 @@
  */
 ErrorCode Program::Compile(uint32 nIndex, uint8* pData, uint32 nSize) const
 {
-	for(uint32 i = 0; i < Function::MaximumInstructionCount; ++i)
+	ErrorCode eCode = Error_Success;
+
+	for(uint32 i = 0; i < Function::MaximumInstructionCount && eCode == Error_Success; ++i)
 	{
-		const uint32 nAction = kFunction[nIndex].nInstruction[i];
+		// encode user-defined actions
+		eCode = EncodeAction(kFunction[nIndex].nInstruction[i], pData, nSize);
+	}
 
-		if(Action_Forward <= nAction && nAction <= Action_Light)
+	if(eCode == Error_Success)
+	{
+		if(nSize > 0)
+		{
+			// add implicit return statement
+			*pData++ = Instruction_End;
+		}
+		else
 		{
-			if(nSize < 2)
-				return Error_Fail;
+			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;
 
-			nSize -= 2;
+			return nSize -= 2, Error_Success;
 		}
-		else
+	}
+	else
 
-		if(Action_FunctionA <= nAction && nAction <= Action_FunctionB)
+	if(Action_FunctionA <= nAction && nAction <= Action_FunctionB)
+	{
+		if(nSize >= 2)
 		{
-			if(nSize < 2)
-				return Error_Fail;
-
 			*pData++ = Instruction_Call;
 			*pData++ = nAction - Action_FunctionA + 1;
 
-			nSize -= 2;
+			return nSize -= 2, Error_Success;
+		}
+	}
+	else
+
+	{
+		if(nSize >= 1)
+		{
+			*pData++ = Instruction_None;
+
+			return nSize -= 1, Error_Success;
 		}
 	}
 
-	return Error_Success;
+	return Error_Fail;
 }