• Examples for gforth's objects.fs

    From okflo@okflo@teletyp.ist to comp.lang.forth on Fri Oct 3 16:44:12 2025
    From Newsgroup: comp.lang.forth

    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    Any recommendations of f.e. a bigger project, thas has public code?

    many thanks - okflo
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Hans Bezemer@the.beez.speaks@gmail.com to comp.lang.forth on Fri Oct 3 17:11:49 2025
    From Newsgroup: comp.lang.forth

    On 03-10-2025 16:44, okflo@teletyp.ist wrote:

    Well, it isn't gForth or object.fs, but there are a lot of examples to
    be found here (foos*.4pp):
    https://sourceforge.net/p/forth-4th/code/HEAD/tree/trunk/4th.src/4pp/

    Most of 'em cover design patterns (all of them as a matter of fact):
    https://en.wikipedia.org/wiki/Design_Patterns

    There is an article on FOOS OOP here:
    https://sourceforge.net/p/forth-4th/wiki/Object%20Orientation/

    It's got all the normal OOP features:
    - Encapsulation;
    - Subtype polymorphism;
    - Single Inheritance;
    - Lazy initialization;
    - Constructors and destructors.

    Hans Bezemer

    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    Any recommendations of f.e. a bigger project, thas has public code?

    many thanks - okflo

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Fri Oct 3 21:15:05 2025
    From Newsgroup: comp.lang.forth

    okflo@teletyp.ist writes:
    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    Any recommendations of f.e. a bigger project, thas has public code?

    For objects.fs, I have hear from a few users a long time ago, but have
    nothing to point to.

    Gerry Jackson has adopted and extended mini-oof for his lexgen <https://github.com/gerryjackson/Forth-LexGen> and lexex scanner
    generator tools, and for grace, his parser generator.

    Bernd Paysan is using mini-oof2 heavily in his net2o (see the bottom
    of <https://fossil.net2o.de/net2o/doc/trunk/wiki/get-it.md> about how
    to get source code).

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html
    EuroForth 2025 registration: https://euro.theforth.net/
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Fri Oct 3 20:21:32 2025
    From Newsgroup: comp.lang.forth

    On 10/3/25 9:44 AM, okflo@teletyp.ist wrote:
    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    Any recommendations of f.e. a bigger project, thas has public code?

    many thanks - okflo

    Here, you will find an example of object-oriented code in Forth, written
    for Bernd Paysan's mini-oof.4th.

    https://github.com/mynenik/kForth-64/blob/master/forth-src/qm/epr-sim.4th

    Some definitions of classes and inheritance are shown below -- I expect
    these may be easily translatable to the object.fs declarations.

    Cheers,
    Krishna

    --

    \ Quantum Two-Particle State class and its methods

    object class
    complex var C1 \ amplitude of |11> component
    complex var C2 \ amplitude of |10> component
    complex var C3 \ " |01> component
    complex var C4 \ " |00> component
    method init-2p2s ( o -- ) ( F: z1 z2 z3 z4 -- )
    method normalize ( o -- )
    method exchange ( o -- ) \ exchange particle labels
    method P_up ( o -- ) ( F: stheta ctheta -- P_up )
    method M_up ( o -- ) ( F: stheta ctheta -- C1' C2' C3' C4' )
    method M_down ( o -- ) ( F: stheta ctheta -- C1' C2' C3' C4' ) end-class Q2p2s \ two-particle, bipartite quantum state


    \ Text Graphic class and its methods

    object class
    2 cells var topLeft \ col row
    2 cells var dims \ columns rows
    2 cells var colors \ foreground and background colors
    4 cells var auxColors \ auxiliary colors
    method init ( ... o -- )
    method get-height ( o -- w )
    method get-width ( o -- h )
    method set-fb-colors ( o -- )
    method fill-area ( color o -- )
    method clear-area ( o -- )
    method draw ( ... o -- )
    end-class text-graphic

    \ Three-angle selector widget class

    text-graphic class
    cell var pos \ selector position
    3 dfloats var theta[ \ angles (for pos=1 to 3)
    3 dfloats var ctheta[ \ cos(theta_i)
    3 dfloats var stheta[ \ sin(theta_i)
    method assign-angles ( o -- ) ( F: deg1 deg2 deg3 -- )
    method get-current-angle ( o -- ) ( F: -- deg )
    method get-sincos ( o -- ) ( F: sintheta costheta )
    end-class selector3A


    \ Two-light indicator widget class

    text-graphic class
    cell var stateBits \ bits 0 and 1 indicate status for each light
    method set-lights ( bits o -- )
    end-class indicator2L


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Gerry Jackson@do-not-use@swldwa.uk to comp.lang.forth on Sat Oct 4 09:05:22 2025
    From Newsgroup: comp.lang.forth

    On 03/10/2025 22:15, Anton Ertl wrote:
    okflo@teletyp.ist writes:
    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    Any recommendations of f.e. a bigger project, thas has public code?

    For objects.fs, I have hear from a few users a long time ago, but have nothing to point to.

    Gerry Jackson has adopted and extended mini-oof for his lexgen <https://github.com/gerryjackson/Forth-LexGen> and lexex scanner
    generator tools, and for grace, his parser generator.

    If you look at this I recommend that you look at the file
    src/syntaxtree.fth as well as lib/xmini_oof.fth.

    I would suggest that you start with mini-oof & xmini_oof to see how they
    can be used as these are much simpler than Anton's object.fs which is a
    more complete OO system. Once you see how mini_oof can be used in LexGen and/or Krishna's epr-sim.4th you can move on to object.fs.


    Bernd Paysan is using mini-oof2 heavily in his net2o (see the bottom
    of <https://fossil.net2o.de/net2o/doc/trunk/wiki/get-it.md> about how
    to get source code).

    - anton
    --
    Gerry
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From albert@albert@spenarnc.xs4all.nl to comp.lang.forth on Sat Oct 4 15:07:51 2025
    From Newsgroup: comp.lang.forth

    In article <877bxcm4eb.fsf@news.teletyp.ist>, <okflo@teletyp.ist> wrote:
    Hi,

    I am currently trying to digg deeper into gforth's object.fs.

    For that I would like to read some good object oriented forth code
    (preferred gforth's object.fs, but open to other good examples).

    I have made an ansified copy of Mark's Probst gforth lisp, but I have
    also remade it using objects:

    https://github.com/albertvanderhorst/forthlisp/blob/master/advanced
    See the part lisp.frt
    (lispl.frt is a remake of the interpreter. Forth is its own
    interpreter, don't *write* an interpreter in Forth. Ignore this
    if you're interested in objects only.)

    Any recommendations of f.e. a bigger project, thas has public code?

    For an advanced project (reverse engineering system) see //github.com/albertvanderhorst/ciasdis

    [ lina530 is a Forth system, with interspersed data and code.
    ciasdis , with the help of Forth scripting, generates a source
    lina530.asm from the executable with all the labels recovered from
    the Forth headers. Then `` ciasdis -a '' recovers lina530
    byte for byte. Or you could insert a buffer anywhere, and the
    result is a working Forth, moving data to different addresses. ]

    This is the same system that recreates colorforth from its
    binary, scoffed by Jeff Fox.



    many thanks - okflo

    Groetjes Albert
    --
    The Chinese government is satisfied with its military superiority over USA.
    The next 5 year plan has as primary goal to advance life expectancy
    over 80 years, like Western Europe.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sat Oct 4 16:53:51 2025
    From Newsgroup: comp.lang.forth

    Gerry Jackson <do-not-use@swldwa.uk> writes:
    I would suggest that you start with mini-oof & xmini_oof to see how they
    can be used as these are much simpler than Anton's object.fs which is a
    more complete OO system. Once you see how mini_oof can be used in LexGen >and/or Krishna's epr-sim.4th you can move on to object.fs.

    But given that so many people found mini-oof good enough to work with
    it, or to base their own extension on it, maybe the "more complete"
    part of object.fs as actually not needed that often.

    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object. He added it into (the Gforth-specific) mini-oof2. Interestingly, mini-oof is based on the internal OO system
    of Gray <https://www.complang.tuwien.ac.at/forth/gray.zip>, but that
    system has a THIS. So the removal of THIS in mini-oof probably was
    one simplification too far.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html
    EuroForth 2025 registration: https://euro.theforth.net/
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Sat Oct 4 17:04:18 2025
    From Newsgroup: comp.lang.forth

    On 10/4/25 11:53 AM, Anton Ertl wrote:
    Gerry Jackson <do-not-use@swldwa.uk> writes:
    I would suggest that you start with mini-oof & xmini_oof to see how they
    can be used as these are much simpler than Anton's object.fs which is a
    more complete OO system. Once you see how mini_oof can be used in LexGen
    and/or Krishna's epr-sim.4th you can move on to object.fs.

    But given that so many people found mini-oof good enough to work with
    it, or to base their own extension on it, maybe the "more complete"
    part of object.fs as actually not needed that often.

    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object.

    We needed "this" for use in our code at work. In those instances where
    it was needed, it was simple enough to implement and use within the
    framework of mini-oof.fs in the following manner:

    <x> class \ define a class derived from class x
    ... \ specify vars and members
    end-class <y>

    For every class member of <y> which needs "this", define the member as follows,

    0 ptr this \ ptr is synonymous with VALUE in ANS Forth
    :noname ( o -- )
    to this
    ...
    this V1 @ \ V1 is a var which belongs to the class for object o
    ...
    <y> defines <member-name>

    Each member saves the object pointer passed to it on the stack and
    allows "this" to reference the vars or members of that object.


    --
    Krishna


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Krishna Myneni@krishna.myneni@ccreweb.org to comp.lang.forth on Sat Oct 4 20:11:04 2025
    From Newsgroup: comp.lang.forth

    On 10/4/25 5:04 PM, Krishna Myneni wrote:
    ...
    For every class member of <y> which needs "this", define the member as follows,

    0 ptr this-a-a \ ptr is synonymous with VALUE in ANS Forth
    :noname ( o -- )
    -a-a to this
    -a-a ...
    -a-a this V1 @-a-a \ V1 is a var which belongs to the class for object o
    -a-a ...
    <y> defines <member-name>
    ...

    An error in the above code is fixed below:

    0 ptr this \ ptr is synonymous with VALUE in ANS Forth
    :noname ( o -- )
    to this
    ...
    this V1 @ \ V1 is a var which belongs to the class for object o
    ...
    ; \ close the noname definition, leave xt on the stack
    <y> defines <member-name>

    --
    KM

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Gerry Jackson@do-not-use@swldwa.uk to comp.lang.forth on Sun Oct 5 11:54:48 2025
    From Newsgroup: comp.lang.forth

    On 04/10/2025 17:53, Anton Ertl wrote:
    Gerry Jackson <do-not-use@swldwa.uk> writes:
    I would suggest that you start with mini-oof & xmini_oof to see how they
    can be used as these are much simpler than Anton's object.fs which is a
    more complete OO system. Once you see how mini_oof can be used in LexGen
    and/or Krishna's epr-sim.4th you can move on to object.fs.

    But given that so many people found mini-oof good enough to work with
    it, or to base their own extension on it, maybe the "more complete"
    part of object.fs as actually not needed that often.

    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object. He added it into (the Gforth-specific) mini-oof2. Interestingly, mini-oof is based on the internal OO system
    of Gray <https://www.complang.tuwien.ac.at/forth/gray.zip>, but that
    system has a THIS. So the removal of THIS in mini-oof probably was
    one simplification too far.

    - anton

    I agree about THIS, if I'd thought of it I would probably have added it.
    Also if mini-oof2 had been available at the time I would have used that.

    I suppose mini-oof is a good example of the Forth philosophy: a few
    lines of code giving the basics of OO yet powerful, easily extendable to
    add more features, easy to understand (and abuse if you need to!). And
    like Forth itself a careless mistake can easily crash the system.
    --
    Gerry
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sun Oct 5 11:19:26 2025
    From Newsgroup: comp.lang.forth

    Krishna Myneni <krishna.myneni@ccreweb.org> writes:
    On 10/4/25 11:53 AM, Anton Ertl wrote:
    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object.

    We needed "this" for use in our code at work. In those instances where
    it was needed, it was simple enough to implement and use within the >framework of mini-oof.fs in the following manner:

    <x> class \ define a class derived from class x
    ... \ specify vars and members
    end-class <y>

    For every class member of <y> which needs "this", define the member as >follows,

    0 ptr this \ ptr is synonymous with VALUE in ANS Forth
    :noname ( o -- )
    to this
    ...
    this V1 @ \ V1 is a var which belongs to the class for object o
    ...
    ; \ close the noname definition, leave xt on the stack
    <y> defines <member-name>

    [I inserted the line that you added in <10bsgj8$2srh2$1@dont-email.me>]

    This code does not save THIS on entering the word and restore it on
    exit. Which plays a role if you call another method with a different
    object of the same class (e.g., when your object is a tree node, and
    you want to do something for both child nodes). And once you save and
    restore THIS, a per-class THIS does not buy you anything.

    The saving and restoring of THIS is what M: and ;M do in objects.fs.
    So that's the way I prefer to deal with THIS, and it means that I
    prefer to pass the object for which a method is to be called on the
    data stack.

    By contrast, Bernd Paysan prefers to manipulate the current object
    explicitly and to implicitly pass the object for which a method should
    be called through the current object, as exemplified in mini-oof2. So
    you write code as follows:

    mini-oof2 objects.fs
    invoke O.M, with O in THIS already: M O M
    invoke O.M, with O not yet in THIS: O >o M o> O M
    define a method implementation: :noname ... ; M: ... ;M
    access THIS unneeded THIS

    Note that O> is not like R>, but like RDROP.

    Concerning implementation, one (or several) global values THIS only
    work in single-threaded programs. For multi-threaded programs, you
    need to use a UVALUE (i.e., a thread-local value), or use a different
    approach. A simple approach that you can use in mini-oof is to define
    THIS as a local; your example above becomes:

    :noname {: par1 par2 this -- ... :}
    ...
    this v1 @
    ...
    ;
    <y> defines <method-selector>

    Another alternative is to have special support for the current object
    in the Forth engine, which is what mini-oof2 is doing for >O and O>;
    but that means that the Forth engine also needs to know about method
    calls and instance variable accesses (they also work with the current
    object and access it internally).

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html
    EuroForth 2025 registration: https://euro.theforth.net/
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From anton@anton@mips.complang.tuwien.ac.at (Anton Ertl) to comp.lang.forth on Sun Oct 5 11:56:44 2025
    From Newsgroup: comp.lang.forth

    Gerry Jackson <do-not-use@swldwa.uk> writes:
    I suppose mini-oof is a good example of the Forth philosophy: a few
    lines of code giving the basics of OO yet powerful, easily extendable to
    add more features, easy to understand (and abuse if you need to!).

    Yes.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html
    EuroForth 2025 registration: https://euro.theforth.net/
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Hans Bezemer@the.beez.speaks@gmail.com to comp.lang.forth on Sun Oct 5 14:31:50 2025
    From Newsgroup: comp.lang.forth

    On 05-10-2025 12:54, Gerry Jackson wrote:
    I suppose mini-oof is a good example of the Forth philosophy: a few
    lines of code giving the basics of OO yet powerful, easily extendable to
    add more features, easy to understand (and abuse if you need to!). And
    like Forth itself a careless mistake can easily crash the system.

    More than once I've listed mini-oof as a shining example of brilliant
    Forth code (no kidding - I'm serious!).

    As far as the "crashing" is concerned - yes, the fun part of OOP Forth
    is that in fact very little - if any - checking is done.

    The real fun comes when OOP proponents list those checks as a proof of
    the superiority of the OOP concept. They implicitly assume that if you
    feature the characteristics of OOP that the enforcement of those rules
    follows automatically.

    Which is not true of course. At least not in my version of Forth OOP,
    FOOS. To me it proves how twisted their mindset actually is.

    Hans Bezemer

    On 04/10/2025 17:53, Anton Ertl wrote:
    Gerry Jackson <do-not-use@swldwa.uk> writes:
    I would suggest that you start with mini-oof & xmini_oof to see how they >>> can be used as these are much simpler than Anton's object.fs which is a
    more complete OO system. Once you see how mini_oof can be used in LexGen >>> and/or Krishna's epr-sim.4th you can move on to object.fs.

    But given that so many people found mini-oof good enough to work with
    it, or to base their own extension on it, maybe the "more complete"
    part of object.fs as actually not needed that often.

    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object.-a He added it into (the Gforth-specific)
    mini-oof2.-a Interestingly, mini-oof is based on the internal OO system
    of Gray <https://www.complang.tuwien.ac.at/forth/gray.zip>, but that
    system has a THIS.-a So the removal of THIS in mini-oof probably was
    one simplification too far.

    - anton

    I agree about THIS, if I'd thought of it I would probably have added it. Also if mini-oof2 had been available at the time I would have used that.

    I suppose mini-oof is a good example of the Forth philosophy: a few
    lines of code giving the basics of OO yet powerful, easily extendable to
    add more features, easy to understand (and abuse if you need to!). And
    like Forth itself a careless mistake can easily crash the system.


    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From albert@albert@spenarnc.xs4all.nl to comp.lang.forth on Mon Oct 6 10:46:09 2025
    From Newsgroup: comp.lang.forth

    In article <2025Oct5.131926@mips.complang.tuwien.ac.at>,
    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
    Krishna Myneni <krishna.myneni@ccreweb.org> writes:
    On 10/4/25 11:53 AM, Anton Ertl wrote:
    One thing that Bernd Paysan found missing in mini-oof is THIS, i.e., a
    handle for the current object.

    We needed "this" for use in our code at work. In those instances where
    it was needed, it was simple enough to implement and use within the >>framework of mini-oof.fs in the following manner:

    <x> class \ define a class derived from class x
    ... \ specify vars and members
    end-class <y>

    For every class member of <y> which needs "this", define the member as >>follows,

    0 ptr this \ ptr is synonymous with VALUE in ANS Forth
    :noname ( o -- )
    to this
    ...
    this V1 @ \ V1 is a var which belongs to the class for object o
    ...
    ; \ close the noname definition, leave xt on the stack
    <y> defines <member-name>

    [I inserted the line that you added in <10bsgj8$2srh2$1@dont-email.me>]

    This code does not save THIS on entering the word and restore it on
    exit. Which plays a role if you call another method with a different
    object of the same class (e.g., when your object is a tree node, and
    you want to do something for both child nodes). And once you save and >restore THIS, a per-class THIS does not buy you anything.

    The saving and restoring of THIS is what M: and ;M do in objects.fs.
    So that's the way I prefer to deal with THIS, and it means that I
    prefer to pass the object for which a method is to be called on the
    data stack.

    By contrast, Bernd Paysan prefers to manipulate the current object
    explicitly and to implicitly pass the object for which a method should
    be called through the current object, as exemplified in mini-oof2. So
    you write code as follows:

    mini-oof2 objects.fs
    invoke O.M, with O in THIS already: M O M
    invoke O.M, with O not yet in THIS: O >o M o> O M
    define a method implementation: :noname ... ; M: ... ;M
    access THIS unneeded THIS

    Note that O> is not like R>, but like RDROP.

    Concerning implementation, one (or several) global values THIS only
    work in single-threaded programs. For multi-threaded programs, you
    need to use a UVALUE (i.e., a thread-local value), or use a different >approach. A simple approach that you can use in mini-oof is to define
    THIS as a local; your example above becomes:

    :noname {: par1 par2 this -- ... :}
    ...
    this v1 @
    ...
    ;
    <y> defines <method-selector>

    Another alternative is to have special support for the current object
    in the Forth engine, which is what mini-oof2 is doing for >O and O>;
    but that means that the Forth engine also needs to know about method
    calls and instance variable accesses (they also work with the current
    object and access it internally).

    What is the big deal?
    I have M: .. M; normally working on a current object.

    Max Probst's lisp works with objects 1] in the stack,
    so I added

    : M:r BLD} HERE DP-MARKER @ - >R SWAP-DP : R> "%d +" FORMAT&EVAL ;

    1] Actually he used structs.

    - anton

    Groetjes Albert
    --
    The Chinese government is satisfied with its military superiority over USA.
    The next 5 year plan has as primary goal to advance life expectancy
    over 80 years, like Western Europe.
    --- Synchronet 3.21a-Linux NewsLink 1.2