[英]Forth: How to create a word that compiles other words until certain delimiter is found?
I use Forth (namely Swapforth ) to configure certain hardware via I2C. 我使用Forth(即Swapforth )通过I2C配置某些硬件 。 I have a word: 我有一句话:
i2c1-send ( reg-address byte -- )
that writes a byte to the specific internal register of a certain chip. 将字节写入特定芯片的特定内部寄存器。 The initialization sequence is quite long, and therefore implementing it as below is not vialable due to memory consumption. 初始化序列很长,因此由于内存消耗,无法按以下方式实现。
: i2c1-init
$1201 $10 i2c1-send
$2130 $43 i2c1-send
[...]
$0231 $43 i2c1-send
;
I have created an implementation that creates a structure holding the length of the sequence in the first cell and triple bytes in the next cells. 我创建了一个实现,该实现创建一个结构,该结构在第一个单元格中保留序列的长度,在下一个单元格中保留三字节。 (Please note that i2c1-send is just a placeholder allowing you to test it without my hardware). (请注意,i2c1发送只是一个占位符,允许您在没有我的硬件的情况下对其进行测试)。
: i2c1-send ( reg_addr byte -- )
\ It is just a placeholder to show what will be written in HW
swap
." addr=" hex . ." val=" . decimal CR
;
: i2c1: ( "<spaces>name" -- )
create here $326e9 0 ,
does> dup cell+ swap
@ 0 do
dup c@ >r 1+
dup c@ 8 lshift swap 1+
dup c@ rot or r> i2c1-send
1+
loop
drop
;
: i2c1-def ( addr val -- )
c, ( adr )
dup 8 rshift c,
255 and c,
;
: i2c1; ( -- )
\ Make sure that i2c1: was used before
$326e9 <> abort" i2c1; without i2c1:"
dup cell+ here swap - ( first_cell length )
\ Verify that the length is a multiple of 3
3 /mod swap 0<> abort" illegal length - not a multiple of 3"
swap !
;
With the above code you define the initialization list similarly: 使用上面的代码,您可以类似地定义初始化列表:
i2c1: set1
$1234 $11 i2c1-def
$1521 $18 i2c1-def
[...]
$2313 $10 i2c1-def
i2c1;
But the memory consumption is significantly reduced (by factor of 2 in case of J1B Forth CPU). 但是显着减少了内存消耗(对于J1B Forth CPU,减少了2倍)。
However I dislike the syntax. 但是我不喜欢这种语法。 I'd prefere to have something that would allow to define the initialization list just by numbers, until certain delimiter is found, like below: 我希望可以通过数字定义初始化列表,直到找到某些定界符为止,如下所示:
i2c1-x: i2c1-init
$1234 $11
$1521 $18
[...]
$2313 $10
i2c1-x;
I have created the word shown below: 我创建了如下所示的单词:
: i2c-delim s" i2c1-x;" ;
: i2c1-x: create here 0 ,
begin
parse-name
2dup i2c-delim compare 0<> while
evaluate \ We store the address later
parse-name
evaluate
c,
\ Now store the address
dup 8 rshift c,
255 and c,
repeat
2drop
dup cell+ here swap - ( first_cell length )
\ Verify that the length is a multiple of 3
3 /mod swap 0<> abort" length not a multiple of 3"
swap !
does> dup cell+ swap
@ 0 do
dup c@ >r 1+
dup c@ 8 lshift swap 1+
dup c@ rot or r> i2c1-send
1+
loop
drop
;
It works perfectly for short definitions: 它非常适合简短定义:
i2c1-x: set2 $1234 $ac $6543 $78 $9871 $01 $3440 $02 i2c1-x;
But fails for longer ones that use multiple lines: 但是对于使用多行的较长代码则失败:
i2c1-x: set2
$1234 $ac
$6543 $78
$9871 $01
$3440 $02
i2c1-x;
Is it possible to define i2c1-x
so that it handles multiple lines, or do I have to use solution based on separate i2c1:
, i2c1-def
and i2c1;
是否可以定义i2c1-x
以便处理多行,还是必须使用基于单独的i2c1:
, i2c1-def
和i2c1;
解决方案i2c1;
? ?
There is REFILL
word to parse multiple lines. 有REFILL
字来解析多行。
\ Get the next name (lexeme) possibly from the next lines
\ NB: Use the result of parse-name-sure immediate
\ since it may be garbled after the next refill
\ (the buffer may be be overwritten by the next line).
: parse-name-sure ( -- c-addr u|0 )
begin parse-name dup 0= while refill 0= if exit then 2drop repeat
;
\ Check if the first string equals to the second
: equals ( c-addr2 u2 c-addr1 u1 -- flag )
dup 3 pick <> if 2drop 2drop false exit then
compare 0=
;
It is a common approach to translate the input until some delimiter. 转换输入直到分隔符是一种常见的方法。 A general function to perform this approach: 执行此方法的一般功能:
\ Translate the input till a delimiter
\ using xt as translator for a lexeme
2variable _delimiter
: translate-input-till-with ( i*x c-addr u xt -- j*x )
>r _delimiter 2!
begin parse-name-sure dup while
2dup _delimiter 2@ equals 0= while
r@ execute
repeat then 2drop rdrop
;
There is a sense to also factor out the manipulation of 16-bits units into a library: 还可以考虑将16位单元的操作纳入库中:
[undefined] w@ [if]
\ NB: little-endian endianness variant
: w! ( x addr -- ) dup 1+ >r >r dup 8 rshift r> c! r> c! ;
: w@ ( addr -- x ) dup c@ 8 lshift swap 1+ c@ or ;
: w, ( x -- ) here 2 allot w! ;
[then]
Also, a function to converting text into number should be in a library. 另外,将文本转换为数字的功能应该在库中。 Using evaluate
for that is not hygienic. 为此使用evaluate
不是卫生的。 See example of StoN
definition in "How to enter numbers in Forth" question. 请参阅“如何在Forth中输入数字”问题中的StoN
定义示例。 A helper to convert the "$"-prefixed numbers may be found in your Forth-system. 在您的Forth系统中可以找到转换“ $”前缀数字的助手。
\ dummy definitions for test only
: s-to-n ( addr u -- x ) evaluate ;
: send-i2c1 ( addr x -- ) ." send: " . . CR ;
The application code: 应用代码:
\ Translate the input numbers till the delimiter into the special format
\ (the code could be simplified using the quotations)
: i2c-delim s" i2c1-x;" ;
: translate-i2c-pair ( c-addr u -- )
s-to-n
parse-name-sure
2dup i2c-delim equals abort" translate-i2c: unexpected delimiter"
s-to-n c, w,
;
: translate-i2c-input ( -- )
i2c-delim ['] translate-i2c-pair translate-input-till-with
;
\ Send data from the special format
: send-i2c1-bulk ( addr u -- )
3 / 0 ?do
dup c@ swap 1+
dup w@ swap 2+ >r send-i2c1 r>
loop drop
;
\ The defining word
: i2c1-x:
create here >r 0 , here >r translate-i2c-input here r> - r> !
does> dup cell+ swap @ send-i2c1-bulk
;
A testcase 一个测试用例
i2c1-x: test
1 2
3 4
5
6
i2c1-x;
test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.