Redcar REPL
I got a little bored with the job of porting Redcar to JRuby last weekend, and decided to let myself get diverted into implementing a REPL in Redcar. (A REPL is a Read-Evaluate-Print Loop, like irb.) This was a lot of fun, and I’m also finding it invaluable for exploring SWT APIs from within Redcar.
Implementation
It was a lot easier than it looks. Redcar has an abstraction for documents, and doesn’t assume that they are always going to represent a file on disk.
Documents are actually called “Mirrors”, as they mirror changes to some external resource against the contents of the edit tab. Mirrors only need to implement a few methods to work perfectly within an edit tab.
The benefit is that it’s very easy for anyone to write another Mirror against any resource they like, and then they can immediately ‘edit’ that resource in Redcar.
For example, the file mirror implements something like:
class FileMirror
def read
# read the file off disk
end
def commit(new_contents)
# save new contents to the file
end
end
But it’s easy to fit the repl into this framework too:
class ReplMirror
def read
# display the history
end
def commit(new_contents)
# execute a new line and add the result to the history
end
end
You can see the full implementations of the FileMirror here and the ReplMirror here. Take a look, they are not complicated.
Getting the syntax highlighting to work was simple too. It’s all Ruby, except for the prompts and for error messages. One quick Textmate syntax definition to cover those cases and highlighting is done for free.
Other mirrors I’d like to implement:
- An encrypted file mirror.
- REPLs for other JVM languages.
- An external REPL so you can talk to other processes.
Anyway, that’s the Repl done. Now I should probably get back to…. you know, implementing Save As and so on.
Have a nice day!