29

Can I launch Squeak as a REPL (no GUI), where I can enter and evaluate Smalltalk expressions? I know the default image don't allow this. Is there any documentation on how to build a minimum image that can be accessed from a command-line shell?

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93

4 Answers4

17

Here is a (hackish) solution: First, you need OSProcess, so run this in a Workspace:

Gofer new squeaksource:'OSProcess'; package:'OSProcess';load.

Next, put this in the file 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)

And last, run this command:

squeak -headless path/to/squeak.image /absolute/path/to/repl.st

You can now have fun with a Smalltalk REPL. Dont forget to type in the command:

Smalltalk snapshot:true andQuit:true

if you want to save your changes.

Now, onto the explanation of this solution: OSProcess is a package that allows to run other processes, read from stdin, and write to stdout and stderr. You can access the stdout AttachableFileStream with OSProcess thisOSProcess (the current process, aka squeak).

Next, you run an infinite loop at userBackgroundPriority (to let other processes run). In this infinite loop, you use Compiler evaluate: to execute the input.

And you run this in a script with a headless image.

Géal
  • 1,482
  • 11
  • 13
9

As of Pharo 2.0 (and 1.3/1.4 with the fix described below), there are no more hacks necessary. The following snippet will turn your vanilla Pharo image into a REPL server...

From 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.

If you want the image to always be a REPL, put the code in a #startup: method; otherwise, pass the script at the command line when you want REPL mode, like:

"/path/to/vm" -headless "/path/to/Pharo-2.0.image" "/path/to/gistfile1.st"
Sean DeNigris
  • 6,306
  • 1
  • 31
  • 37
7

Please visit: http://map.squeak.org/package/2c3b916b-75e2-455b-b25d-eba1bbc94b84 and Run Smalltalk on server without GUI?

Community
  • 1
  • 1
Friedrich
  • 5,916
  • 25
  • 45
  • This doesn't really answer the question. The first link is basically unrelated to the question. The second is to a question about server operation. But this question, as I understand it, is about running Squeak like `irb` or `python` so that it interacts with the terminal on stdin and stdout. – Jason Orendorff May 19 '11 at 08:48
1

The project http://www.squeaksource.com/SecureSqueak.html includes a REPL package that may provide much of what you are looking for.

Ken Causey
  • 31
  • 2
  • Oh and the first link in Friedrich's response includes reference to ExternalCommandShell which sounds like it provides similar functionality. – Ken Causey May 19 '11 at 14:39
  • More information about REPLServer from SecureSqueak can be found at http://gulik.pbworks.com/w/page/7760030/REPLServer . – Ken Causey May 19 '11 at 14:44