我想在调试期间使用一个简单的测试工具来测试我的代码,使用与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必须像( ... )注释一样工作。我该如何对此进行编码?
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
;发布于 2017-05-18 23:51:52
惟一的方法是将输入源流解析到}t。如果t{可以嵌套,就会变得有点棘手-请参阅[ELSE] word的reference implementation。
作为参考,标准Forth中简单(非嵌套)大小写的t{ word的生产模式定义:
: 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的另一个(生产模式)定义。
发布于 2017-05-21 15:11:27
我最终做了一些与@ruvim类似的事情,在调试模式下包含tester.f,在生产模式中包含notester.f,如下所示:
\ 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我发现在生产文件中将测试作为使用注释有助于清晰。
https://stackoverflow.com/questions/43845430
复制相似问题