/// TKS_FunctionCallStackEntry.h 
///
/// (c) 2001-2008 Bastian Spiegel <bs@tkscript.de> 
///     - distributed under the terms of the GNU general public license (GPL). 
/// 

#include "tks.h"

#include "tks_inc_class.h"
#include "tks_inc_compiler.h"

#include "TKS_FunctionCallStackEntry.h"
 
 
void TKS_FunctionCallStackEntry::init(TKS_FunctionCallStackEntry *_src) {
   function    = _src->function;
   commandY    = _src->commandY;
   yclass_name = _src->yclass_name;
   src_loc     = _src->src_loc;
}

void TKS_FunctionCallStackEntry::print(sSI _line, sSI _sid) {
   (void)_sid;

   if(function != NULL) 
   { 
      // ---- script function / method 
      const char *callerModName = Dtkscompiler->getModuleNameByIndex((sU16)((src_loc>>16)&0xFFFF));
      printf("[---] %6i.: \"%s\":%u: %s::%s() (in \"%s\":%u)\n",  
         _line, 
         callerModName,  
         (sUI)((src_loc&0xFFFF)+1), 
         function->parent_class?((char*)function->parent_class->name.chars):"", 
         (char*)function->name.chars, 
         Dtkscompiler->getModuleNameByIndex((sU16)((function->src_loc>>16)&0xFFFF)),  
         (sUI)((function->src_loc&0xFFFF)+1) 
         );  
   } 
   else 
   { 
      // ---- YAC/C++ function / method 
      printf("[---] %6i.: \"%s\":%u: <native> %s::%s()\n", 
         _line, 
         Dtkscompiler->getModuleNameByIndex((sU16)((src_loc>>16)&0xFFFF)),
         (sUI)((src_loc&0xFFFF)+1),
         (char*)yclass_name, 
         (char*)commandY->y_name 
         ); 
   } 
}

void TKS_FunctionCallStackEntry::appendString(YAC_String *_s, sSI _line) {
   YAC_String t; t.alloc(32);
   t.printf("%6i", _line);
   _s->append(&t);
   _s->append(".: \"");
   _s->append(Dtkscompiler->getModuleNameByIndex((sU16)((src_loc>>16)&0xFFFF)));
   _s->append("\":");
   t.printf("%i",(src_loc&0xFFFF)+1);
   _s->append(&t);
   _s->append(": ");

   if(function != NULL) 
   { 
      // ---- script function / method 
      if(function->parent_class != NULL)
      {
         _s->append(&function->parent_class->name);
      }
      _s->append("::");
      _s->append(&function->name);
      _s->append("() (in \"");
      _s->append(Dtkscompiler->getModuleNameByIndex((sU16)((function->src_loc>>16)&0xFFFF)));
      _s->append("\":");
      t.printf("%i", (function->src_loc&0xFFFF)+1);
      _s->append(&t);
      _s->append(")\n");
   } 
   else 
   { 
      // ---- YAC/C++ function / method 
      _s->append("<native> ");
      _s->append(yclass_name);
      _s->append("::");
      _s->append((char*)commandY->y_name);
      _s->append("()\n");
   } 
}
