2010-06-18 75 views
25

我可以將Squeak作爲REPL(無GUI)發送,我可以在其中輸入和評估Smalltalk表達式嗎?我知道默認圖像不允許這樣做。有沒有關於如何構建可從命令行shell訪問的最小圖像的文檔?使用外殼中的吱吱聲

回答

12

這裏是一個(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(當前進程,aka吱吱聲)訪問標準輸出AttachableFileStream。

接下來,您在userBackgroundPriority上運行一個無限循環(以讓其他進程運行)。在這個無限循環中,您使用Compiler evaluate:來執行輸入。

然後你可以在一個無頭像的腳本中運行它。

0

項目http://www.squeaksource.com/SecureSqueak.html包括REPL包,可以提供很多的,你在找什麼。

+0

哦,Friedrich的回覆中的第一個鏈接包括對ExternalCommandShell的引用,這聽起來像是它提供了類似的功能。 – 2011-05-19 14:39:04

+0

有關來自SecureSqueak的REPLServer的更多信息,請訪問http://gulik.pbworks.com/w/page/7760030/REPLServer。 – 2011-05-19 14:44:18

7

從Pharo 2.0(和1.3/1.4以及下面描述的修復程序)開始,不再需要黑客了。下面的代碼片段會變成你的香草菲羅圖像到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"