Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / MFC

2D LUA Based Robot Simulator

Rate me:
Please Sign up or sign in to vote.
4.89/5 (26 votes)
14 Apr 2014Public Domain9 min read 130.7K   7.9K   119  
An article on designing your own robot simulator
// ---------------------------------------------------------------------------
// FILE NAME            : LuaDebugger.cpp
// ---------------------------------------------------------------------------
// DESCRIPTION :
//
// Simple debugging routines
// 
// ---------------------------------------------------------------------------
// VERSION              : 1.00
// DATE                 : 1-Sep-2005
// AUTHOR               : Richard Shephard
// ---------------------------------------------------------------------------
// LIBRARY INCLUDE FILES
#include "stdafx.h"
#include "luadebugger.h"
#include "string.h"
// ---------------------------------------------------------------------------

bool CLuaDebugger::bAbortScript = false;

// typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
void CLuaDebugger::LuaHookCall (lua_State *lua)
{
   
   printf ("---- Call Stack ----\n");
   //printf ("[Level] [Function] [# args] [@line] [src]\n");

   lua_Debug ar;
   
   // Look at call stack
   for (int iLevel = 0; lua_getstack (lua, iLevel, &ar) != 0; ++iLevel)
   {
      if (lua_getinfo (lua, "Snlu", &ar) != 0)
      {
         printf ("%d %s %s %d @%d %s\n", iLevel, ar.namewhat, ar.name, ar.nups, ar.linedefined, ar.short_src);
      }
   }
}

void CLuaDebugger::LuaHookRet (lua_State *lua)
{
   
}

void CLuaDebugger::LuaHookLine (lua_State *lua)
{
	// +- Modified by Auralius based : http://www.codegurus.be/codegurus/Programming/luahooks_en.htm
	// Check the global flag to know if we should abort
    if(bAbortScript)
    {
      // Ok, let's abort the script
      lua_pushstring(lua, "HookRoutine: Abort requested!");
      lua_error(lua);
    }
}

void CLuaDebugger::LuaHookCount (lua_State *lua)
{
   LuaHookLine (lua);
}

void CLuaDebugger::LuaHook (lua_State *lua, lua_Debug *ar)
{
   // Handover to the correct hook
   switch (ar->event)
   {
   case LUA_HOOKCALL:
      LuaHookCall (lua);
      break;
   case LUA_HOOKRET:
   case LUA_HOOKTAILRET:
      LuaHookRet (lua);
      break;
   case LUA_HOOKLINE:
      LuaHookLine (lua);
      break;
   case LUA_HOOKCOUNT:
      LuaHookCount (lua);
      break;
   }
}

//============================================================================

CLuaDebugger::CLuaDebugger (CLuaVirtualMachine& vm) : m_iCountMask (10), m_vm (vm)
{
   // Clear all current hooks
   if (vm.Ok ())
   {
      vm.AttachDebugger (this);
      lua_sethook ((lua_State *) vm, LuaHook, 0, m_iCountMask);
   }
   bAbortScript = false;
}

CLuaDebugger::~CLuaDebugger (void)
{
   // Clear all current hooks
   if (m_vm.Ok ())
   {
      lua_sethook ((lua_State *) m_vm, LuaHook, 0, m_iCountMask);
   }
}

void CLuaDebugger::SetHooksFlag (int iMask)
{
   // Set hooks
   lua_sethook ((lua_State *) m_vm, LuaHook, iMask, m_iCountMask);
}

// +- Modified by Auralius Jan 10, 2009
void CLuaDebugger::ErrorRun (int iErrorCode)
{
   /*switch (iErrorCode)
   {
   case LUA_ERRRUN:
      strcpy(errorMsg, "LUA_ERRRUN\n");
      break;
   case LUA_ERRMEM:
      strcpy(errorMsg, "LUA_ERRMEM\n");
      break;
   case LUA_ERRERR:
	  strcpy(errorMsg, "LUA_ERRERR\n");
      break;
   }*/

   // Get the error string that appears on top of stack when a function
   //	fails to run
	sprintf(errorMsg, "Error code: %i\r\nLine:%s\n", iErrorCode, lua_tostring ((lua_State *) m_vm, -1));
}

// ++ Added by Auralius Jan 10, 2009
void CLuaDebugger::GetErrorMsg(char *msg)
{
	strcpy(msg, errorMsg);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Student
Indonesia Indonesia
http://kataauralius.com/

Comments and Discussions