簡體   English   中英

REPL和程序之間的交互

[英]Interaction between a REPL and a program

我正在嘗試做的是創建一個程序來充當REPL可以說scala repl )和用戶之間的代禱。 換句話說,用戶應該鍵入命令,而不是直接進入REPL進行執行,而是將這些命令傳遞給程序,然后在進行一些編輯后將該程序返回給REPL以執行,並顯示答案,依此類推。上。

到目前為止,我得到的是一個使用scala.tools.jline.console.ConsoleReader的簡單界面來顯示提示,獲取用戶輸入並在調用get_edited_data之后顯示結果

我也知道調用“ scala”。!<可以很好地完成工作。 我無法確定的是如何連接它們。 我已經嘗試過使用ProcessIO了。

import java.io.PrintWriter
import scala.sys.process._
import scala.tools.jline.console.ConsoleReader

object Interfere {

  def main(args: Array[String]) {

    val repl = "scala"
    val console = new ConsoleReader
    val prompt = "prompt> "
    console.setPrompt(prompt)
    val out = new PrintWriter(
      console.getTerminal.wrapOutIfNeeded(System.out)
    )
    var line = console.readLine
    while (!line.equals("bye")) {
      val res = get_edited_data(line)
      out.println(s"The answer: $res")
      out.flush()
      line = console.readLine
    }
  }
}

我對ProcessIO所做的事情如下所示。 但這是沒有意義的,因為不可能將當前行作為輸入,並且通常無法訪問為其提供的內容。

Process(repl).run(
      new ProcessIO(
        stdin => line,
        stdout => out,
        stderr => out
      )
    )

我最近有類似的問題。 后來我使用java.lang.Process來處理輸入和輸出。

更好的選擇是使用scala.tools.nsc.interpreter.ILoopscala.tools.nsc.interpreter.IMain來構建REPL,而不是創建另一個進程。

是ILoop的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM