Skip to content

Commit

Permalink
merge with devel
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Mar 4, 2018
2 parents 119cb52 + 4cb7892 commit 6d5149f
Show file tree
Hide file tree
Showing 19 changed files with 2,694 additions and 137 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text eol=lf
.* text eol=lf
dist/* binary
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
*~
node_modules
coverage
npm-debug.log
\#*
.\#*
.DEV
.[0-9]*
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
spec
*~
covarage
Makefile
jest.config.js
version
templates
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "node"
install:
- npm install
script:
- make lint
- make test
after_script:
- make coveralls
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## 0.2.0
### Features
* Add reduce and set functions
* add while, ++ and -- macros
* ignore comments everything after ; but not inside strings and regexes
* gensym and load functions
* better string function
* Pair methods for working with ALists + Pair::reduce
* throw exception on car/cdr with non list

### Bugs
* fix parsing empty strings
* fix various errors catch by lint
* fix parsing ALists with list as keys and values
* fix parsing quasiquote that evaluate to single pair out if unquote

## 0.1.0
* Initial version
58 changes: 56 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
.PHONY publish
.PHONY: publish test coveralls lint

VERSION=0.1.0
BRANCH=`git branch | grep '^*' | sed 's/* //'`
DATE=`date -uR`
SPEC_CHECKSUM=`md5sum spec/lips.spec.js | cut -d' ' -f 1`
COMMIT=`git log -n 1 | grep commit | sed 's/commit //'`

GIT=git
SED=sed
RM=rm
TEST=test
CAT=cat
NPM=npm
ESLINT=./node_modules/.bin/eslint
COVERALLS=./node_modules/.bin/coveralls
JEST=./node_modules/.bin/jest
UGLIFY=./node_modules/.bin/uglifyjs
BABEL=./node_modules/.bin/babel


ALL: Makefile .$(VERSION) dist/lips.js dist/lips.min.js README.md package.json

dist/lips.js: src/lips.js .$(VERSION)
$(GIT) branch | grep '* devel' > /dev/null && $(SED) -e "s/{{VER}}/DEV/g" -e "s/{{DATE}}/$(DATE)/g" src/lips.js > dist/lips.tmp.js || $(SED) -e "s/{{VER}}/$(VERSION)/g" -e "s/{{DATE}}/$(DATE)/g" src/lips.js > dist/lips.tmp.js
$(BABEL) dist/lips.tmp.js > dist/lips.js
$(RM) dist/lips.tmp.js

dist/lips.min.js: dist/lips.js .$(VERSION)
$(UGLIFY) -o dist/lips.min.js --comments --mangle -- dist/lips.js

Makefile: templates/Makefile
$(SED) -e "s/{{VER""SION}}/"$(VERSION)"/" templates/Makefile > Makefile

package.json: templates/package.json .$(VERSION)
$(SED) -e "s/{{VER}}/"$(VERSION)"/" templates/package.json > package.json || true

README.md: templates/README.md
$(GIT) branch | grep '* devel' > /dev/null && $(SED) -e "s/{{VER}}/DEV/g" -e \
"s/{{BRANCH}}/$(BRANCH)/g" -e "s/{{CHECKSUM}}/$(SPEC_CHECKSUM)/g" \
-e "s/{{COMMIT}}/$(COMMIT)/g" < templates/README.md > README.md || \
$(SED) -e "s/{{VER}}/$(VERSION)/g" -e "s/{{BRANCH}}/$(BRANCH)/g" -e \
"s/{{CHECKSUM}}/$(SPEC_CHECKSUM)/g" -e "s/{{COMMIT}}/$(COMMIT)/g" < templates/README.md > README.md

.$(VERSION): Makefile
touch .$(VERSION)

publish:
npm publish --access=public
$(NPM) publish --access=public

test:
$(JEST)

coveralls:
$(CAT) ./coverage/lcov.info | $(COVERALLS)

lint:
$(ESLINT) src/lips.js spec/lips.spec.js
73 changes: 55 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## Lips is Pretty Simple
## LIPS is Pretty Simple

[![npm](https://img.shields.io/badge/npm-0.1.0-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
[![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&acf06f4b5cae4b8fb9ca088cda6a89e805b48569)](https://travis-ci.org/jcubic/jquery.terminal)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&)](https://coveralls.io/github/jcubic/lips?branch=devel)

Lips is very simple Lisp, similar to Scheme writen in JavaScript.


LIPS is very simple Lisp, similar to Scheme writen in JavaScript.

[Demo](https://codepen.io/jcubic/full/LQBaaV/)

Expand Down Expand Up @@ -31,8 +35,8 @@ https://cdn.rawgit.com/jcubic/lips/master/index.js
```javascript
var {parse, tokenize, evaluate} = require('@jcubic/lips');

parse(tokenize(code)).forEach(function(code) {
evalute(code);
parse(tokenize(string)).forEach(function(code) {
evaluate(code);
});
```

Expand All @@ -46,7 +50,9 @@ You can create new environment using:
var env = new Environment({}, lips.global_environment);
```

You need to use global environment otherwise you will not have any functions.
First argument is an object with functions, macros and varibles (see Extending LIPS at the end).
Second argument is parent environment, you need to use global environment (or other that extend global)
otherwise you will not have any functions.

## What's in

Expand All @@ -71,7 +77,7 @@ You need to use global environment otherwise you will not have any functions.
(print (cadaddr lst)))
```

all functions that match this regex `c[ad]{2,5}r`
all functions that match this regex `c[ad]{2,5}r` are defined.

### ALists

Expand Down Expand Up @@ -124,7 +130,7 @@ then type S-Expression like `(print 10)`. If function return Promise
the execution is paused and restored when Promise is resolved


### Access JavaScript functions
### Access JavaScript functions and objects

```scheme
((. window "alert") "hello")
Expand All @@ -145,14 +151,14 @@ function `$` is available because it's in window object.

or operate on strings

```scheme
```
((. "foo bar baz" "replace") /^[a-z]+/g "baz")
(let ((match (. "foo bar baz" "match")))
(array->list (match /([a-z]+)/g)))
```

### Mapping and filtering
### Mapping, filtering and reducing

```scheme
(map car (list
Expand All @@ -164,13 +170,47 @@ or operate on strings
(filter (lambda (x)
(== (% x 2) 0))
(list 1 2 3 4 5))
(define (reverse list)
(reduce (lambda (list x) (cons x list)) list))
(reverse '(1 2 3 4))
```

### Working with arrays

You can modify array with `set` function and to get the value of the array you can use `.` dot function.

```scheme
(let ((arr (list->array '(1 2 3 4))))
(set arr 0 2)
(print (array->list arr)))
(let* ((div ((. document "querySelectorAll") ".terminal-output > div"))
(len (. div "length"))
(i 0))
(while (< i len)
(print (. (. div i) "innerHTML"))
(++ i)))
```

this equivalent of JavaScript code:

```javascript
var div = document.querySelectorAll(".terminal div");
var len = div.length;
var i = 0;
while (i < len) {
console.log(div[i].innerHTML);
++i;
}
```

### Math and boolean operators

`< > => <= ++ -- + - * / % and or`

## Extending Lips
## Extending LIPS

to create new function from JavaScript you can use:

Expand All @@ -180,9 +220,9 @@ env.set('replace', function(re, sub, string) {
});
```

then you can use it in lips:
then you can use it in LIPS:

```scheme
```
(replace /(foo|bar)/g "hello" "foo bar baz")
```

Expand All @@ -194,14 +234,11 @@ single function argument, that should return lisp code (instance of Pair)
var {Macro, Pair, Symbol, nil} = lips;

env.set('quote-car', new Macro(function(code) {
return new Pair(
new Symbol("quote"),
new Pair(code.car.car, nil)
);
return Pair.fromArray([new Symbol('quote'), code.car.car]);
}));
```

and you can execute this macro in lips:
and you can execute this macro in LIPS:

```scheme
(quote-car (foo bar baz))
Expand Down
44 changes: 29 additions & 15 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Lips Demo</title>
<title>LIPS Demo</title>
<link href="https://rawgit.com/jcubic/jquery.terminal/master/css/jquery.terminal.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://rawgit.com/jcubic/jquery.terminal/master/js/jquery.terminal.js"></script>
<script src="https://rawgit.com/inexorabletash/polyfill/master/keyboard.js"></script>
<script src="index.js"></script>
</head>
<body></body>
<script>
var greetings = 'LIPS is Pretty Simple - Scheme like Lisp Interpreter\n' +
'type [[b;#fff;](env)] to see environment with ' +
'functions macros and variables\n';
window.onerror = function(message, source, lineno, colno,) {
message += ' ' + 'in file ' + source + ' line ' + lineno +
'\nYour browser may not support ES6.';
var term = $('body');
if (term.hasClass('terminal')) {
$.terminal.active().error(message);
} else {
$('body').terminal(function() {
this.error('You need to use modern browser');
}, {
greetings: greetings
}).error(message);
}
};
</script>
<script src="src/lips.js"></script>
<script>
(function() {
var {
Expand Down Expand Up @@ -44,17 +63,14 @@
return string.replace(re, sub);
});
env.set('quote-car', new Macro(function(code) {
return new Pair(
new Symbol("quote"),
new Pair(code.car.car, nil)
);
return Pair.fromArray([new Symbol('quote'), code.car.car]);
}));
var term = $('body').terminal(function(code, term) {
if (!balanced_parenthesis(code)) {
term.error('Unbalanced parenthesis');
} else {
parse(tokenize(code)).forEach(function(code) {
try {
try {
parse(tokenize(code)).forEach(function(code) {
var ret = evaluate(code, env);
if (ret instanceof Promise) {
ret.then((ret) => {
Expand All @@ -65,16 +81,14 @@
} else if (ret !== undefined) {
env.get('print').call(env, ret);
}
} catch (e) {
term.error(e.message);
}
});
});
} catch (e) {
term.exception(e);
}
}
}, {
name: 'lips',
greetings: 'Simple, Scheme like Lisp Interpreter\n' +
'type [[b;#fff;](env)] to see environment with ' +
'functions macros and variables\n'
greetings: greetings
});
})();
</script>
Expand Down

0 comments on commit 6d5149f

Please sign in to comment.