Re: [Ficl-developers] poping values off the vm stack?
Brought to you by:
jsadler
From: Bob D. <bo...@dr...> - 2001-11-30 03:46:11
|
11/29/2001 9:08:51 PM, Michel Pelletier <mi...@zo...> wrote: >The current fun little thing I"m working on is embedding ficl in >Python. How very cool. Will we soon be able to do ficl scripting in Zope? Can you send me a copy of your stuff when it gets close to working? > I have it working pretty well at the moment, I can create vms >and evalute python strings containing forth code, and I can also push >integers onto the stack from python, but I can't figure out how to pop >them. That's my question. I have these two C functions: >static PyObject * >FiclVM_pushint(FiclVMObject *self, PyObject *args) >{ > int x; > if (!PyArg_ParseTuple(args, "i:pushInt", &x)) > return NULL; > vmPush(self->ficl_vm, LVALUEtoCELL(x)); > Py_INCREF(Py_None); > return Py_None; >} The only thing I notice right off the bat is that LVALUEtoCELL expects at least a long, and you're handing it an int. This will of course only work if int and long are the same length on your platform. Wouldn't declaring x a long make this clearer? Perhaps using unambiguous types such as int32_t, or more directly-relevent types such as FICL_INT, would be better? >static PyObject * >FiclVM_popint(FiclVMObject *self, PyObject *args) >{ > CELL c; > PyObject *i; > if (!PyArg_ParseTuple(args, ":pushInt")) > return NULL; > c = vmPop(self->ficl_vm); > i = PyInt_FromLong((long)&c); > Py_INCREF(i); > return i; >} I don't think that you want to take the address of c here :-) Quoting http://www.python.org/doc/current/api/intObjects.html : PyObject* PyInt_FromLong(long ival) : Return value: New reference. : Creates a new integer object with a value of ival. Perhaps you meant &c->i ? In any event, unless sizeof(long) = sizeof(float) = sizeof(void *) = sizeof(void (*)(void)) on your platform, beware of alignment issues if you just use the cell union without a member reference. Also, if the int type is already a long on your system, I'd personally rather see just i = PyInt_FromLong(c.i); But now I'm probably just being picky. >The first function works fine and pushes the right integer onto the >Forth stack. The second function does return a valid python integer but >when I push '5' I get '-1758473533' or whatever back. Yes, addresses can be quite large numbers... :-) > How can I turn >'c' into the right integer value? PyInt_FromLong expects a long value. Feed it the value rather than the address? :-) >Thanks, No problem. >-Michel > >_______________________________________________ >Ficl-developers mailing list >Fic...@li... >https://lists.sourceforge.net/lists/listinfo/ficl-developers > > |