Skip to content

Commit

Permalink
fix guard macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Jan 13, 2024
1 parent dd26bf4 commit c82f8ed
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dist/std.min.scm

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

30 changes: 25 additions & 5 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.
25 changes: 21 additions & 4 deletions lib/R7RS.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1186,15 +1186,32 @@

;; -----------------------------------------------------------------------------
(define-syntax guard
(syntax-rules (catch)
(syntax-rules (catch aux =>)
((_ aux (cond result) rest ...)
(let ((it cond))
(if it
result
(guard aux rest ...))))
((_ aux (cond => fn) rest ...)
(let ((it cond))
(if it
(fn it)
(guard aux rest ...))))
((_ aux (cond) rest ...)
(let ((it cond))
(if it
it
(guard aux rest ...))))
((_ (var cond1 cond2 ...)
body ...)
(try
body ...
(catch (var)
(cond cond1
cond2 ...)))))
"(guard (variable (cond result)
(guard aux
cond1
cond2 ...)))))
"(guard (variable (cond)
(cond => fn)
(cond2 result))
body)
Expand Down
24 changes: 24 additions & 0 deletions tests/std.scm
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,27 @@
(t.is (parameterize ((radix 2))
(f 12))
"1100")))

(test "std: guard function with =>"
(lambda (t)
(t.is (guard (condition
((assq 'a condition) => cdr)
((assq 'b condition) => car))
(raise (list (cons 'b 23))))
'b)))

(test "std: guard list"
(lambda (t)
(t.is (guard (condition
((assq 'a condition) => cdr)
((assq 'b condition) "error"))
(raise (list (cons 'b 23))))
"error")))

(test "std: guard identity"
(lambda (t)
(t.is (guard (condition
((assq 'a condition) => cdr)
((assq 'b condition)))
(raise (list (cons 'b 23))))
'(b . 23))))

0 comments on commit c82f8ed

Please sign in to comment.