在 Forth 中执行一个字符或字符串,就好像它是一个单词一样

Sch*_*eme 1 forth gforth

假设我已经s" Hello"存储在某个地方。而且我还定义了: Hello something ;. 有没有办法像执行单词一样执行字符串?

我已阅读文档,但找不到任何相关功能。这可能很明显,但可惜我无法解决。

ruv*_*vim 5

注意:解释(翻译)一个字符串。这意味着在编译状态你的“你好”将被编译。例如:EVALUATE

: foo s" hello" evaluate ;
: [foo] foo ; immediate


: hello ." something" ;
: bar [foo] ; \ "hello" will be compiled into bar

bar \ will execute hello
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用SEARCH-WORDLIST或FIND-NAME词,例如:

s" hello" find-name dup ?nf name> execute
Run Code Online (Sandbox Code Playgroud)

在哪里

find-name ( c-addr u -- nt|0 ) \ returns a name token nt or 0
?nf ( nt|0 -- ) \ throws an exception on zero, i.e. not found
name> ( nt -- xt )
Run Code Online (Sandbox Code Playgroud)

最后两个可以在 Gforth 中定义为

: ?nf ( x|0 -- ) if exit then -13 throw ;
: name> ( nt -- xt|0 )
  dup name>interpret swap name>compile drop over = if exit then drop 0
;
Run Code Online (Sandbox Code Playgroud)