Skip to content

Commit

Permalink
better binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 12, 2019
1 parent 5b1ff1d commit ac8ffdb
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 0.9.0
### Features
* better binary (options -f -c + read stdin + REPL)

## 0.8.1
### Bugfix
* use exception.code for code that triggered exception
Expand Down
8 changes: 4 additions & 4 deletions README.md
@@ -1,8 +1,8 @@
## LIPS is Pretty Simple

[![npm](https://img.shields.io/badge/npm-0.8.1-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=master&627e7bc615eae8dc657320600d1185076849735f)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=master&66ae96f244da4bd63c74f953f57cfcf3)](https://coveralls.io/github/jcubic/lips?branch=master)
[![npm](https://img.shields.io/badge/npm-DEV-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
[![travis](https://travis-ci.org/jcubic/jquery.terminal.svg?branch=devel&5b1ff1d038f7f0f258d02e0e3034ccfeb68007ce)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&66ae96f244da4bd63c74f953f57cfcf3)](https://coveralls.io/github/jcubic/lips?branch=devel)


LIPS is very simple Lisp, similar to Scheme written in JavaScript.
Expand Down Expand Up @@ -37,7 +37,7 @@ https://unpkg.com/@jcubic/lips
or from rawgit

```
https://cdn.rawgit.com/jcubic/lips/master/dist/lips.min.js
https://cdn.rawgit.com/jcubic/lips/devel/dist/lips.min.js
```

## Usage
Expand Down
133 changes: 106 additions & 27 deletions bin/lips.js
@@ -1,35 +1,114 @@
#!/usr/bin/env node

var {exec} = require('../src/lips');
var fs = require('fs');
/*
global.$ = function() {
return {
css: function(...args) {
console.log('css');
console.log(args);
return this;
},
on: function(...args) {
console.log('on');
console.log(args);
return this;
}
};
};
*/
const {exec} = require('../src/lips');
const fs = require('fs');
const readline = require('readline');

if (process.argv.length > 2) {
process.argv.slice(2).forEach(function(filename) {
fs.readFile(filename, function(err, data) {
if (err) {
console.error(err);
// -----------------------------------------------------------------------------
// code taken from jQuery Terminal
function parse_options(arg, options) {
var settings = Object.assign({}, {
boolean: []
}, options);
var result = {
_: []
};
function token(value) {
this.value = value;
}
var rest = arg.reduce(function(acc, arg) {
if (typeof arg !== 'string') {
arg = String(arg);
}
if (arg.match(/^-/) && acc instanceof token) {
result[acc.value] = true;
}
if (arg.match(/^--/)) {
var name = arg.replace(/^--/, '');
if (settings.boolean.indexOf(name) === -1) {
return new token(name);
} else {
exec(data.toString()).catch(function(e) {
console.error(e.message);
console.error(e.stack);
});
result[name] = true;
}
} else if (arg.match(/^-/)) {
var single = arg.replace(/^-/, '').split('');
if (settings.boolean.indexOf(single.slice(-1)[0]) === -1) {
var last = single.pop();
}
single.forEach(function(single) {
result[single] = true;
});
if (last) {
return new token(last);
}
} else if (acc instanceof token) {
result[acc.value] = arg;
} else if (arg) {
result._.push(arg);
}
return null;
}, null);
if (rest instanceof token) {
result[rest.value] = true;
}
return result;
}

// -----------------------------------------------------------------------------
function run(code) {
if (typeof code !== 'string') {
code = code.toString();
}
return exec(code).catch(function(e) {
console.error(e.message);
if (e.code) {
console.error('error in line ' + e.code);
}
console.error(e.stack);
});
}

// -----------------------------------------------------------------------------
function print(result) {
if (result.length) {
var last = result.pop();
if (last) {
console.log(last.toString());
}
}
}
// -----------------------------------------------------------------------------

const options = parse_options(process.argv);

if (options.c) {
run(options.c).then(print);
} else if (options.f) {
fs.readFile(options.f, function(err, data) {
if (err) {
console.error(err);
} else {
run(data)
}
});
} else {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'lips> ',
terminal: !!process.stdin.isTTY
});
if (process.stdin.isTTY) {
rl.prompt();
}
rl.on('line', function(line) {
rl.pause();
run(line).then(function(result) {
if (process.stdin.isTTY) {
print(result);
rl.prompt();
}
rl.resume();
});
});
}
6 changes: 3 additions & 3 deletions dist/lips.js
@@ -1,12 +1,12 @@
/**@license
* LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. 0.8.1
* LIPS is Pretty Simple - simple scheme like lisp in JavaScript - v. DEV
*
* Copyright (c) 2018-2019 Jakub T. Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
* includes unfetch by Jason Miller (@developit) MIT License
*
* build: Tue, 08 Jan 2019 22:26:13 +0000
* build: Sat, 12 Jan 2019 21:52:33 +0000
*/
(function () {
'use strict';
Expand Down Expand Up @@ -4450,7 +4450,7 @@ function _typeof(obj) {
}); // --------------------------------------

return {
version: '0.8.1',
version: 'DEV',
exec: exec,
parse: parse,
tokenize: tokenize,
Expand Down
6 changes: 3 additions & 3 deletions dist/lips.min.js

Large diffs are not rendered by default.

0 comments on commit ac8ffdb

Please sign in to comment.