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?
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?
: 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?
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 am curious about your application/need for this.
I am curious about your application/need for this.
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 :)
You have to call MYWORD before you can use NEWWORD and ANOTHERWORD,
otherwise it does not work (not in Gforth, either).
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>.
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 ]] + ; [[
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?
And I see that macros.fs is for this.
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.
dxforth schrieb am Mittwoch, 10. August 2022 um 19:18:01 UTC+2:
On 10/08/2022 23:30, Brian Fox wrote:
Pretty sure I never has such thoughts when I was beginning forth.
I am curious about your application/need for this.
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.
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?
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.In my case I could do the same with structs or something similar to it,
Perhaps a dumb question but would DEFER words do this job?
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>
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: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?
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 45:28:57 |
Calls: | 6,710 |
Calls today: | 3 |
Files: | 12,243 |
Messages: | 5,354,235 |
Posted today: | 1 |