compilation - 在编译字中编译匿名字

标签 compilation metaprogramming forth

我目前正在开发一个用 Forth 编写的小型解释器。对于一个小的优化,我试图用一个词来创建编译词。例如,行为如下:

: creator ( -- a )
    :noname ( u -- u )
        10 + ;
;

10 creator execute .
>> 20 ok

到目前为止,我尝试了几种方法,但都没有奏效(像上面那样天真,切换到解释模式,试图编译一串 Forth 源代码)。这真的可能吗?

最佳答案

当你编写编译词时,你必须非常小心哪些词在编译时执行,哪些在运行时执行。这样的话,10+是在编译时运行的,不会编译成你:noname定义。

我相信这就是你想要的:

: creator ( -- xt )   :noname ( n1 -- n2 )
   10 postpone literal  postpone +  postpone ; ;

另请注意,在许多情况下您可能会使用 CREATE DOES>。例如。如果您希望您的 creator 接受子词使用的数字:

: creator ( n1 "name" -- )   create ,
   does> ( n2 -- n1+n2 )   @ + ;

关于compilation - 在编译字中编译匿名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19363904/

相关文章:

java - 如何构建android UIAutomator 项目?

在 Windows 上用 C.vim 编译?

c++ - 派生类中类模板的部分特化会影响基类

Ruby 元编程

forth - Forth 中的词重定义

linux - 编译 CodeBlocks 13.12 时出现的问题

c++ - const 值作为模板参数

multithreading - Forth 支持多线程吗?

forth - Forth LEAVE ... LOOP 是如何实现的,因为事先不知道 LEAVE 的数量?

c - sybdb.h 导致 "two or more data types in declaration specifiers"