Esempio n. 1
0
NODE *cnv_node_to_strnode(NODE *nd)
   {
   char s[MAX_NUMBER];

   if (nd == UNBOUND || aggregate(nd))
      {
      return (UNBOUND);
      }
   switch (nodetype(nd))
      {
       case STRING:
       case BACKSLASH_STRING:
       case VBAR_STRING:
           return (nd);
       case CASEOBJ:
           return strnode__caseobj(nd);
       case QUOTE:
           nd = valref(cnv_node_to_strnode(node__quote(nd)));
           nd = reref(nd, make_strnode(getstrptr(nd),
                 (char *) NULL, getstrlen(nd) + 1,
                 nodetype(nd), quote_strnzcpy));
           unref(nd);
           return (nd);
       case COLON:
           nd = valref(cnv_node_to_strnode(node__colon(nd)));
           nd = reref(nd, make_strnode(getstrptr(nd),
                 (char *) NULL, getstrlen(nd) + 1,
                 nodetype(nd), colon_strnzcpy));
           unref(nd);
           return (nd);
       case INT:
           sprintf(s, "%ld", getint(nd));
           return (make_strnode(s, (char *) NULL, (int) strlen(s),
                 STRING, strnzcpy));
       case FLOAT:
           sprintf(s, "%0.15g", getfloat(nd));
           return (make_strnode(s, (char *) NULL, (int) strlen(s),
                 STRING, strnzcpy));
      }

   /*NOTREACHED*/
   return (NIL);
   }
Esempio n. 2
0
void init()
   {
   //    extern long time();
   int i /* = 0 */;
   NODE *proc = NIL, *pname = NIL, *cnd = NIL;

   fill_reserve_tank();
   Unbound = newnode(PUNBOUND);

#ifdef bsd
   srandom((int) time((long *) NULL));
#else
   //    srand((int)time((long *)NULL));
   //    srand((int)12345);
#endif
#ifdef ecma
   for (i = 0; i < 128; i++)
      ecma_array[i] = i;
   for (i = 0; i < ecma_size; i++)
      ecma_array[special_chars[i]] = ecma_begin+i;
#endif

   //    logolib = getenv("LOGOLIB");
   //    if (logolib == NULL) logolib = libloc;
   //    editor = getenv("EDITOR");
   //    if (editor == NULL) editor = "jove";

   editor = "jove";
   editorname = strrchr(editor, (int) '/');
   editorname = (editorname ? editorname + 1 : editor);

   i = 0;
   while (prims[i].name) 
      {
      if (prims[i].priority == MACRO_PRIORITY)
         proc = reref(proc, newnode(MACRO));
      else if (prims[i].priority == TAIL_PRIORITY)
         proc = reref(proc, newnode(TAILFORM));
      else if ((prims[i].priority & ~4) == PREFIX_PRIORITY)
         proc = reref(proc, newnode(PRIM));/* incl. --                        */
      else
         proc = reref(proc, newnode(INFIX));
      if (prims[i].priority < PREFIX_PRIORITY)
         setprimpri(proc, PREFIX_PRIORITY);
      else
         setprimpri(proc, prims[i].priority);
      setprimfun(proc, (lexzer) prims[i].prim);
      setprimdflt(proc, prims[i].defargs);
      setprimmax(proc, prims[i].maxargs);
      setprimmin(proc, prims[i].minargs);
      pname = reref(pname, make_static_strnode(prims[i].name));
      cnd = reref(cnd, make_instance(pname, pname));
      setprocnode__caseobj(cnd, proc);
      if (nodetype(proc) == MACRO)
         setflag__caseobj(cnd, PROC_MACRO);
      i++; 
      }
   deref(proc);
   deref(cnd);
   deref(pname);
   Truex = intern(make_static_strnode("true"));
   Falsex = intern(make_static_strnode("false"));
   Left_Paren = intern(make_static_strnode("("));
   Right_Paren = intern(make_static_strnode(")"));
   Minus_Sign = intern(make_static_strnode("-"));
   Minus_Tight = intern(make_static_strnode("--"));
   Query = intern(make_static_strnode("?"));
   Null_Word = intern(make_static_strnode("\0"));
   To = intern(make_static_strnode("to"));
   Macro = intern(make_static_strnode(".macro"));
   Toplevel = intern(make_static_strnode("toplevel"));
   System = intern(make_static_strnode("system"));
   Error = intern(make_static_strnode("error"));
   End = intern(make_static_strnode("end"));
   If = intern(make_static_strnode("if"));
   Ifelse = intern(make_static_strnode("ifelse"));
   Redefp = intern(make_static_strnode("redefp"));
   Caseignoredp = intern(make_static_strnode("caseignoredp"));
   setvalnode__caseobj(Caseignoredp, Truex);
   setflag__caseobj(Caseignoredp, VAL_BURIED);
   Erract = intern(make_static_strnode("erract"));
   Printdepthlimit = intern(make_static_strnode("printdepthlimit"));
   Printwidthlimit = intern(make_static_strnode("printwidthlimit"));
   Pause = intern(make_static_strnode("pause"));
   Startup = intern(make_static_strnode("startup"));
   Output = intern(make_static_strnode("output"));
   Op = intern(make_static_strnode("op"));
   Stop = intern(make_static_strnode("stop"));
   Goto = intern(make_static_strnode("goto"));
   Tag = intern(make_static_strnode("Tag"));
   the_generation = valref(cons(NIL, NIL));
   Not_Enough_Node = valref(cons(NIL, NIL));

   }
/** Translate python list of tuples to Config
 *
 *  [(K,V)]   -> Config
 *    float   -> double
 *    str     -> string
 *    [[()]]    -> vector<Config>  (recurse)
 *    ndarray -> vector<double>
 *    TODO: [0.0]   -> vector<double>
 */
void List2Config(Config& ret, PyObject *list, unsigned depth)
{
    if(depth>3)
        throw std::runtime_error("too deep for Dict2Config");

    PyRef<> iter(PyObject_GetIter(list));
    while(true) {
        PyObject *item = PyIter_Next(iter.py());
        if(!item) break;
        PyRef<> itemref(item);
        const char *kname;
        PyObject *value;
        if(!PyArg_ParseTuple(item, "sO", &kname, &value))
            throw std::runtime_error("list item is not a tuple?");

        if(PyArray_Check(value)) { // array as vector<double>
            PyRef<> arr(PyArray_ContiguousFromAny(value, NPY_DOUBLE, 0, 2));
            double *buf = (double*)PyArray_DATA(arr.py());
            std::vector<double> temp(PyArray_SIZE(arr.py()));
            std::copy(buf, buf+temp.size(), temp.begin());

            ret.swap<std::vector<double> >(kname, temp);

        } else if(PyNumber_Check(value)) { // scalar as double
            PyRef<> dval(PyNumber_Float(value));
            double val = PyFloat_AsDouble(dval.py());
            ret.set<double>(kname, val);

        } else if(PyUnicode_Check(value) || (PY_MAJOR_VERSION < 3 && PyBytes_Check(value))) { // string
            PyRef<> valref(value, borrow());
            PyCString sval(valref);
            const char *val = sval.c_str();

            ret.set<std::string>(kname, val);

        } else if(PySequence_Check(value)) { // list of dict
            Py_ssize_t N = PySequence_Size(value);

            Config::vector_t output;
            output.reserve(N);

            for(Py_ssize_t i=0; i<N; i++) {
                PyRef<> elem(PySequence_GetItem(value, i));

                if(PyDict_Check(elem.py())) {
                    elem.reset(PyMapping_Items(elem.py()));
                }
                if(!PyList_Check(elem.py())) {
                    PyTypeObject *valuetype = (PyTypeObject*)PyObject_Type(elem.py());
                    throw std::invalid_argument(SB()<<"lists must contain only dict or list of tuples, not "<<valuetype->tp_name);
                }

                output.push_back(ret.new_scope());

                List2Config(output.back(), elem.py(), depth+1); // inheirt parent scope
            }

            ret.set<Config::vector_t>(kname, output);

        } else {
            PyTypeObject *valuetype = (PyTypeObject*)PyObject_Type(value);
            throw std::invalid_argument(SB()<<"Must be a dict, not "<<valuetype->tp_name);
        }
    }
}