1

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

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

4

2 回答 2

5

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

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


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

bar \ will execute hello

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

s" hello" find-name dup ?nf name> execute

在哪里

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 )

最后两个可以在 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
;
于 2020-04-21T20:14:10.227 回答
2

这适用于我的系统。

s" Hello" EVALUATE
于 2020-04-21T09:01:32.627 回答