/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__SCRIPT_DEBUGGER__
#define	__SCRIPT_DEBUGGER__


		#include "script/script_vm.h"
		#include "script/script_engine_types.h"


namespace GS {
	namespace NML { class Tag; }
	namespace Script {

/// Debugger variable.
struct	DebuggerVariable
{
		String		id;
		uint		type;
		String		type_string;

		bool		referenced;
		bool		modified;

		AutoList <DebuggerVariable *> member_list;

		union
		{
			bool	v_bool;
			int		v_int;
			float	v_float;
		};

		String		v_string;
		String		value;

		DebuggerVariable()
		{
			type = 0;
			referenced = false;
		}
};

/// Source breakpoint.
struct	SourceBreakpoint
{
		String		source;
		int			line;

		SourceBreakpoint(const char *s, int l)
		{	source = s; line = l;	}
};

/*
	@short	Script debugger interface.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
class	IDebugger
{
protected:

		bool		is_suspended,
					is_stepping;

		int			debug_stack_frame,
					step_to_callstack_depth;

		DebuggerVariable *locals_root;

		AutoList <DebuggerVariable *> local_var_tree;
		AutoList <SourceBreakpoint *> source_breakpoint_list;

		/// Mark all variables in tree as non-referenced.
		void		DereferenceVarTree(const List <DebuggerVariable *> &tree);

public:

/*!
	@name	Interface
	@{
*/
		/// Format variable parameter.
virtual	String		FormatParameter(DebuggerVariable *variable) = 0;
		/// Format safe ptr parameter.
virtual	String		FormatUserObjectParameter(CObjectType type) = 0;
		/// Convert a debugger variable to a meta string.
virtual	void		VariableToMetatagString(DebuggerVariable *, String &) = 0;

		/// Get call stack depth.
virtual	int			GetCallstackDepth() = 0;
		/// Get current script execution call frame index.
virtual	int			GetStackFrameIndex() = 0;

		/// Refresh stack locals list.
virtual	void		RefreshStackFrameLocalsCache() = 0;
		/// Get source/line info for the current debug stack frame.
virtual	void		GetStackFrameSource(const char *&, int &) = 0;
/// @}

/*!
	@name	Breakpoint.
	@{
*/
		/// Check for suspend on breakpoint.
		void		CheckSuspendOnBreakpoint(const char *source, int line);
		/// Check for suspend on step.
		void		CheckSuspendOnStep();

		/// Find a source breakpoint.
		SourceBreakpoint *FindSourceBreakpoint(const char *source, int line);
		/// Add a new source breakpoint.
		SourceBreakpoint *AddSourceBreakpoint(const char *source, int line);
		/// Remove a source breakpoint.
		bool		RemoveSourceBreakpoint(SourceBreakpoint *breakpoint);

		/// Set breakpoints from a tag.
		bool		SetBreakpoints(const NML::Tag &tag);
///	@}

/*!
	@name	Execution control.
	@{
*/
		/// Suspend execution.
		void		Suspend();
		/// Resume execution.
		void		Resume();

		/// Step to the next instruction.
		void		Step();
		/// Step into the next call.
		void		StepInto();
		/// Step out of the current call.
		void		StepOut();

		/// Return whether the current scene script execution is suspended or not.
		bool		IsSuspended() const { return is_suspended; }
		/// Is the debugger stepping.
		bool		IsStepping() const { return is_stepping; }
///	@}

/*!
	@name	Interface control.
	@{
*/
		/// Set the call stack debug depth.
		void		SetDebugStackFrame(int depth);
		/// Get call stack debug depth.
		int			GetDebugStackFrame() const { return debug_stack_frame; }

		/// Get stack locals as a meta string.
		String		GetStackFrameLocals();

		void		Reset();
///	@}

		IDebugger();
virtual	~IDebugger();
};

	} // Script
} // GS


#endif	// __SCRIPT_DEBUGGER__
