Skip to content

Commit

Permalink
throw exception if you don't pass boolean to if + lips code in error …
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
jcubic committed Jan 8, 2019
1 parent d5e0152 commit 24989f8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## 0.8.0
### Features
* new nth and reverse functions
* new type checking functions null? regex? pair? string? number? symbol? array? object?
* new type checking functions null? regex? pair? string? number? symbol? array? object? boolean?
* add lips source code that throwed exception in JavaScript error message
### Bugfix
* fix lambda with rest parameter
### Breaking
* `if` now check if argument is boolean

## 0.7.1
### Bugfix
Expand Down
17 changes: 13 additions & 4 deletions dist/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* includes unfetch by Jason Miller (@developit) MIT License
*
* build: Tue, 08 Jan 2019 21:00:23 +0000
* build: Tue, 08 Jan 2019 21:41:41 +0000
*/
(function () {
'use strict';
Expand Down Expand Up @@ -2625,7 +2625,11 @@ function _typeof(obj) {
var env = this;

var resolve = function resolve(cond) {
if (cond && !is_null(cond) && !isEmptyList(cond)) {
if (typeof cond !== 'boolean') {
throw new Error('if: value need to be boolean');
}

if (cond) {
var true_value = evaluate(code.cdr.car, {
env: env,
dynamic_scope: dynamic_scope,
Expand Down Expand Up @@ -3286,6 +3290,10 @@ function _typeof(obj) {
return is_null(obj) || obj instanceof Pair && obj.isEmptyList();
},
// ------------------------------------------------------------------
'boolean?': function boolean(obj) {
return typeof obj === 'boolean';
},
// ------------------------------------------------------------------
'symbol?': function symbol(obj) {
return obj instanceof _Symbol;
},
Expand Down Expand Up @@ -4271,7 +4279,7 @@ function _typeof(obj) {
return code;
}
} catch (e) {
error && error(e);
error && error(e, code);
}
} // ----------------------------------------------------------------------

Expand Down Expand Up @@ -4317,7 +4325,8 @@ function _typeof(obj) {
return evaluate(code, {
env: env,
dynamic_scope: dynamic_scope,
error: function error(e) {
error: function error(e, code) {
e.message += '\nin code: ' + code.toString();
throw e;
}
});
Expand Down
4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,10 @@
}
var env = this;
var resolve = (cond) => {
if (cond && !is_null(cond) && !isEmptyList(cond)) {
if (typeof cond !== 'boolean') {
throw new Error('if: value need to be boolean');
}
if (cond) {
var true_value = evaluate(code.cdr.car, {
env,
dynamic_scope,
Expand Down Expand Up @@ -1852,6 +1855,10 @@
return is_null(obj) || (obj instanceof Pair && obj.isEmptyList());
},
// ------------------------------------------------------------------
'boolean?': function(obj) {
return typeof obj === 'boolean';
},
// ------------------------------------------------------------------
'symbol?': function(obj) {
return obj instanceof Symbol;
},
Expand Down Expand Up @@ -2477,7 +2484,7 @@
return code;
}
} catch (e) {
error && error(e);
error && error(e, code);
}
}

Expand All @@ -2500,7 +2507,8 @@
var result = await evaluate(code, {
env,
dynamic_scope,
error: (e) => {
error: (e, code) => {
e.message += '\nin code: ' + code.toString();
throw e;
}
});
Expand Down
25 changes: 12 additions & 13 deletions todo.lips
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
(define (on-todo fn element)
"Invoke function for each todo"
(let* ((todo (find-todo element)))
(if todo
(fn todo))))
(if (not (null? todo))
(fn todo))))


(define (get-todos)
Expand Down Expand Up @@ -93,21 +93,20 @@

(define (store namespace . data)
"set or get value from localStorage"
(if data
(--> localStorage (setItem namespace (-> (car data) toString)))
(let ((store (-> localStorage getItem namespace)))
(if store
(read store)
'()))))
(if (not (null? data))
(--> localStorage (setItem namespace (-> (car data) toString)))
(let ((store (--> localStorage (getItem namespace))))
(if (null? store)
'()
(read store)))))

;; ref: https://www.programming-idioms.org/idiom/12/check-if-list-contains-a-value/154/scheme
(define (contains x list)
(if (not list)
false
(if (eq? (car list) x)
true
(contains x (cdr list)))))

false
(if (eq? (car list) x)
true
(contains x (cdr list)))))


(define log (. console "log"))
Expand Down

0 comments on commit 24989f8

Please sign in to comment.