使用Squeak从命令行界面

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

17

这里有一个(不太正式的)解决方案: 首先,你需要安装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是一个允许运行其他进程、从stdin读取并写入stdout和stderr的软件包。您可以通过OSProcess thisOSProcess(当前进程,也称为squeak)访问stdout AttachableFileStream。

接下来,在用户后台优先级下运行一个无限循环(以便让其他进程运行)。在这个无限循环中,您使用Compiler evaluate:执行输入。

并且您在一个无头映像的脚本中运行它。


9

从Pharo 2.0开始(1.3/1.4版本通过下面的修复也可以实现),不再需要任何黑客技巧。以下代码片段将把您的原始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"

7

这并没有真正回答问题。第一个链接基本上与问题无关。第二个链接是关于服务器操作的问题。但是,据我所知,这个问题是关于像irbpython一样运行Squeak,以便它与标准输入和标准输出终端交互的。 - Jason Orendorff

1

哦,而Friedrich回复中的第一个链接包括对ExternalCommandShell的引用,听起来它提供了类似的功能。 - Ken Causey
有关SecureSqueak的REPLServer的更多信息,请访问http://gulik.pbworks.com/w/page/7760030/REPLServer。 - Ken Causey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接