Structures and routines

Examples are from under OS X. If the code here wants to be copy/pasted to the World prompt, turn auto-brackets off with Ctrl-A in World.

The libc routine gettimeofday returns the current calendar time as the elapsed time since the epoch. It takes pointers to two structures as arguments and fill those structures with data.

First libc needs to be loaded:

    w> libc: load/library %/usr/lib/libc.dylib

Then the structures and routine are defined:

    timeval: struct [
        slong sec
        sint32 usec
    ] none

    timezone: struct [
        sint minuteswest
        sint dsttime
    ] none

    gettimeofday: routine [
        [typecheck]
        libc "gettimeofday" [
            tp [struct!] pointer
            tzp [struct!] pointer
        ]
        sint
    ]

gettimeofday can now be called:

    w> gettimeofday timeval timezone
    == 0


timeval and timezone then holds the data. The routine localtime_r takes time and a struct tm as arguments and fill the structure with data:

    tm: struct [
        sint sec
        sint min
        sint hour
        sint mday
        sint mon
        sint year
        sint wday
        sint yday
        sint isdst
        slong gmtoff
        pointer zone
    ] none

    localtime-r: routine [
        [typecheck]
        libc "localtime_r" [
            time [struct!] pointer
            resultp [struct!] pointer
        ]
        pointer handle!
    ]

The time argument is a pointer to a slong holding the seconds. But that's the first variable in the timeval structure, so we can just pass that:

    w> localtime-r timeval tm

Now tm is filled with data, and we can e.g. get the day of the year:

    w> tm/yday
    == 353


There is some freedom in defining structures. C datatype and argument name can be exchanged. It's also possible to give initial values, so these two are the same:

    struct [float f] [1.0]
    struct [f float] [1.0]

Now find some more interesting libraries to integrate with!

7 comments:

  1. Integrates with OpenCOBOL runtime and compile shared modules.

    ReplyDelete
  2. That's cool, Brian! I would like World to integrate with lots of other technologies, both World calling others, and others calling into World. The former is possible for example with these routines, the latter is yet to come.

    ReplyDelete
  3. Wow. I had played a bit with Red, but this is also amazing. Just playing with parse and seems very complete.
    I was getting worried about the state of rebol and now there are two excellent projects to take the ideas forward. Keep up the great work and thank you :-)

    ReplyDelete
  4. Very nice. This will help make World a great glue language.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. do you continue developing world? I like it but haven't seen a post for a long time

    ReplyDelete
  7. Yes, World is still being developed, but as I finance the development myself, I had to take time away from the project. A new version is available, so use the "Get World" link at the top to get this. I may put some new blog posts up soon.

    ReplyDelete