Skip to content

Commit

Permalink
Made line continuations in the REPL much, much nicer and moved all of
Browse files Browse the repository at this point in the history
the REPL-specific code out of CoffeeScript.eval and into the REPL
function (thanks for the suggestion, @TrevorBurnham)
  • Loading branch information
michaelficarra committed Jul 7, 2011
1 parent b1111c9 commit 2a9fd34
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
11 changes: 3 additions & 8 deletions lib/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions lib/repl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/coffee-script.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ exports.eval = (code, options = {}) ->
o = {}
o[k] = v for own k, v of options
o.bare = on # ensure return value
js = compile "_=(#{code}\n)", o
_ = sandbox._
returnValue = Script.runInContext js, sandbox
sandbox._ = _ if returnValue is undefined
returnValue
js = compile code, o
Script.runInContext js, sandbox

# Instantiate a Lexer for our use here.
lexer = new Lexer
Expand Down
29 changes: 21 additions & 8 deletions src/repl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Module = require 'module'
# REPL Setup

# Config
REPL_PROMPT = 'coffee> '
REPL_PROMPT_CONTINUATION = '......> '
enableColours = no
unless process.platform is 'win32'
enableColours = not process.env.NODE_DISABLE_COLORS
Expand All @@ -26,28 +28,39 @@ stdout = process.stdout
error = (err) ->
stdout.write (err.stack or err.toString()) + '\n\n'

# The current backlog of multi-line code.
backlog = ''

# The main REPL function. **run** is called every time a line of code is entered.
# Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting.
run = do ->
# The current backlog of multi-line code.
backlog = ''
# The REPL context
sandbox = Script.createContext()
sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
(buffer) ->
code = backlog += '\n' + buffer.toString()
unless buffer.toString().trim()
repl.prompt()
return
code = backlog += buffer
if code[code.length - 1] is '\\'
return backlog = backlog[0...backlog.length - 1]
backlog = "#{backlog[...-1]}\n"
repl.setPrompt REPL_PROMPT_CONTINUATION
repl.prompt()
return
repl.setPrompt REPL_PROMPT
backlog = ''
try
val = CoffeeScript.eval code, {
_ = sandbox._
returnValue = CoffeeScript.eval "_=(#{code}\n)", {
sandbox,
filename: 'repl'
modulename: 'repl'
}
unless val is undefined
process.stdout.write inspect(val, no, 2, enableColours) + '\n'
if returnValue is undefined
sandbox._ = _
else
process.stdout.write inspect(returnValue, no, 2, enableColours) + '\n'
catch err
error err
repl.prompt()
Expand Down Expand Up @@ -98,7 +111,7 @@ if readline.createInterface.length < 3
else
repl = readline.createInterface stdin, stdout, autocomplete

repl.setPrompt 'coffee> '
repl.setPrompt REPL_PROMPT
repl.on 'close', ->
process.stdout.write '\n'
stdin.destroy()
Expand Down

0 comments on commit 2a9fd34

Please sign in to comment.