Skip to content

Commit

Permalink
add pretty print to the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
Yosbel Marín committed Dec 9, 2017
1 parent 1ad7274 commit 4507583
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function dispatch(command, opts) {
run(opts)
break
case 'repl' :
repl.launch(R)
repl.launch({R: R})
break
case 'eval' :
_eval(opts)
Expand Down
17 changes: 11 additions & 6 deletions src/compiler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var nodes = require('./nodes')
var util = require('./util')
var T = nodes.type
Expand Down Expand Up @@ -378,18 +377,23 @@ function writeCommonJSStub(ast, ctx) {

// write IIFE stub
function writeIIFEStub(ctx) {
writeRamdaFunctionAsGlobalStub(ctx)
ctx.writeTop(';(function () {')

ctx.newLine()
ctx.newLine()
ctx.write('})()')
}

// write each used Ramda function as if `R` is in the global scope
function writeRamdaFunctionAsGlobalStub(ctx) {
ctx.newLineTop()
ctx.newLineTop()

Object.keys(ctx.usedRamdaFns).forEach(function(key) {
ctx.newLineTop()
ctx.writeTop('var ' + key + ' = R.' + key)
})
ctx.writeTop(';(function () {')

ctx.newLine()
ctx.newLine()
ctx.write('})()')
}

function writeCompilerInfo(ctx) {
Expand Down Expand Up @@ -417,6 +421,7 @@ exports.astToChunks = function astToChunks(ast, ctx, format) {
writeIIFEStub(ctx)
break
case 'none' :
writeRamdaFunctionAsGlobalStub(ctx)
break
default :
throw '`' + format + '` is not a valid format'
Expand Down
45 changes: 27 additions & 18 deletions src/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,41 @@
var repl = require('repl')
var vm = require('vm')
var ram = require('./ramdascript')
var util = require('./util')

// launch RamdaScript REPL, external variables can be
// added to the context, example:
//
// repl.launch(R)
// repl.launch({R: require('ramda')})
//
exports.launch = function launch(extCtx) {
extCtx = extCtx || {}

Object.assign(global, extCtx)
// launch
repl.start('ram> ', null, _eval)
repl.start({
prompt : 'ram> ',
eval : _eval,
writer : util.inspect
})
}

function _eval(code, ctx, file, cb) {
var js
var err
var result
try {
// import passed context
Object.assign(ctx, extCtx)
result = ram.compile(code, {
filename: '<repl>'
})
result = vm.runInContext(result.js, ctx, file)
} catch (e) {
err = e
}
cb(err, result)
function _eval(code, ctx, file, cb) {
var err
var result
var value
var filename = '<repl>'
try {
result = ram.compile(code, {
filename: filename,
format: 'none'
})
value = vm.runInThisContext(result.js, {
filename: filename,
lineOffset: 0,
displayErrors: true
})
} catch (e) {
err = e
}
cb(err, value)
}
36 changes: 36 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,42 @@ exports.isDefVar = function isDefVar(node, varName) {
return node.defVars.indexOf(varName) !== -1
}

exports.inspect = function inspect(val, indent) {
indent = R.defaultTo(0, indent)
var indentUnit = ' '
var indentStr = R.times(R.always(indentUnit), indent).join('')
if (val === void 0) {
return 'void'
}
switch (R.type(val)) {
case 'Null' :
return 'nil'
case 'String' :
return '\'' + val + '\''
case 'RegExp' :
return '/' + val.source + '/' + val.flags
case 'Date' :
return '(new Date \'' + val.toString() + '\')'
case 'Boolean' :
return val ? 'true' : 'false'
case 'Function' :
return '(func [...])'
case 'Object' :
indent++
var kv = R.keys(val).map(function(k) {
return indentStr + indentUnit + ':' + k + ' ' + inspect(val[k], indent)
})
return kv.length ? '{\n' + kv.join('\n') + '\n' + indentStr + '}' : '{}'
case 'Array' :
var arr = val.map(function(item) {
return inspect(item)
})
return '[' + arr.join(' ') + ']'

}
return val
}

// format a string using `{}` placeholder
// example
// u.format('Hello {0}', ['World'])
Expand Down

0 comments on commit 4507583

Please sign in to comment.