Skip to content

Commit

Permalink
fix else as only expression in cond #366
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed May 27, 2024
1 parent c1414d5 commit cf33f5f
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* fix processing list with nested unquote-splicing [#362](https://github.com/LIPS-scheme/lips/issues/362)
* fix exception during error from eval [#362](https://github.com/LIPS-scheme/lips/issues/362)
* allow to catch exceptions from eval
* fix else keyword as only expression in cond [#366](https://github.com/LIPS-scheme/lips/issues/366)

## 1.0.0-beta.19
### Breaking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![npm](https://img.shields.io/badge/npm-1.0.0%E2%80%93beta.19-blue.svg)](https://www.npmjs.com/package/@jcubic/lips)
![1.0.0 Complete](https://img.shields.io/github/milestones/progress-percent/jcubic/lips/1?label=1.0.0%20Complete)
[![Build and test](https://github.com/jcubic/lips/actions/workflows/build.yaml/badge.svg?branch=devel&event=push)](https://github.com/jcubic/lips/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&7aac2bf43fa6f0db134cff1f7721397a)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/lips/badge.svg?branch=devel&6046de8d07eaf73d7b32a46f0fb3d24f)](https://coveralls.io/github/jcubic/lips?branch=devel)
[![Join Gitter Chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jcubic/lips)
![NPM Download Count](https://img.shields.io/npm/dm/@jcubic/lips)
![JSDelivr Download count](https://img.shields.io/jsdelivr/npm/hm/@jcubic/lips)
Expand Down
16 changes: 8 additions & 8 deletions dist/lips.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions dist/lips.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.esm.min.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions dist/lips.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/lips.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/std.min.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 24 additions & 17 deletions dist/std.scm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified dist/std.xcb
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/reference.json

Large diffs are not rendered by default.

41 changes: 24 additions & 17 deletions lib/bootstrap.scm
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,25 @@
Helper macro used by cond.")

;; -----------------------------------------------------------------------------
(define (%else-literal? obj)
"(%else-literal? obj)
Checks if object is symbol else."
(and (symbol? obj)
(or (eq? obj 'else)
(eq? (--> (new lips.LString (obj.literal))
(cmp "else")) 0))))

;; -----------------------------------------------------------------------------
(define-macro (cond . list)
"(cond (predicate? . body)
(predicate? . body))
(predicate? . body)
(else . body))
(cond (predicate? => procedure)
(predicate? => procedure))
(predicate? => procedure)
(else . body))
Macro for condition checks. For usage instead of nested ifs.
You can use predicate and any number of expressions. Or symbol =>
Expand All @@ -573,21 +585,16 @@
(caddr item)
(cdr item)))
(rest (cdr list)))
`(let ((,value ,first))
(if ,value
,(if fn
`(,expression ,value)
`(begin
,@expression))
,(if (and (pair? rest)
(let ((x (caar rest)))
(or (eq? x true)
(and (symbol? x)
(or (eq? x 'else)
(eq? (--> (new lips.LString (x.literal)) (cmp "else")) 0))))))
`(begin
,@(cdar rest))
(if (not (null? rest))
(if (%else-literal? first)
`(begin
,@expression)
`(let ((,value ,first))
(if ,value
,(if fn
`(,expression ,value)
`(begin
,@expression))
,(if (not (null? rest))
`(cond ,@rest))))))
'()))

Expand Down
8 changes: 8 additions & 0 deletions tests/std.scm
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,11 @@
x
x"))))

(test "std: cond"
(lambda (t)
(t.is (cond (else 10)) 10)
(t.is (cond ((zero? 0) 10) (else 20)) 10)
(t.is (cond ((zero? 10) 10) (else 20)) 20)
(t.is (let ((alist '((a . 10) (b . 20) (c . 30))))
(cond ((assoc 'b alist) => cdr) (else #f)))
20)))

0 comments on commit cf33f5f

Please sign in to comment.