【问题标题】:Using Squeak from a shell在 shell 中使用 Squeak
【发布时间】:2011-03-05 07:09:39
【问题描述】:

我可以将 Squeak 作为 REPL(无 GUI)启动,我可以在其中输入和评估 Smalltalk 表达式吗?我知道默认图像不允许这样做。是否有任何关于如何构建可以从命令行 shell 访问的最小映像的文档?

【问题讨论】:

    标签: smalltalk squeak pharo


    【解决方案1】:

    这是一个(骇人听闻的)解决方案: 首先,您需要 OSProcess,所以在 Workspace 中运行它:

    Gofer new squeaksource:'OSProcess'; package:'OSProcess';load.
    

    接下来,将其放入文件 repl.st:

    OSProcess thisOSProcess stdOut 
      nextPutAll: 'Welcome to the simple Smalltalk REPL'; 
      nextPut: Character lf; nextPut: $>; flush.
    [ |input|
      [ input := OSProcess readFromStdIn.
        input size > 0 ifTrue: [
          OSProcess thisOSProcess stdOut 
            nextPutAll: ((Compiler evaluate: input) asString; 
            nextPut: Character lf; nextPut: $>; flush 
        ]
      ] repeat.
    ]forkAt: (Processor userBackgroundPriority)
    

    最后,运行这个命令:

    squeak -headless path/to/squeak.image /absolute/path/to/repl.st
    

    您现在可以享受使用 Smalltalk REPL 的乐趣了。不要忘记输入命令:

    Smalltalk snapshot:true andQuit:true
    

    如果您想保存更改。

    现在,解释一下这个解决方案: OSProcess 是一个允许运行其他进程、从标准输入读取、写入标准输出和标准错误的包。您可以使用 OSProcess thisOSProcess(当前进程,又名 squeak)访问标准输出 AttachableFileStream。

    接下来,您在 userBackgroundPriority 上运行一个无限循环(让其他进程运行)。在这个无限循环中,您使用Compiler evaluate: 来执行输入。

    然后你在一个带有无头图像的脚本中运行它。

    【讨论】:

      【解决方案2】:

      从 Pharo 2.0(和带有下述修复的 1.3/1.4)开始,不再需要任何 hack。下面的 sn-p 会将您的原版 Pharo 图像转换为 REPL 服务器...

      来自https://gist.github.com/2604215

      "Works out of the box in Pharo 2.0. For prior versions (definitely works in 1.3 and 1.4), first file in https://gist.github.com/2602113"
      
      | command |
      [
          command := FileStream stdin nextLine.
          command ~= 'exit' ] whileTrue: [ | result |
              result := Compiler evaluate: command.
              FileStream stdout nextPutAll: result asString; lf ].
      
      Smalltalk snapshot: false andQuit: true.
      

      如果您希望图像始终是 REPL,请将代码放在 #startup: 方法中;否则,当您需要 REPL 模式时,请在命令行中传递脚本,例如:

      "/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
      

      【讨论】:

        【解决方案3】:

        【讨论】:

        • 这并不能真正回答问题。第一个链接基本上与问题无关。第二个是关于服务器操作的问题。但据我了解,这个问题是关于像 irbpython 这样运行 Squeak,以便它与标准输入和标准输出上的终端交互。
        【解决方案4】:

        http://www.squeaksource.com/SecureSqueak.html 项目包含一个 REPL 包,它可以提供您正在寻找的大部分内容。

        【讨论】:

        • 哦,弗里德里希的回复中的第一个链接包括对 ExternalCommandShell 的引用,听起来它提供了类似的功能。
        • 更多关于 SecureSqueak 的 REPLServer 的信息可以在 gulik.pbworks.com/w/page/7760030/REPLServer 找到。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多