/*
 * (find-fline "~/PFORTH/mtamacros.h")
 * (find-fline "~/MTA/vtutilsh.c")
 *
 * pf_init:
 * (find-pftag "main")
 * (find-pftag "pfDoForth")
 *
 * pf_finish:
 * (find-pffile "csrc/pf_core.c" "\tpfDeleteTask( cftd );")
 *
 * pf_runforth: 
 * (find-pffile "csrc/pf_core.c" "Result = pfRunForth()")
 *
 * pf_includefile:
 * (find-pftag "pfIncludeFile")
 *
 * pf_evaluate:
 * (find-pffile "csrc/pf_guts.h" "PUSH_DATA_STACK(x)")
 * (find-pffile "csrc/pf_core.c" "AUTO.INIT")
 *
 * Other things:
 * (find-pftag "ffQuit")
 * (find-pftag "ffInterpret")
 * (find-pftag "ffPushInputStream")
 * (find-pftag "ffPopInputStream")
 * (find-pftag "pfExecByName")
 * (find-pffile "csrc/pf_core.c" "pfDoForth")
 * (find-pffile "csrc/pf_core.E-" "int32 pfDoForth")
 */


/* #include <stdio.h> */
/* #include "pforth.h" */
#include "pf_all.h"
#include "mtamacros.h"

static cfTaskData *cftd;
static cfDictionary *dic;

/* (find-fline "~/PFORTH/tclpf_custom.c")
 */
extern Tcl_Interp *tclpfinterp;


int ET_OBJCOMMAND_pf_init(ET_OBJARGS) {
  ExecToken EntryPoint = 0;
  char *dicname = OARGV_STRING(1, NULL);
  pfSetQuiet(1);
  pfInitGlobals();
  cftd = pfCreateTask(512, 512);
  pfSetCurrentTask(cftd);
  dic = pfLoadDictionary(dicname, &EntryPoint);
  if(dic == NULL)
    return TCL_ERROR;
  return TCL_OK;
}


int ET_OBJCOMMAND_pf_finish(ET_OBJARGS) {
  pfDeleteDictionary( dic );
  pfDeleteTask( cftd );
  return TCL_OK;
}


int ET_OBJCOMMAND_pf_runforth(ET_OBJARGS) {
  pfRunForth();
  return TCL_OK;
}


int ET_OBJCOMMAND_pf_includefile(ET_OBJARGS) {
  if(objc != 2)
    ET_OARGSERROR("FILENAME");
  pfIncludeFile( OARGV_STRING(1, NULL) );
  return TCL_OK;
}


int ET_OBJCOMMAND_pf_evaluate(ET_OBJARGS) {
  int len;
  if(objc != 2)
    ET_OARGSERROR("STRING");
  PUSH_DATA_STACK(OARGV_STRING(1, &len));
  PUSH_DATA_STACK(len);
  tclpfinterp = interp;
  pfExecByName("EVALUATE");
  return TCL_OK;
}
