Skip to content

Commit 6eb8015

Browse files
committed
Add REPL
1 parent bc03ec4 commit 6eb8015

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

repl.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module.exports = Repl
2+
3+
var repl = require('repl')
4+
var spinner = require('char-spinner')
5+
var CHR = require('./src/index')
6+
var parse = require('./src/repl.peg.js').parse
7+
8+
function Repl () {
9+
var chr = new CHR()
10+
11+
var r = repl.start({
12+
prompt: 'CHR > ',
13+
input: process.stdin,
14+
output: process.stdout,
15+
eval: function (cmd, context, filename, callback) {
16+
// try Program
17+
try {
18+
var rules = parse(cmd.trim(), { startRule: 'Program' })
19+
} catch (e) {
20+
// try Query
21+
try {
22+
var queries = parse(cmd.trim(), { startRule: 'Query' })
23+
} catch (e) {
24+
// no query
25+
var res = eval(cmd)
26+
callback(null, res)
27+
28+
return
29+
}
30+
31+
// run query
32+
var spin = spinner({
33+
stream: r.outputStream
34+
})
35+
36+
var queryPromise = queries.reduce(function (promise, query) {
37+
return promise.then(function () {
38+
var chrCmd = 'chr.' + query.original
39+
if (query.original.slice(-1)[0] !== ')') {
40+
chrCmd += '()'
41+
}
42+
var queryPromise = eval(chrCmd)
43+
return queryPromise
44+
})
45+
}, Promise.resolve())
46+
47+
queryPromise.then(function () {
48+
clearInterval(spin)
49+
r.output.write(chr.Store.toString().split('\n').map(function (row) {
50+
return ' '+row
51+
}).join('\n'))
52+
callback()
53+
}).catch(function (e) {
54+
clearInterval(spin)
55+
r.output.write(' [Error] ' + errorMsg(e) + '\n')
56+
r.output.write(chr.Store.toString())
57+
callback()
58+
})
59+
60+
return
61+
}
62+
63+
// add rules
64+
chr(rules)
65+
r.output.write(' [Rule' + (rules.body.length > 1 ? 's' : '') + '] Added.\n')
66+
67+
callback()
68+
return
69+
}
70+
})
71+
}
72+
73+
function errorMsg (e) {
74+
e = e.toString()
75+
76+
if (e.match(/^TypeError: chr\..* is not a function$/)) {
77+
var constraint = e.replace(/^TypeError: chr\.(.*) is not a function$/, '$1')
78+
return 'Undefined constraint: '+constraint
79+
}
80+
81+
return e
82+
}

src/compile/head.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,33 @@ Compiler.prototype.generateTellPromises = function generateTellPromises () {
312312
}
313313
return
314314
}
315+
316+
if (body.type === 'Replacement' && body.hasOwnProperty('func')) {
317+
var func = eval(body.func)
318+
var params = util.getFunctionParameters(func)
319+
var lastParamName = util.getLastParamName(params, true)
320+
321+
if (lastParamName && self.opts.defaultCallbackNames.indexOf(lastParamName) > -1) {
322+
// async
323+
parts.push(
324+
indent(1) + 'return new Promise(function (s) {',
325+
indent(2) + '('+body.func+').apply(self, [' + util.replaceLastParam(params,'s') + '])',
326+
indent(1) + '})',
327+
'})'
328+
)
329+
} else {
330+
// sync
331+
parts.push(
332+
indent(1) + 'return new Promise(function (s) {',
333+
indent(2) + '('+body.func+').apply(self, [' + params + '])',
334+
indent(2) + 's()',
335+
indent(1) + '})',
336+
'})'
337+
)
338+
}
339+
340+
return
341+
}
315342
})
316343

317344
parts.push(

src/compile/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports.indentBy = indentBy
44
module.exports.destructuring = destructuring
55
module.exports.getFunctionParameters = getFunctionParameters
66
module.exports.getLastParamName = getLastParamName
7+
module.exports.replaceLastParam = replaceLastParam
78
module.exports.isArrowFunction = isArrowFunction
89

910
function indent (level, text, spaces) {
@@ -70,6 +71,10 @@ function getLastParamName (params) {
7071
return params.replace(/(^.*,|^)\s*([^,]+)$/g, '$2')
7172
}
7273

74+
function replaceLastParam (params, replacement) {
75+
return params.replace(/((^.*,|^)\s*)([^,]+)$/g, '$1'+replacement)
76+
}
77+
7378
function isArrowFunction (func) {
7479
return !func.hasOwnProperty('prototype')
7580
}

src/parser.pegjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ Replacement
413413
expr: func
414414
};
415415
}
416+
/ ReplacementOpeningSymbol __ func:$(ArrowFunction) __ ReplacementClosingSymbol {
417+
return {
418+
type: 'Replacement',
419+
func: func
420+
};
421+
}
416422
/ ReplacementOpeningSymbol __ expr:$(Expression) __ ReplacementClosingSymbol {
417423
return {
418424
type: 'Replacement',

0 commit comments

Comments
 (0)