我可以将 Squeak 作为 REPL(无 GUI)启动,以便输入和评估 Smalltalk 表达式吗?我知道默认图像不允许这样做。是否有关于如何构建可以从命令行 shell 访问的最小图像的文档?
这里有一个(不太正式的)解决方案: 首先,你需要安装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:
执行输入。
并且您在一个无头映像的脚本中运行它。
从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.
"/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
irb
或python
一样运行Squeak,以便它与标准输入和标准输出终端交互的。 - Jason Orendorff