How can I get a words string representation on the data stack in Forth (gforth)?

144 views Asked by At

In gforth I can see a word's definition with see wordname and even get its execution token with ' wordname and also some information with the see subwords such as ' wordname seecol

I have not been able to find a way to get the output of see wordname into a string. I have tried fiddling with stdout and stdin words but cant find the right words.

Can anyone help me please?

2

There are 2 answers

0
ruvim On BEST ANSWER

Gforth provides a word >string-execute ( i*x xt – j*x c-addr u ) that redirects the output of type and emit to a string.

Interactive usage examples

"see wordname" ' evaluate >string-execute type

' see >string-execute wordname type

"wordname" ' see ' execute-parsing >string-execute type

' wordname ' xt-see >string-execute type

Helper words

: dis-xt ( xt -- sd.disforth )
  ['] xt-see >string-execute 
;
: dis-word ( sd.name -- sd.disforth )
  ['] see ['] execute-parsing >string-execute
;

The datatype symbol sd means a character string represented by a cell pair ( c-addr u ).

Helper words usage examples

' traverse-wordlist dis-xt type
"traverse-wordlist" dis-word type
0
francois P On

I have a quick & dirty solution that may inspire you

this is a commandout word that is build to capture external commands outputs as a string into forth

So I can also call gfroth from inside itself as an external command to capture results

like this :

2variable out                  \ the output string
create buffer 1024 allot       \ a buffer
: commandout ( command string -- out variable ) \ s" action" commandout
     r/o open-pipe throw dup buffer swap 256 swap read-file throw  swap close-pipe throw drop buffer swap out 2!
;

\ now an example
s\" gforth -e \qsee ? cr bye \q" commandout  \ capture the ? word content

\ how it displays : 
out 2@ type 
: ?  
  @ . ;

you may work around the commandout word to better fit your needs or reuse it as it is.