Changeset 297c5f5 in tol for tol/contrib/cint/tolCint.cpp


Ignore:
Timestamp:
Dec 15, 2010, 2:23:26 PM (16 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
7cdd510
Parents:
a29d86c4
Message:

Refs #1029
Using Gdouble and Gint
New methods Cint.initialize and Cint.scratch_all

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@2832 be4846ae-f051-483a-831f-b64e7d08527f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol/contrib/cint/tolCint.cpp

    ra29d86c4 r297c5f5  
    3131#include <tol/tol_bdtegra.h>
    3232#include <tol/tol_btxtgra.h>
     33#include <tol/tol_bsetgra.h>
    3334
    3435//#pragma pack(16)
    35 //#include <Api.h>
     36#include <Api.h>
    3637#include <G__ci.h>
    3738//#pragma pack(4)
    … …  
    6970}
    7071
    71 
    72 //--------------------------------------------------------------------
    73 int Cint_initialize()
    74 //--------------------------------------------------------------------
    75 {
    76   int G__init_cint_ = G__init_cint("cint");
    77   G__set_errmsgcallback(reinterpret_cast<void*>(Cint_errmsgcallback));
    78   G__setautoconsole(1);
    79   return(G__init_cint_);
    80 }
    81 
    82 //--------------------------------------------------------------------
    83 static int G__init_cint__ = Cint_initialize();
    84 //--------------------------------------------------------------------
     72//--------------------------------------------------------------------
     73int& Cint_is_initialized()
     74//--------------------------------------------------------------------
     75{
     76  static int _Cint__initialized_ = false;
     77  return(_Cint__initialized_);
     78}
     79
     80//--------------------------------------------------------------------
     81int Cint_initialize(const BText& filePath)
     82//--------------------------------------------------------------------
     83{
     84  if(Cint_is_initialized())
     85  {
     86    return(G__INIT_CINT_SUCCESS);
     87  }
     88  else
     89  {
     90    Cint_is_initialized() = true;
     91    BText order = "cint";
     92    if(filePath.HasName()) { order += BText(" ")+filePath; }
     93    int G__init_cint_ = G__init_cint(order);
     94    switch(G__init_cint_) {
     95    case G__INIT_CINT_SUCCESS_MAIN:
     96      Error("Cint called out of main should never happen G__INIT_CINT_SUCCESS_MAIN.\n");
     97      /* Should never happen */
     98      break;
     99    case G__INIT_CINT_SUCCESS:
     100      Std("\nCint has been succesfully initialized.\n");
     101      break;
     102    case G__INIT_CINT_FAILURE:
     103    default:
     104      Error("Cint initialization failed G__INIT_CINT_FAILURE.\n");
     105    }
     106    G__set_errmsgcallback(Cint_errmsgcallback);
     107    return(G__init_cint_);
     108  }
     109}
     110
     111//--------------------------------------------------------------------
     112int Cint_scratch_all()
     113//--------------------------------------------------------------------
     114{
     115  if(Cint_is_initialized())
     116  {
     117    return(false);
     118  }
     119  else
     120  {
     121    Cint_is_initialized() = false;
     122    return(Cint_scratch_all());
     123  }
     124}
     125
     126//--------------------------------------------------------------------
     127  DeclareContensClass(BDat, BDatTemporary, BDatCint_initialize);
     128  DefExtOpr(1, BDatCint_initialize, "Cint.initialize", 1, 1, "Text",
     129  "(Text filePath)",
     130  I2("Initializes the CINT environment with an optional C/C++ file.",
     131     "Inicilaliza el entorno de CINT con un fichero C/C++ opcional.")+
     132     cint_url_link(),
     133     BOperClassify::Conversion_);
     134  void BDatCint_initialize::CalcContens()
     135//--------------------------------------------------------------------
     136{
     137  if(CheckNonDeclarativeAction("Cint.initialize")) { return; }
     138  BText& filePath = Text(Arg(1));
     139  contens_ = Cint_initialize(filePath);
     140}
     141
     142
     143//--------------------------------------------------------------------
     144  DeclareContensClass(BDat, BDatTemporary, BDatCint_scratch_all);
     145  DefExtOpr(1, BDatCint_scratch_all, "Cint.scratch_all", 1, 1, "Real",
     146  "(Real unused)",
     147  I2("Terminates the interpreter and frees heap memory used by it."
     148     "The interpreter should not be used after call this function."
     149     "To resume the interpreter, user needs to call function Cint.initialize.",
     150     "Termina el intérprete y libera memoria heap usada por él."
     151     "El intérprete no debe utilizarse después de llamar a esta función."
     152     "Para continuar con el intérprete, el usuario debe llamar a la "
     153     "función Cint.initialize.")+
     154     cint_url_link(),
     155     BOperClassify::Conversion_);
     156  void BDatCint_scratch_all::CalcContens()
     157//--------------------------------------------------------------------
     158{
     159  if(CheckNonDeclarativeAction("Cint.scratch_all")) { return; }
     160  contens_ = Cint_scratch_all();
     161}
    85162
    86163
    … …  
    121198//--------------------------------------------------------------------
    122199  DeclareContensClass(BDat, BDatTemporary, BDatCINT_exec_tempfile);
    123   DefExtOpr(1, BDatCINT_exec_tempfile, "Cint.exec_text", 1, 1, "Text",
     200  DefExtOpr(1, BDatCINT_exec_tempfile, "Cint.exec_tempfile", 1, 1, "Text",
     201  "(Text filePath)",
     202  I2("Opens temporary source file and execute it in the current context. "
     203     "The tempfile can contain variable declaration, struct, union, "
     204     "enum definitions, conditional expressions, loop and control "
     205     "statements but cannot contain function nor class definitions, "
     206     "wich can be loaded just calling Cint.loadfile",
     207     "Abre un archivo temporal de código fuente, que se ejecuta en el "
     208     "contexto actual. El archivo temporal puede contener declaración "
     209     "de variables, definiones de struct, union, enum, expresiones "
     210     "condicionales, órdenes de control y bucles, pero no puede "
     211     "contener definiciones de función ni de clase, las cuales "
     212     "sólo pueden cargarse con Cint.loadfile"),
     213     BOperClassify::Conversion_);
     214  void BDatCINT_exec_tempfile::CalcContens()
     215//--------------------------------------------------------------------
     216{
     217  if(CheckNonDeclarativeAction("Cint.exec_tempfile")) { return; }
     218  BText& filePath = Text(Arg(1));
     219  G__exec_tempfile(filePath.String());
     220  contens_ = true;
     221}
     222
     223
     224//--------------------------------------------------------------------
     225  DeclareContensClass(BDat, BDatTemporary, BDatCINT_exec_text);
     226  DefExtOpr(1, BDatCINT_exec_text, "Cint.exec_text", 1, 1, "Text",
    124227  "(Text expression)",
    125228    I2("Executes any C/C++ expression or set of expressions.\n"
    … …  
    136239     cint_url_link(),
    137240     BOperClassify::Conversion_);
    138   void BDatCINT_exec_tempfile::CalcContens()
     241  void BDatCINT_exec_text::CalcContens()
    139242//--------------------------------------------------------------------
    140243{
    … …  
    166269  if(CheckNonDeclarativeAction("Cint.calc")) { return; }
    167270  BText& expression = Text(Arg(1));
    168   G__value g = G__calc(expression.String());
     271  contens_ = G__double(G__calc(expression));
     272/*
     273  G__value g = G__calc(expression);
    169274  switch (g.type)
    170275  {
    171276    case 100: contens_ = g.obj.d; break;
    172277    case 105: contens_ = g.obj.i; break;
    173     default: Error(I2("Unknown CINT type ",
    174                       "Tipo CINT desconocido ")<<g.type);
    175   }
    176 }
    177 
    178 
    179 
    180 //--------------------------------------------------------------------
    181   DeclareContensClass(BDat, BDatTemporary, BDatCINT_import_from_tol);
    182   DefExtOpr(1, BDatCINT_import_from_tol,
    183   "Cint.import_from_tol", 1, 0, "Anything Anything",
    184   "(Anything var1 [, Anything var2, ...])",
     278    default: Error(BText("[Cint.calc] ")+
     279     I2("Unknown CINT numerical type ",
     280                      "Tipo CINT numérico desconocido ")<<g.type);
     281  }
     282*/
     283}
     284
     285//--------------------------------------------------------------------
     286  DeclareContensClass(BSet, BSetTemporary, BSetCINT_import_from_tol);
     287  DefExtOpr(1, BSetCINT_import_from_tol,
     288  "Cint.import_from_tol", 1, 4, "Set Text Text Text",
     289  "(Set vars [, Text prefix=\"\", Text suffix=\"\", Text namespace=\"\"])",
    185290  I2("Imports TOL variables, global or local, into CINT global scope.\n"
    186      "Returns the number of imported variables\n"
     291     "Returns the names of imported CINT variables.\n"
    187292     "TOL variables must have a valid name in TOL and C languages.\n"
    188293     "At this moment these are the available TOL types :\n",   
    189294     "Importa variables TOL, globales o locales, al ámbito global de CINT\n"
    190      "Devuelve el número total de variables correctamen te importadas.\n"
     295     "Devuelve los nombres de las variables CINT correctamente importadas.\n"
    191296     "Las variables TOL deben tener un nombre válido en los lenguajes "
    192297     "TOL y C\n"
    … …  
    199304     "Puede ver la declaración de los correspondientes tipos C/C++ en ")+
    200305     "\nhttps://www.tol-project.org/browser/tolp/trunk/tol/"
    201      "stdlib/general/TolCint/tol_cint_casting.cpp"+
     306     "stdlib/general/TolCint/tol_cint_casting.cpp\n"+
    202307  I2("Imported variables can be modified by CINT routines but memory "
    203308     "allocation and the internal structure cannot be altered. "
    204309     "For example, you can change the elements of a Matrix but cannot "
    205      "change the number of cells, rows nor columns.",
     310     "change the number of cells, rows nor columns.\n",
    206311     "Las variables importadas pueden ser modificadas por rutinas CINT "
    207312     "pero el alojo de memoria no puede ser alterado. "
    208313     "Por ejemplo, es posible cambiar los elementos de una matriz pero "
    209      "no el número de celdas, filas o columnas")+
     314     "no el número de celdas, filas o columnas.\n")+
     315  I2("If argument prefix is not empty then it will added before each TOL name to "
     316     "create transformed CINT names.\n",
     317     "Si el argumento prefix no es vacío se añadirá por delante de cada nombre TOL "
     318     "para crear los nombres CINT transformados.\n")+
     319  I2("If argument suffix is not empty then it will added after each TOL name to "
     320     "create transformed CINT names.\n",
     321     "Si el argumento suffix no es vacío se añadirá por detrás de cada nombre TOL "
     322     "para crear los nombres CINT transformados.\n") +
     323  I2("If argument namespace is not empty then CINT variables will be created in "
     324     "in the speciefied and pre-existant namespace instead of global scope.\n",
     325     "Si el argumento namespace no es vacío se crearán las variables dentro "
     326     "de dicho namespace preexistente, en lugar de en el ámbito global.\n")+
    210327    cint_url_link(),
    211328  BOperClassify::Conversion_);
    212   void BDatCINT_import_from_tol::CalcContens()
     329  void BSetCINT_import_from_tol::CalcContens()
    213330//--------------------------------------------------------------------
    214331{
    … …  
    216333 
    217334  BSyntaxObject* var = NULL;
     335  BGrammar* gra = NULL;
    218336  int numArg;
    219   contens_ = 0;
    220  
    221   for(numArg = 1; var=Arg(numArg); numArg++)
    222   {
    223     BGrammar* gra = var->Grammar();
     337  BSet& arg = Set(Arg(1));
     338  BText prefix_ = "";
     339  BText sufix_ = "";
     340  BText namespace_ = "";
     341  if(Arg(2)) { prefix_ = Text(Arg(2)); }
     342  if(Arg(3)) { sufix_ = Text(Arg(3)); }
     343  if(Arg(4)) { namespace_ = Text(Arg(4)); }
     344  BText expression = "";
     345  if(namespace_.HasName())
     346  {
     347    expression = BText("namespace ") + namespace_ + " {\n";
     348  };
     349  int c = arg.Card();
     350  contens_.PrepareStore(c);
     351  for(numArg = 1; numArg<=c; numArg++)
     352  {
     353    var = arg[numArg];
     354    if(!var) { continue; }
     355    gra = var->Grammar();
     356    BText pns = prefix_+var->Name()+sufix_;
    224357    if(gra==GraReal())
    225358    {
    … …  
    227360      double* ptr = (double*)(&dat);
    228361      int ptrNum = (int)ptr;
    229       BText expression = BText("double& ")+var->Name()+" = *((double *)("<<ptrNum<<"))";
     362      expression += BText("double& ")+pns+" = *((double *)("<<ptrNum<<"));\n";
    230363    //Std(BText("TRACE [Cint.import_from_tol] ")+expression);
    231       G__exec_text(expression);
    232       contens_ += 1;
     364      contens_.AddElement(new BContensText("",pns,""));
    233365    }
    234366    else if(gra==GraMatrix())
    … …  
    237369      double* ptr = (double*)(mat.GetData().GetBuffer());
    238370      int ptrNum = (int)ptr;
    239       BText expression = BText("Matrix ")+var->Name()+" ( "<<
     371      expression += BText("Matrix ")+pns+" ( "<<
    240372        mat.Rows()<<","<<
    241373        mat.Columns()<<","<<
    242         " ((double *)("<<ptrNum<<")) ); 0;";
     374        " ((double *)("<<ptrNum<<")) );\n";
    243375    //Std(BText("TRACE [Cint.import_from_tol] ")+expression);
    244       G__exec_text(expression);
    245       contens_ += 1;
     376      contens_.AddElement(new BContensText("",pns,""));
    246377    }
    247378    else if(gra==GraText())
    … …  
    250381      char* ptr = (char*)(txt.Buffer());
    251382      int ptrNum = (int)ptr;
    252       BText expression = BText("Text ")+var->Name()+" ( "<<
     383      expression += BText("Text ")+pns+" ( "<<
    253384        txt.Length()<<","<<
    254         " ((char *)("<<ptrNum<<")) ); 0;";
     385        " ((char *)("<<ptrNum<<")) );\n";
    255386    //Std(BText("TRACE [Cint.import_from_tol] ")+expression);
    256       G__exec_text(expression);
    257       contens_ += 1;
     387      contens_.AddElement(new BContensText("",pns,""));
    258388    }
    259389    else
    260390    {
    261391      Warning(BText("[Cint.import_from_tol] ")+
    262         "Cannot import TOL variables of type "<<gra->Name());
    263     }
    264   }
    265 }
    266 
     392        I2("Cannot import TOL variable ",
     393           "No se puede importar la variable TOL ")<<
     394           gra->Name()+" "+var->Name());
     395    }
     396  }
     397  if(namespace_.HasName())
     398  {
     399    expression += "\n};";
     400  };
     401//Std(BText("\nTRACE [Cint.import_from_tol] expression=[\n")+expression+"\n]\n");
     402  G__exec_text(expression);
     403}
     404
     405
     406//--------------------------------------------------------------------
     407  DeclareContensClass(BDat, BDatTemporary, BDatCINT_delete_variables);
     408  DefExtOpr(1, BDatCINT_delete_variables,
     409  "Cint.delete_variables", 1, 3,
     410  "Set Text Text",
     411  "(Set varNames [, Text prefix=\"\", Text suffix=\"\"] )",
     412  I2("Deletes  from CINT global scope the variables which names are in "
     413     "specified set varNames.\n"
     414     "Returns the number of deleted variables.\n",   
     415     "Borra del ámbito global de CINT las variables cuyos nombres se "
     416     "especifican en el conjunto varNames\n"
     417     "Devuelve el número total de variables realmente borradas.\n")+
     418  I2("If argument prefix is not empty then it will added before each TOL name to "
     419     "create transformed CINT names.\n",
     420     "Si el argumento prefix no es vacío se añadirá por delante de cada nombre TOL "
     421     "para crear los nombres CINT transformados.\n")+
     422  I2("If argument suffix is not empty then it will added after each TOL name to "
     423     "create transformed CINT names.\n",
     424     "Si el argumento suffix no es vacío se añadirá por detrás de cada nombre TOL "
     425     "para crear los nombres CINT transformados.\n") +
     426     cint_url_link(),
     427  BOperClassify::Conversion_);
     428  void BDatCINT_delete_variables::CalcContens()
     429//--------------------------------------------------------------------
     430{
     431  if(CheckNonDeclarativeAction("Cint.delete_variables")) { return; }
     432 
     433  BSyntaxObject* var = NULL;
     434  BGrammar* gra = NULL;
     435  int numArg;
     436  contens_ = 0;
     437  BSet& arg = Set(Arg(1));
     438  BText prefix_ = "";
     439  BText sufix_ = "";
     440  if(Arg(2)) { prefix_ = Text(Arg(2)); }
     441  if(Arg(3)) { sufix_ = Text(Arg(3)); }
     442
     443  for(numArg = 1; numArg<=arg.Card(); numArg++)
     444  {
     445    var = arg[numArg];
     446    if(!var) { continue; }
     447    gra = var->Grammar();
     448    if(gra==GraText())
     449    {
     450      BText pns = prefix_+var->Name()+sufix_;
     451      contens_ += G__deletevariable(pns);
     452    }
     453  }
     454}
     455
Note: See TracChangeset for help on using the changeset viewer.