\ -*- mode: forth -*-
\ $Id: objinst.gforth,v 1.2 2001/06/25 00:22:55 doug Exp $
\ http://www.bagley.org/~doug/shootout/

\ from Anton Ertl:
\ I'm using objects.fs here, code using one of the other OO Forth
\ extensions will look different.

warnings off \ don't complain about redefining catch, state, value

0. argc @ 1- arg >number 2drop drop constant NUM

require objects.fs

object class
    selector activate 
    selector value 
    cell% inst-var state

    m: 
    state ! ;m
    overrides construct
    
    m: 
    state @ ;m
    overrides value
    
    m: 
    state @ invert state !
    this ;m
    overrides activate

end-class Toggle

Toggle class
    cell% inst-var count-max
    cell% inst-var counter

    m: 
    this [parent] construct
    count-max !
    0 counter ! ;m
    overrides construct

    m: 
    1 counter +!
    counter @ count-max @ >= if
        state @ invert state !
        0 counter !
    endif
    this ;m
    overrides activate
    
end-class NthToggle

: flag. 
    if ." true" else ." false" endif cr ;

: mainloop  { class n }
    true class heap-new true n 0 ?do
    drop dup activate value dup flag.
    loop
    drop ;

: main 
    Toggle 5 mainloop
    NUM 0 ?do
    true Toggle heap-new free drop \ like the C version
    loop
    cr
    3 NthToggle 8 mainloop
    NUM 0 ?do
    3 true NthToggle heap-new free drop \ like the C version
    loop ;

main bye