简单的 repl 函数被 sbcl 编译为“乱序”

Eri*_*ada 2 lisp compilation sbcl common-lisp

我有这个简单的程序

(defun testing-func ()
  (print "@Repl has Started@")
  (loop (print (eval (read)))))

(sb-ext:save-lisp-and-die #P"output-test"
   :toplevel #'testing-func
   :executable t)
Run Code Online (Sandbox Code Playgroud)

每当我调用testing-funcREPL 时,它都能正常工作;像这样:

CL-USER> (testing-func)
@Repl has Started@
1 ;;input

1 2 ;; 1 is output, 2 is input

2 3 ;; 2 is output, 3 is input

3 ;; 3 is output
Run Code Online (Sandbox Code Playgroud)

但在编译后sbcl --load testing-func.lisp,我得到的输出与输入“乱序”。

@Repl has Started@
1 ;; input
  ;; output
2 ;; input
1 ;; output
3 ;; input
2 ;; output
Run Code Online (Sandbox Code Playgroud)

我真的不知道为什么或如何会发生这种情况。loop我还尝试通过使用递归和let*or来摆脱and 严格的顺序funcall,但出现了相同的现象,这意味着我对编译有根本性的误解。

Rai*_*wig 6

与编译无关。

Common Lisp Streams 可以被缓冲。在让用户输入之前,您需要确保输出已到达用户手中。

请参阅Common Lisp 中的函数FORCE-OUTPUT和。导致任何挂起的输出写入输出设备,然后返回。FINISH-OUTPUTFINISH-OUTPUT

您需要FINISH-OUTPUT在输出完成后调用您想要到达的输出设备。

(defun testing-func ()
  (print "@Repl has Started@")
  (print '---)
  (terpri)      
  (loop (finish-output)
        (print (eval (read)))
        (print '---)
        (terpri)))
Run Code Online (Sandbox Code Playgroud)