• Standard way to define new words in a word

    From shtps@21:1/5 to All on Wed Aug 10 15:01:44 2022
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    One way seems to be with evaluate:

    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ;
    newword anotherword .s

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all possible?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@arcor.de@21:1/5 to shtps on Wed Aug 10 06:44:56 2022
    shtps schrieb am Mittwoch, 10. August 2022 um 15:02:08 UTC+2:
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    One way seems to be with evaluate:

    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ; newword anotherword .s

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all possible?

    Modern Forths support nonames within other words, called quotations. See http://www.forth200x.org/quotations.txt

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to shtps on Wed Aug 10 06:30:02 2022
    On Wednesday, August 10, 2022 at 9:02:08 AM UTC-4, shtps wrote:
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    One way seems to be with evaluate:

    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ; newword anotherword .s

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all possible?

    I don't believe the standard provides a way to do this but I am not
    an expert.

    I am curious about your application/need for this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to shtps on Wed Aug 10 13:55:42 2022
    shtps <shtps@eclipso.de> writes:
    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ; >newword anotherword .s

    I've tested this with gforth and it works, but is this standard?

    You have to call MYWORD before you can use NEWWORD and ANOTHERWORD,
    otherwise it does not work (not in Gforth, either). But apart from
    that it is standard. However, EVALUATE uses a lot of the environment
    when MYWORD is called, so I don't recommend using it for this purpose.
    See <http://www.complang.tuwien.ac.at/forth/why-evaluate-is-bad>.

    How
    would I do this without evaluate, but still be standard forth, if at all >possible?

    : myword
    : 1 postpone literal 2 postpone literal postpone ;
    : postpone + postpone ;
    ;
    myword newword anotherword
    newword anotherword .s

    Note that in this variant the names of the two words are provided at
    MYWORD run-time. If you want to provide them at compile-time, one way
    to do it is to use EXECUTE-PARSING (defined in standard Forth in <http://www.complang.tuwien.ac.at/forth/compat/execute-parsing.fs>,
    but present in Gforth out of the box):

    : myword
    s" newword" ['] : execute-parsing
    1 postpone literal 2 postpone literal postpone ;
    s" anotherword" ['] : execute-parsing postpone + postpone ;
    ;
    myword
    newword anotherword .s

    There's a shorthand for all the POSTPONEs (and LITERALs) in Gforth:

    : myword
    s" newword" ['] : execute-parsing ]] 1 2 ; [[
    s" anotherword" ['] : execute-parsing ]] + ; [[
    ;
    myword
    newword anotherword .s

    - 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 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From NN@21:1/5 to shtps on Wed Aug 10 08:23:53 2022
    On Wednesday, 10 August 2022 at 14:02:08 UTC+1, shtps wrote:
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    Forth is flat. You cannot nest words like you can nest functions in say Pascal.



    One way seems to be with evaluate:

    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ; newword anotherword .s

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all possible?

    : newword 1 2 ;
    : anotherword + ;
    : myword newword anotherword ;
    myword .s

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Brian Fox on Thu Aug 11 03:17:58 2022
    On 10/08/2022 23:30, Brian Fox wrote:

    I am curious about your application/need for this.

    Pretty sure I never has such thoughts when I was beginning forth.
    Must be the new generation :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From shtps@21:1/5 to Brian Fox on Wed Aug 10 20:44:58 2022
    On 8/10/22 15:30, Brian Fox wrote:
    I am curious about your application/need for this.

    I'm experimenting around with creating a game in Forth and I'm writing a
    domain specific language that generates the words I need.

    Normally, if I would just be writing a binding generator for some API
    for example, I would write a Forth program that generates the bindings
    and writes them to a file (Forth source code) and then include that in
    my application at the very top. However the code generated in this case
    is so specific to my game/application that simply generating the words
    in Forth itself is preferable, without having to deal with source code
    or files at all, and instead generate the words directly.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@arcor.de@21:1/5 to dxforth on Wed Aug 10 11:23:50 2022
    dxforth schrieb am Mittwoch, 10. August 2022 um 19:18:01 UTC+2:
    On 10/08/2022 23:30, Brian Fox wrote:

    I am curious about your application/need for this.
    Pretty sure I never has such thoughts when I was beginning forth.
    Must be the new generation :)

    In those antiquish days a closure was a closure. ;-)

    Now for those youngish ones a closure opens new ways of programming.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From shtps@21:1/5 to Anton Ertl on Wed Aug 10 20:40:10 2022
    On 8/10/22 15:55, Anton Ertl wrote:
    You have to call MYWORD before you can use NEWWORD and ANOTHERWORD,
    otherwise it does not work (not in Gforth, either).

    Sorry, yes, I have done that but forgot to write it here.

    However, EVALUATE uses a lot of the environment
    when MYWORD is called, so I don't recommend using it for this purpose.
    See <http://www.complang.tuwien.ac.at/forth/why-evaluate-is-bad>.

    It was not my intention to use the environment when MYWORD is called,
    but when it is compiled. I did not consider this and I'll keep this in
    mind when considering the use of EVALUATE in the future.

    If you want to provide them at compile-time, one way
    to do it is to use EXECUTE-PARSING (defined in standard Forth in <http://www.complang.tuwien.ac.at/forth/compat/execute-parsing.fs>,
    but present in Gforth out of the box):

    : myword
    s" newword" ['] : execute-parsing
    1 postpone literal 2 postpone literal postpone ;
    s" anotherword" ['] : execute-parsing postpone + postpone ;
    ;
    myword
    newword anotherword .s

    This is exactly what I was attempting to do but didn't know about EXECUTE-PARSING or how to put it all together.

    There's a shorthand for all the POSTPONEs (and LITERALs) in Gforth:

    : myword
    s" newword" ['] : execute-parsing ]] 1 2 ; [[
    s" anotherword" ['] : execute-parsing ]] + ; [[

    I like this syntax sugar. I assume I can use the files in the <https://www.complang.tuwien.ac.at/forth/compat/> directory for
    compatibility with standard Forth? And I see that macros.fs is for this.

    Thank you very much for taking the time to write up such elaborate explanations! It helps a lot!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to shtps on Wed Aug 10 21:20:40 2022
    shtps <shtps@eclipso.de> writes:
    On 8/10/22 15:55, Anton Ertl wrote:
    There's a shorthand for all the POSTPONEs (and LITERALs) in Gforth:

    : myword
    s" newword" ['] : execute-parsing ]] 1 2 ; [[
    s" anotherword" ['] : execute-parsing ]] + ; [[

    I like this syntax sugar. I assume I can use the files in the ><https://www.complang.tuwien.ac.at/forth/compat/> directory for
    compatibility with standard Forth?

    Yes. You can get them in one package as <http://www.complang.tuwien.ac.at/forth/compat.zip>

    And I see that macros.fs is for this.

    Yes, but depending on the Forth system you may have to write

    2 1 ]] literal literal [[

    instead of

    ]] 1 2 [[

    - 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 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to shtps on Wed Aug 10 18:08:08 2022
    On Wednesday, August 10, 2022 at 2:45:00 PM UTC-4, shtps wrote:
    On 8/10/22 15:30, Brian Fox wrote:
    I am curious about your application/need for this.
    I'm experimenting around with creating a game in Forth and I'm writing a domain specific language that generates the words I need.

    Normally, if I would just be writing a binding generator for some API
    for example, I would write a Forth program that generates the bindings
    and writes them to a file (Forth source code) and then include that in
    my application at the very top. However the code generated in this case
    is so specific to my game/application that simply generating the words
    in Forth itself is preferable, without having to deal with source code
    or files at all, and instead generate the words directly.

    So I guess the DSL is dynamic inside the game? (makes my old head spin) Perhaps the player defines the meanings of words as they play?

    I am with DxForth. I have never thought about this kind of thing.
    Perhaps a dumb question but would DEFER words do this job?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to minf...@arcor.de on Thu Aug 11 14:52:53 2022
    On 11/08/2022 04:23, minf...@arcor.de wrote:
    dxforth schrieb am Mittwoch, 10. August 2022 um 19:18:01 UTC+2:
    On 10/08/2022 23:30, Brian Fox wrote:

    I am curious about your application/need for this.
    Pretty sure I never has such thoughts when I was beginning forth.
    Must be the new generation :)

    In those antiquish days a closure was a closure. ;-)

    Now for those youngish ones a closure opens new ways of programming.

    It may be youngish ones are simply accepting of what older ones are
    offering as solutions - instead of examining it for themselves, which
    takes effort and discrimination.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From shtps@21:1/5 to Brian Fox on Sun Aug 14 12:39:00 2022
    On 8/11/22 03:08, Brian Fox wrote:
    So I guess the DSL is dynamic inside the game? (makes my old head spin) Perhaps the player defines the meanings of words as they play?

    The idea is to create a simple way to define higher level concepts for
    the engine. For my game I'm going to be using an entity component
    system, as I already have experience with writing games in it and which
    gives the game a lot of flexibility, but it also comes with the
    necessary "bookkeeping".

    Writing games takes a lot of time if you write everything from scratch,
    so I'm currently just experimenting because I don't have the time needed
    to devote myself to writing a game to completion.

    I am with DxForth. I have never thought about this kind of thing.
    Perhaps a dumb question but would DEFER words do this job?

    In my case I could do the same with structs or something similar to it,
    or even just aliasing really. It isn't even necessary to generate the
    words in my case, I probably wont stick with it.

    I have a C# background so maybe that's where I got the idea from: <https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to shtps on Sun Aug 14 07:27:15 2022
    On Sunday, August 14, 2022 at 6:39:03 AM UTC-4, shtps wrote:
    On 8/11/22 03:08, Brian Fox wrote:
    So I guess the DSL is dynamic inside the game? (makes my old head spin) Perhaps the player defines the meanings of words as they play?
    The idea is to create a simple way to define higher level concepts for
    the engine. For my game I'm going to be using an entity component
    system, as I already have experience with writing games in it and which
    gives the game a lot of flexibility, but it also comes with the
    necessary "bookkeeping".

    Writing games takes a lot of time if you write everything from scratch,
    so I'm currently just experimenting because I don't have the time needed
    to devote myself to writing a game to completion.
    I am with DxForth. I have never thought about this kind of thing.
    Perhaps a dumb question but would DEFER words do this job?
    In my case I could do the same with structs or something similar to it,
    or even just aliasing really. It isn't even necessary to generate the
    words in my case, I probably wont stick with it.

    I have a C# background so maybe that's where I got the idea from: <https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview>

    I have not read the entire source generator article but I wonder if Forth already
    solves this because the interpreter is running while your are compiling source code.

    Forth can read source and interpret parts of it while compiling other parts.

    I may misunderstand this entirely but you may be trying to solve a problem that you
    don't need to solve. ? :-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to shtps@eclipso.de on Tue Aug 16 22:34:53 2022
    In article <td0a7o$9mh$1@gioia.aioe.org>, shtps <shtps@eclipso.de> wrote:
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    : aap ": hello 1234 . ; " EVALUATE ;

    aap
    OK
    hello
    1234 OK



    Note:
    If you have an old fashioned Forth without denotations (recognizers)
    you have to do
    : aap S" : hello 1234 . ; " EVALUATE ;

    If you are able to place double quotes in a string (as per ciforth)
    a more canonical example would be. (ciforth shows the most recently
    words last, contrary to most Forths)

    AMDX86 ciforth 5.4.0
    OK
    : aap ": hello ""hello cruel world!"" TYPE CR ; " EVALUATE ;
    OK
    WORDS
    ...
    BYE LIST INDEX .S ENVIRONMENT? .SIGNON TASK aap OK
    aap
    WORDS
    ...
    BYE LIST INDEX .S ENVIRONMENT? .SIGNON TASK aap hello OK hello
    hello cruel world!
    OK

    One way seems to be with evaluate:
    Right. The only game in town, IMO.

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all >possible?

    EVALUATE is in CORE. There are people who content that using it is
    not politically correct. Listen to them and you land in a snake pit
    full POSTPONE's and new features that are not guaranteed to work
    everywhere. Your choice.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to minf...@arcor.de on Thu Aug 18 13:20:38 2022
    In article <106cb333-5705-4378-8738-442dcae4ce7bn@googlegroups.com>, minf...@arcor.de <minforth@arcor.de> wrote:
    shtps schrieb am Mittwoch, 10. August 2022 um 15:02:08 UTC+2:
    Hi, I'm quite new to Forth and I'm hoping I'll find some help here with
    my Forth related questions.

    How do I define new words within a word in standard Forth?

    One way seems to be with evaluate:

    : myword s" : newword 1 2 ; " evaluate s" : anotherword + ;" evaluate ;
    newword anotherword .s

    I've tested this with gforth and it works, but is this standard? How
    would I do this without evaluate, but still be standard forth, if at all
    possible?

    Modern Forths support nonames within other words, called quotations. See >http://www.forth200x.org/quotations.txt

    Note that this is not an answer to the question. "define" means adding
    words to the dictionary.
    You answered the question: how can generate an execution token within a definition.

    Groetjes Albert
    --
    "in our communism country Viet Nam, people are forced to be
    alive and in the western country like US, people are free to
    die from Covid 19 lol" duc ha
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)