代码之家  ›  专栏  ›  技术社区  ›  Vijay Mathew Chor-ming Lung

用贝壳发出的吱吱声

  •  25
  • Vijay Mathew Chor-ming Lung  · 技术社区  · 15 年前

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

    4 回复  |  直到 13 年前
        1
  •  14
  •   Géal    14 年前

    这里有一个(hackish)解决方案: 首先,您需要osprocess,所以在工作区中运行它:

    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是一个允许运行其他进程、从stdin读取以及写入stdout和stderr的包。您可以使用访问stdout-attachablefilestream OSProcess thisOSProcess (当前的过程,又称吱吱声)。

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

    在一个无头图像的脚本中运行这个。

        2
  •  8
  •   Sean DeNigris    13 年前

    从Pharo 2.0(和1.3/1.4,以及下面描述的修复)开始,不需要再进行黑客攻击。下面的代码片段将把您的Vanilla 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"
    
        4
  •  0
  •   Ken Causey    14 年前

    项目 http://www.squeaksource.com/SecureSqueak.html 包括一个repl包,它可以提供您所需要的大部分内容。