3

我是 Forth 新手,正在尝试开发一些(伪有用的)玩具来学习语言。我想精简以下操作:

[ifundef] vehicles    2variable vehicles [then]
[ifundef] cars<       2variable cars<    [then]
vehicles 2@ s" cars "    s+ vehicles 2!
cars<    2@ s" vehicles" s+ cars<    2!

通过以下(更紧凑的)指令

-> vehicles cars

或者,换句话说:

  • "->" 解析以下两个名字
  • 为第一个名称创建一个字典条目“车辆”,用于类似 2 变量的结构,如果它不存在,将为其分配一个字符串
  • 对于第二个名称,创建另一个条目“cars<”(注意 <),如果它不存在,也是一个 2 变量
  • 然后将字符串“cars”添加(而不是替换)到变量车辆
  • 并将字符串“vehicles”添加到变量 cars<

我做了一个黑客攻击,使用字符串操作和评估来获得这种行为......

: space+ ( str -- ) s"  " s+ ;
\ use like: cars add alfa-romeo (first is a 2variable name, second a parsed name)
: add ( a "name" -- ) dup >r 2@ parse-name space+ s+ r> 2! ;

create _x 256 chars allot align
: _x@ ( -- ) _x count ;
: _x! ( -- ) _x place ;

create _y 256 chars allot align
: _y@ ( -- ) _y count ;
: _y! ( -- ) _y place ;

: init_x ( str -- ) 2dup s" [ifundef] " 2swap s+ s"  2variable " s+ 2swap s+ s"  [then]" s+ evaluate ;
: init_y ( str -- ) 2dup s" [ifundef] " 2swap s+ s" < 2variable " s+ 2swap s" <" s+ s+ s"  [then]" s+ evaluate ;

: make-dictionary-entries ( -- ) _x@ init_x    _y@ init_y ;
: add-strings-to-entries  ( -- ) _x@ s"  add "  s+ _y@ s+ evaluate
                                 _y@ s" < add " s+ _x@ s+ evaluate ;

: -> parse-name _x! parse-name _y!
     make-dictionary-entries
     add-strings-to-entries ;



\ CUSTOM TESTING to improve readability of the examples
: test( POSTPONE assert( ; immediate
: true! 0= throw ;
: false! throw ;
: same-string! str= true! ;

-> vehicles cars
test( vehicles 2@  s" cars "     same-string! )
test( cars< 2@     s" vehicles " same-string! )

-> vehicles trucks
-> vehicles dreams
test( vehicles 2@  s" cars trucks dreams " same-string! )
test( trucks< 2@   s" vehicles "           same-string! )

-> cars ferrari
-> cars lamborghini
-> dreams lamborghini
test( cars 2@          s" ferrari lamborghini " same-string! )
test( lamborghini< 2@  s" cars dreams "         same-string! )

我认为存在另一种更直接、更优雅的方式,但这是我目前能做的最好的方式。有什么建议么?

4

1 回答 1

3

一些建议

  1. 从纯后缀解决方案开始自上而下的分解。
  2. 找到顶级后缀解决方案的某种理想 概念实现。
  3. 实现您的 Forth 系统中缺少的单词和/或级别。

注意:这不是一般规则,而是问题中代码的一些弱点。

一个常见的规则是遵循。在管道部分,只能使用后缀语法。前缀语法(解析词)只能在顶层作为糖发生。即任何解析词都应该有一个后缀变体。

给定问题的一般后缀解决方案是:S" content" S" name" update-var

解决方案

\ reference implementations of some underlying words for testing purpose only
: s, ( d-txt -- ) here swap dup allot move ;
: s+ ( d-txt1 d-txt2 -- d-txt3 ) here >r 2swap s, s, r> here over - 0 c, ;
: s+! ( d-txt addr -- ) dup >r 2@ 2swap s+ r> 2! ; \ '+!' naming convention 
: gs+ ( d-txt1 -- d-txt2 ) s"  " s+ ; \ add gap string ('space+' is too long)
\ some Forth-systems have these words as factors:
: created ( d-txt-name -- ) s" create " 2swap s+ evaluate ;
: obey ( i*x d-txt-name wid -- j*x true | i*x d-txt-name false )
  >r 2dup r> search-wordlist if nip nip execute true exit then  false
;

\ the solution itself

wordlist constant v \ for special auto-created variables

: make-var ( d-txt-name -- addr )
  get-current >r v set-current
  created here 0 , 0 ,
  r> set-current
;
: obtain-var ( d-txt-name -- addr )
  v obey if exit then  make-var
;
: update-var ( d-txt-content d-txt-name -- )
  obtain-var s+!
;
: -> \ "vehicles" "cars"
  parse-name parse-name
  2over gs+ 2over s" <" s+ update-var
  gs+ 2swap update-var
;

d-此处堆栈符号中的前缀表示两个单元格的值,d-txt-前缀表示一个字符串(单元格对)。

s, ( d-txt -- )将给定的字符串按原样存储到数据空间中(另见,and c,);注意:在 Gforth 中,它以计数字符串格式存储(参见3.1.3.4 计数字符串)。

createdcreate(在适当的系统中,最后一个应该通过第一个定义)的后缀变体;关于词源,另见标准词included(后缀形式)和include(前缀形式,解析词)。

此外,最好为这些自动创建的变量使用单独的单词表,以避免可能出现的冲突名称问题(例如,如果你需要的话-> here str)。请参阅修订版 8中没有单独词表的变体。

要编写测试用例,也可以使用著名的tester.fs库(在 Gforth 中,它位于 ./test/ 目录中)。在这个库->中,单词是为自己的目的而定义的,因此可以使用同义词来克服不希望的阴影。

于 2018-01-31T03:22:51.330 回答