Skip to content

Commit 142afe1

Browse files
committed
v2.0.1
1 parent 6eb8015 commit 142afe1

File tree

8 files changed

+278
-212
lines changed

8 files changed

+278
-212
lines changed

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ The easiest way to precompile your JavaScript source code with embedded Constrai
7070

7171
Functions ecapsulated in `${ ... }` are evaluated at rule application, as for JIT compilation too.
7272

73+
## REPL
74+
75+
CHR.js provides a REPL (Read-eval-print loop) to use it interactively with the command line. The `CHR >` REPL can be started by calling `node repl.js` from within the project's root directory. Then it is possible to directly define rules and call constraints:
76+
77+
CHR > dec(0) <=> true
78+
[Rule] Added.
79+
CHR > dec(N) ==> dec(N-1)
80+
[Rule] Added.
81+
CHR > dec(4)
82+
ID Constraint
83+
-- ----------
84+
1 dec(4)
85+
2 dec(3)
86+
3 dec(2)
87+
4 dec(1)
88+
89+
The REPL can also be used programmatically by calling `var Repl = require('chr/repl')`.
90+
7391
## Background
7492

7593
CHR.js was realized as a part of my Master Thesis in Computer Science at the University of Ulm, Germany. Its Project Report for a prototype implementation (versions `0.x`) with additional information about its architecture can be found online: https://fnogatz.github.io/paper-now-chrjs/.

Diff for: dist/chr-wop.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: dist/chr.js

+233-192
Large diffs are not rendered by default.

Diff for: dist/chr.min.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chr",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Interpreter for Constraint Handling Rules (CHR) in JavaScript",
55
"main": "src/index.js",
66
"scripts": {

Diff for: repl.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function Repl () {
2222
var queries = parse(cmd.trim(), { startRule: 'Query' })
2323
} catch (e) {
2424
// no query
25-
var res = eval(cmd)
25+
var res = eval(cmd) // eslint-disable-line
2626
callback(null, res)
2727

2828
return
@@ -39,15 +39,15 @@ function Repl () {
3939
if (query.original.slice(-1)[0] !== ')') {
4040
chrCmd += '()'
4141
}
42-
var queryPromise = eval(chrCmd)
42+
var queryPromise = eval(chrCmd) // eslint-disable-line
4343
return queryPromise
4444
})
4545
}, Promise.resolve())
4646

4747
queryPromise.then(function () {
4848
clearInterval(spin)
4949
r.output.write(chr.Store.toString().split('\n').map(function (row) {
50-
return ' '+row
50+
return ' ' + row
5151
}).join('\n'))
5252
callback()
5353
}).catch(function (e) {
@@ -75,8 +75,12 @@ function errorMsg (e) {
7575

7676
if (e.match(/^TypeError: chr\..* is not a function$/)) {
7777
var constraint = e.replace(/^TypeError: chr\.(.*) is not a function$/, '$1')
78-
return 'Undefined constraint: '+constraint
78+
return 'Undefined constraint: ' + constraint
7979
}
8080

8181
return e
8282
}
83+
84+
if (require.main === module) {
85+
Repl()
86+
}

Diff for: src/compile/head.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,13 @@ Compiler.prototype.generateTellPromises = function generateTellPromises () {
286286
return
287287
}
288288

289+
var params
290+
var lastParamName
291+
289292
if (body.type === 'Replacement' && body.hasOwnProperty('num')) {
290293
// get parameters via dependency injection
291-
var params = util.getFunctionParameters(self.replacements[body.num])
292-
var lastParamName = util.getLastParamName(params)
294+
params = util.getFunctionParameters(self.replacements[body.num])
295+
lastParamName = util.getLastParamName(params)
293296

294297
if (lastParamName && self.opts.defaultCallbackNames.indexOf(lastParamName) > -1) {
295298
// async
@@ -314,23 +317,23 @@ Compiler.prototype.generateTellPromises = function generateTellPromises () {
314317
}
315318

316319
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+
var func = eval(body.func) // eslint-disable-line
321+
params = util.getFunctionParameters(func)
322+
lastParamName = util.getLastParamName(params, true)
320323

321324
if (lastParamName && self.opts.defaultCallbackNames.indexOf(lastParamName) > -1) {
322325
// async
323326
parts.push(
324327
indent(1) + 'return new Promise(function (s) {',
325-
indent(2) + '('+body.func+').apply(self, [' + util.replaceLastParam(params,'s') + '])',
328+
indent(2) + '(' + body.func + ').apply(self, [' + util.replaceLastParam(params, 's') + '])',
326329
indent(1) + '})',
327330
'})'
328331
)
329332
} else {
330333
// sync
331334
parts.push(
332335
indent(1) + 'return new Promise(function (s) {',
333-
indent(2) + '('+body.func+').apply(self, [' + params + '])',
336+
indent(2) + '(' + body.func + ').apply(self, [' + params + '])',
334337
indent(2) + 's()',
335338
indent(1) + '})',
336339
'})'

Diff for: src/compile/util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function getLastParamName (params) {
7272
}
7373

7474
function replaceLastParam (params, replacement) {
75-
return params.replace(/((^.*,|^)\s*)([^,]+)$/g, '$1'+replacement)
75+
return params.replace(/((^.*,|^)\s*)([^,]+)$/g, '$1' + replacement)
7676
}
7777

7878
function isArrowFunction (func) {

0 commit comments

Comments
 (0)