/************************************************************************
       -= SEER - C-Scripting engine v 0.94a =-
State      : under implementation
Author     : Przemyslaw Podsiadly
             email: ppodsiad@elka.pw.edu.pl
             WWW:   http://home.elka.pw.edu.pl/~ppodsiad
File:exscript.c
Desc:example of script type: how to easly communicate between scripts
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <seer.h>

scScript cowScript,dogScript;

bool createCow(scScriptTypeHeader *s,char *name,char *typeOf)
{scInstance *cow;
 static init_adr=-1;
 cow=scCreate_Instance(cowScript,"");
 if (!cow)
  {
   printf("Error creating cow instance:\n%s\n",scErrorMsg);
   return false;
  }
 if (init_adr==-1)
  init_adr=scGet_Symbol(cow,"init");
 scCall_Instance(cow,init_adr,name);
 return scAssignInstance(s,cow,typeOf);
}

bool createDog(scScriptTypeHeader* s,char *name,char *typeOf)
{scInstance *dog;
 static init_adr=-1;
 dog=scCreate_Instance(dogScript,"");
 if (!dog)
  {
   printf("Error creating dog instance:\n%s\n",scErrorMsg);
   return false;
  }
 if (init_adr==-1)
  init_adr=scGet_Symbol(dog,"init");
 scCall_Instance(dog,init_adr,name);
 return scAssignInstance(s,dog,typeOf);
}

void killCow(scScriptTypeHeader* s)
{
 scCall_Instance(s->ins,scGet_Symbol(s->ins,"die"));
 scFree_Instance(s->ins);
 s->ins=NULL;
}

void killDog(scScriptTypeHeader* s)
{
 scCall_Instance(s->ins,scGet_Symbol(s->ins,"die"));
 scFree_Instance(s->ins);
 s->ins=NULL;
}

/*
This function tells SeeR addresses of functions and variables it can
use (import). If script imports symbol you don't tell the address SeeR
will complain : unresolved reference.Here we use only printf, but we
can add also scanf. In this way you also add variables.
*/
void Init_Externals()
{
 scAddExtSym(printf);
 scAddExtSym(createCow);
 scAddExtSym(createDog);
 scAddExtSym(killCow);
 scAddExtSym(killDog);
}

scScript compile(char *name)
{scScript scrpt;
 scrpt=scCompile_File(name);
//Error?
 if (scErrorNo)
   {
      printf("%s\n> %s\n",scErrorMsg,scErrorLine);exit(scErrorNo);
   }
 if (!scrpt)
   { printf("Unable to find %s!\n",name);exit(101);}
 return scrpt;
}

/*** THE MAIN ***************************************/
int main(int argc,char **argv)
{
//scrpt will contain the compiled script
 scScript script;
//and this is the instance of script, which we can call
//it contains all the data from script.
 scInstance *prog;

//let's give the script access to some functions of ours
 scInit_SeeR();
 Init_Externals();

//when SeeR is in debug mode, here it will store all debugging data
 scOpen_Debug("exscript.dbg");//start debugging

//compile example script : "exscript.sc"
 script=compile("exscript.sc");
//compile script exscrcow and exscrdog
 cowScript=compile("exscrcow.sc");
 dogScript=compile("exscrdog.sc");

//create instance of the script - you can run only instance of script,
//not script
 prog=scCreate_Instance(script,"");
//Error?
 if (!prog)
  {
   printf("Error creating instance!\n");
   return 1;
  }

//now, let's execute the function (here 'main' has same arguments as out main)
 scCall_Instance(prog,scGet_Symbol(prog,"main"),argc,argv);
//Error?
 if (scErrorNo)
      printf("%s\n",scErrorMsg);

//let's delete instance and script
 scFree_Instance(prog);
 free(script);

//close debugging - close the debug file also
 scClose_Debug();

 return 0;
}

