Re: [Ficl-developers] poping values off the vm stack?
Brought to you by:
jsadler
From: Michel P. <mi...@zo...> - 2001-11-30 03:51:37
|
john sadler wrote: > > Michel - > > It looks like your cast is wrong. Try this: > c = vmPop(self->ficl_vm); > i = PyInt_FromLong(c.i); > Default promotion from int to long of the c.i parameter should keep the > compiler happy. Otherwise > try > i = PyInt_FromLong((long)(c.i)); > Your original code is casting the _address_ of c into a long, so you get > insane results. That works great! > HTH, and thanks for using Ficl! NP, BTW, embedding Ficl in Python is cool! Check out this Python module that actually runs! """ import ficl from time import time vm = ficl.vm() vm.evaluate(''' : forth_test ( n -- ) 100 0 do 100 0 do 100 0 do i j + + loop loop loop ; ''') current = time() vm.pushInt(0) vm.evaluate('forth_test') elapsed = time() - current print "Value is %s and took %s" % (vm.popInt(), elapsed) current = time() total = 0 for x in range(100): for y in range(100): for z in range(100): total = total + y + z elapsed = time() - current print "Value is %s and took %s" % (total, elapsed) """ The result? The Ficl loop takes 1.68 seconds, the Python loop takes 5.39 seconds. Ficl might just turn out to be a nice "inline assembly language" for Python. Ficl is extremly easy to embed, I'm not really even a C programmer and I did this in about four hours. My next step is to create better Ficl->Python communication than just integers. I'd like to be able to exchange any basic python type and equivalent Ficl types. Which brings me to my next question. ;) The Ficl docs say that C structs can be wrapped with Ficl classes. This is exactly what I need, as all Python built-in types start out as C structs. I'd like to take a standard Python struct, like the definition for the 'int' type: typedef struct { PyObject_HEAD long ob_ival; } PyIntObject; and wrap it with a Ficl class that defines a pseduo-Python like interface to the struct so that I can pass Python objects into Ficl and work with them in Ficl, and then pass them back. Is there a primer somewhere on wrapping C structs? Thanks! -Michel > - John > > At 11/29/01 06:08 PM, you wrote: > >I never saw my last message come to the list, so for anyone who was > >thinking of answering it don't bother, I figured out my problem. If you > >didn't see it than don't worry about it. > > > >The current fun little thing I"m working on is embedding ficl in > >Python. 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; > >} > > > > > >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; > >} > > > >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. How can I turn > >'c' into the right integer value? PyInt_FromLong expects a long value. > > > >Thanks, > > > >-Michel > > > >_______________________________________________ > >Ficl-developers mailing list > >Fic...@li... > >https://lists.sourceforge.net/lists/listinfo/ficl-developers > > John Sadler - joh...@al... > T 650-595-4954 F 603-687-2885 C 415-271-6795 |