darcsden :: alex -> the -> blob

the programming language

root / examples / ping-pong.the

-- starts a match of n volleys
volley: (n: Integer) = {
    -- start up the ponger
    proc = { notify |
        pong: notify
    } spawn: [self]

    -- start up the pinger
    { ping: n ponger: proc } spawn
} do

pong: notify =
    receive match: {
        -- volley is done finish
        @finished -> {
            "         | ." write
            "volley finished" write
            notify ! @done
        } do

        -- got a ping, send a pong
        @(ping: pid) -> {
            "       . |" write
            pid ! @pong
            pong: notify -- loop
        } do
    }

ping: 0 ponger: pid = pid ! @finished
ping: n ponger: pid = {
    pid ! @(ping: self)

    -- got a pong send a ping
    receive match: {
        @pong -> {
            "|  .      " write
            ping: (n - 1) ponger: pid
        } do
    }
} do

-- start up the match, doing 5 volleys
volley: 5

-- wait for the match to complete
receive print