我正在尝试编写文本编辑器来模仿的输入格式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 -> ]
...
再次说明,它本身就可以,但是总而言之,它不会编译。
环形交叉路口少,不使用当地人
! get input as array of lines
: getinput ( -- input )
{ } [
readln
! stop on .
[ "." = ] keep swap
[
drop f
] [
1array append
t
] if
] loop
;
我认为该错误与以下事实有关:即使使用简单的分支,factor也具有相当严格的堆栈效应。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句