我正在嘗試編寫一個文本編輯器來模擬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'。這樣你就不會在每個追加上分配一個新的。 (後綴和附加的區別適用於有或沒有突變)。 –