从shell中使用Squeak

Vij*_*hew 25 smalltalk squeak pharo

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

小智 15

这是一个(hackish)解决方案:首先,您需要OSProcess,因此在Workspace中运行:

Gofer new squeaksource:'OSProcess'; package:'OSProcess';load.
Run Code Online (Sandbox Code Playgroud)

接下来,将它放在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)
Run Code Online (Sandbox Code Playgroud)

最后,运行此命令:

squeak -headless path/to/squeak.image /absolute/path/to/repl.st
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用Smalltalk REPL获得乐趣.别忘了输入命令:

Smalltalk snapshot:true andQuit:true
Run Code Online (Sandbox Code Playgroud)

如果您想保存更改.

现在,解释一下这个解决方案:OSProcess是一个允许运行其他进程的包,从stdin读取,并写入stdout和stderr.您可以使用OSProcess thisOSProcess(当前进程,也就是吱吱声)访问stdout AttachableFileStream .

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

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


Sea*_*ris 8

从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.
Run Code Online (Sandbox Code Playgroud)

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

"/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
Run Code Online (Sandbox Code Playgroud)