我正在尝试编写一个文本编辑器来模拟ed
的输入格式。在ed
中,一次输入一行,一行输入一个.
即可结束。以下是我想出了:循环输入引用与预期效果不匹配
0 [
[ readln [ "." = not ] keep swap ] dip 1 + swap
] loop
nip 1 - narray
这个片段会从一次用户一个线路输入,停止当它达到一个点,并返回一个字符串数组。
我没有得到任何错误,当它在它自己的,但只要我试图把它变成一个字:
: getinput (-- input)
0 [
[ readln [ "." = not ] keep swap ] dip 1 + swap
] loop
nip 1 -
narray
;
我收到以下错误:
The input quotation to “loop” doesn't match its expected effect
Input Expected Got
[ ~quotation~ dip 1 + swap ] (... -- ... ?) (x -- x x x)
(U) Quotation: [ c-to-factor -> ]
...
我认为这可能与编译器不关心堆栈声明有关,因为它不是一个单词而是一个单词,而不是一个单词。它是否不满意修改下面的循环栈?我知道call()
,但是如果我需要在这里使用它,怎么样?
编辑:我也只是尝试以下操作:
:: getinput (-- input)
0 :> count!
[ [ "." = not ] keep swap ]
[ readln count 1 + count! ] do while
drop count 1 - narray
;
我收到了类似的错误,但栈效果略有不同:
The input quotations to “while” don't match their expected effects
Input Expected Got
[ ~quotation~ keep swap ] (..a -- ..b ?) (x -- x x)
[ _ 1 load-locals readln 0 get-local local-value 1 + 0 get-local... (..b -- ..a) (-- x)
(U) Quotation: [ c-to-factor -> ]
...
再次,对自己罚款,但总而言之,它不会编译。
不错,只是觉得该堆栈用于同样用途的本地变量和函数参数,而不是任意的数据结构。我建议这个小调整:1)'dup“。” ='而不是'[“。” =]保持交换。 2)'V {} clone'而不是'{}'和'suffix!'而不是'1array append'。如果你需要一个数组作为结果,你可以在末尾执行'> array'。这样你就不会在每个追加上分配一个新的。 (后缀和附加的区别适用于有或没有突变)。 –