mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
first draft of node.js REPL
This commit is contained in:
15
src/coffee-script.coffee
Normal file
15
src/coffee-script.coffee
Normal file
@@ -0,0 +1,15 @@
|
||||
# Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
||||
|
||||
sys: require('sys')
|
||||
|
||||
exports.compile: (code, callback) ->
|
||||
js: ''
|
||||
coffee: process.createChildProcess 'coffee', ['--eval', '--no-wrap', '--globals']
|
||||
coffee.addListener 'output', (results) ->
|
||||
js += results if results?
|
||||
coffee.addListener 'exit', ->
|
||||
callback(js)
|
||||
coffee.write(code)
|
||||
coffee.close()
|
||||
|
||||
|
||||
26
src/repl.coffee
Normal file
26
src/repl.coffee
Normal file
@@ -0,0 +1,26 @@
|
||||
# A CoffeeScript port/version of the Node.js REPL.
|
||||
|
||||
# Required modules.
|
||||
coffee: require './coffee-script'
|
||||
process.mixin require 'sys'
|
||||
|
||||
# Shortcut variables.
|
||||
prompt: 'coffee> '
|
||||
quit: -> process.stdio.close()
|
||||
|
||||
# The main REPL function. Called everytime a line of code is entered.
|
||||
readline: (code) -> coffee.compile code, run
|
||||
|
||||
# Attempt to evaluate the command. If there's an exception, print it.
|
||||
run: (js) ->
|
||||
try
|
||||
val: eval(js)
|
||||
p val if val isnt undefined
|
||||
catch err
|
||||
puts err.stack or err.toString()
|
||||
print prompt
|
||||
|
||||
# Start up the REPL.
|
||||
process.stdio.open()
|
||||
process.stdio.addListener 'data', readline
|
||||
print prompt
|
||||
Reference in New Issue
Block a user