首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用测试工具调试Forth

使用测试工具调试Forth
EN

Stack Overflow用户
提问于 2017-05-08 18:34:13
回答 2查看 166关注 0票数 1

我想在调试期间使用一个简单的测试工具来测试我的代码,使用与John Hayes开发的Forth test harness相同的方法。

其概念是定义一个函数,例如my+,然后定义简单的代码片段,这些代码片段将在Tdebug打开时测试代码。

Tdebug if T{ 1 1 my+ -> 2 }T else

它真的像包含tester.f并将{>更改为T{,将}更改为}T一样简单吗

如果大小是个问题,我计划在生产版本中省略tester.f

编辑:

debug if ... then无法工作,因为它在编译之外...

现在我需要帮助!

如果debug为真,则tester.f工作得很好。

如果debug为false,则t{}t必须像( ... )注释一样工作。我该如何对此进行编码?

代码语言:javascript
复制
0 constant debug
: t{
debug if
  ( as defined in tester.fr )
else
  ( what goes here? )
then
;
: }t
debug if
  ( as defined in tester.fr )
else
  ( and what goes here? )
then
;
EN

回答 2

Stack Overflow用户

发布于 2017-05-18 23:51:52

惟一的方法是将输入源流解析到}t。如果t{可以嵌套,就会变得有点棘手-请参阅[ELSE] word的reference implementation

作为参考,标准Forth中简单(非嵌套)大小写的t{ word的生产模式定义:

代码语言:javascript
复制
: t{ ( "ccc }t" -- ) \ skip up to '}t'
  begin
    begin parse-name dup while S" }t" compare 0= until exit then 2drop
    refill 0= 
  until
;

不过,我建议将测试放在单独的文件中,并有条件地包含这些文件("spec“文件)。在这种情况下,您根本不需要t{ word的另一个(生产模式)定义。

票数 1
EN

Stack Overflow用户

发布于 2017-05-21 15:11:27

我最终做了一些与@ruvim类似的事情,在调试模式下包含tester.f,在生产模式中包含notester.f,如下所示:

代码语言:javascript
复制
\ notester.fs
( include either tester.fs or notester.fs )
\ adapted from longcomment.txt
false variable verbose
: t{ ( -- ) \ Long comment
  begin
    token  \ Get next token
    dup 0= if 2drop cr query token then \ If length of token is zero, end of 
                                        \ line is reached. 
                                        \ Fetch new line. Fetch new token.
    s" }t" compare  \ Search for }t
  until
  immediate 0-foldable
;
: testing  ( -- )                                            \ Talking comment.
source verbose @
if dup >r type cr r> >in !
else >in ! drop [char] * emit
then 
;
t{ 1 1 + -> 2 }t                                             \ Usage sample

我发现在生产文件中将测试作为使用注释有助于清晰。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43845430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档