Shop OBEX P1 Docs P2 Docs Learn Events
varargs in spin? — Parallax Forums

varargs in spin?

sssidneysssidney Posts: 64
edited 2011-11-18 12:15 in Propeller 1
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

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-18 10:22
    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
  • jazzedjazzed Posts: 11,803
    edited 2011-11-18 10:54
    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)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-18 11:03
    And then someMethod resets the pointer for the next call, right? 'Pretty dang clever, jazzed!

    -Phil
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-18 11:16
    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
  • jazzedjazzed Posts: 11,803
    edited 2011-11-18 11:28
    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))
  • sssidneysssidney Posts: 64
    edited 2011-11-18 12:15
    Nice! Thanks. I think this will work for me.
Sign In or Register to comment.