Skip to content

Commit

Permalink
v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fnogatz committed Oct 19, 2015
1 parent 6eb8015 commit 142afe1
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 212 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ The easiest way to precompile your JavaScript source code with embedded Constrai

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

## REPL

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:

CHR > dec(0) <=> true
[Rule] Added.
CHR > dec(N) ==> dec(N-1)
[Rule] Added.
CHR > dec(4)
ID Constraint
-- ----------
1 dec(4)
2 dec(3)
3 dec(2)
4 dec(1)

The REPL can also be used programmatically by calling `var Repl = require('chr/repl')`.

## Background

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/.
Expand Down
4 changes: 2 additions & 2 deletions dist/chr-wop.min.js

Large diffs are not rendered by default.

425 changes: 233 additions & 192 deletions dist/chr.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/chr.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chr",
"version": "2.0.0",
"version": "2.0.1",
"description": "Interpreter for Constraint Handling Rules (CHR) in JavaScript",
"main": "src/index.js",
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function Repl () {
var queries = parse(cmd.trim(), { startRule: 'Query' })
} catch (e) {
// no query
var res = eval(cmd)
var res = eval(cmd) // eslint-disable-line
callback(null, res)

return
Expand All @@ -39,15 +39,15 @@ function Repl () {
if (query.original.slice(-1)[0] !== ')') {
chrCmd += '()'
}
var queryPromise = eval(chrCmd)
var queryPromise = eval(chrCmd) // eslint-disable-line
return queryPromise
})
}, Promise.resolve())

queryPromise.then(function () {
clearInterval(spin)
r.output.write(chr.Store.toString().split('\n').map(function (row) {
return ' '+row
return ' ' + row
}).join('\n'))
callback()
}).catch(function (e) {
Expand Down Expand Up @@ -75,8 +75,12 @@ function errorMsg (e) {

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

return e
}

if (require.main === module) {
Repl()
}
17 changes: 10 additions & 7 deletions src/compile/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ Compiler.prototype.generateTellPromises = function generateTellPromises () {
return
}

var params
var lastParamName

if (body.type === 'Replacement' && body.hasOwnProperty('num')) {
// get parameters via dependency injection
var params = util.getFunctionParameters(self.replacements[body.num])
var lastParamName = util.getLastParamName(params)
params = util.getFunctionParameters(self.replacements[body.num])
lastParamName = util.getLastParamName(params)

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

if (body.type === 'Replacement' && body.hasOwnProperty('func')) {
var func = eval(body.func)
var params = util.getFunctionParameters(func)
var lastParamName = util.getLastParamName(params, true)
var func = eval(body.func) // eslint-disable-line
params = util.getFunctionParameters(func)
lastParamName = util.getLastParamName(params, true)

if (lastParamName && self.opts.defaultCallbackNames.indexOf(lastParamName) > -1) {
// async
parts.push(
indent(1) + 'return new Promise(function (s) {',
indent(2) + '('+body.func+').apply(self, [' + util.replaceLastParam(params,'s') + '])',
indent(2) + '(' + body.func + ').apply(self, [' + util.replaceLastParam(params, 's') + '])',
indent(1) + '})',
'})'
)
} else {
// sync
parts.push(
indent(1) + 'return new Promise(function (s) {',
indent(2) + '('+body.func+').apply(self, [' + params + '])',
indent(2) + '(' + body.func + ').apply(self, [' + params + '])',
indent(2) + 's()',
indent(1) + '})',
'})'
Expand Down
2 changes: 1 addition & 1 deletion src/compile/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function getLastParamName (params) {
}

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

function isArrowFunction (func) {
Expand Down

0 comments on commit 142afe1

Please sign in to comment.