我是否可以将Squeak作为REPL (无GUI)启动,以便输入和计算Smalltalk表达式?我知道默认图像不允许这样做。是否有关于如何构建可从命令行shell访问的最小映像的文档?
发布于 2011-05-24 17:00:22
这里有一个(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是一个包,它允许运行其他进程,从标准输入读取数据,并将数据写入标准输出和标准错误。您可以使用OSProcess thisOSProcess (当前进程,也称为squeak)访问标准输出AttachableFileStream。
接下来,在userBackgroundPriority上运行一个无限循环(让其他进程运行)。在这个无限循环中,您使用Compiler evaluate:来执行输入。
你可以在一个带有无头图像的脚本中运行它。
发布于 2012-05-23 22:27:19
从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"发布于 2010-06-18 14:50:37
请访问:http://map.squeak.org/package/2c3b916b-75e2-455b-b25d-eba1bbc94b84和Run Smalltalk on server without GUI?
https://stackoverflow.com/questions/3067563
复制相似问题