View Full Version : varargs in spin?
sssidney
11-18-2011, 05:21 PM
Hi,
Is there a way to implement, or does it already exist, a C varargs type functionality (variable number of function args) in spin?
Thanks,
Dave
Phil Pilgrim (PhiPi)
11-18-2011, 05:22 PM
No, at least not directly. You could pass the address of an array, though, in which the first element gives the number of arguments to follow. Unfortunately, except for the string pragma, Spin does not have an inline array constructor, either, so you will have to set up the array before the method call.
-Phil
jazzed
11-18-2011, 05:54 PM
I've used object.someMethod(object.p(a) + object.p(b) + object.p(c)) ... a,b,c values are stored an object array by index with "p()" and processed by someMethod.
Phil Pilgrim (PhiPi)
11-18-2011, 06:03 PM
And then someMethod resets the pointer for the next call, right? 'Pretty dang clever, jazzed!
-Phil
Phil Pilgrim (PhiPi)
11-18-2011, 06:16 PM
I was so inspired by jazzed's idea, I had to write a sample program:
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
OBJ
pst : "Parallax Serial Terminal"
VAR
long paramlist[20]
PUB start
pst.start(38400)
method(p(100) & p(-4) & p(32))
method(p(5000))
PUB method(params) | i
pst.str(string("This call has "))
pst.dec(long[params])
pst.str(string(" parameters: "))
repeat i from 1 to long[params]
pst.dec(long[params][i])
pst.char(" ")
pst.char(13)
long[params]~
PUB p(param)
paramlist[++paramlist[0]] := param
return @paramlist
-Phil
jazzed
11-18-2011, 06:28 PM
Nice code Phil.
I've implemented a formatted print like that.
Of course it had a format string:
print("Iteration: %d %x\n", p(n) + p(n))
sssidney
11-18-2011, 07:15 PM
Nice! Thanks. I think this will work for me.