Skip to content

Commit

Permalink
Add UNPARSE.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexstream committed Oct 16, 2012
1 parent b8213f6 commit 1fde962
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
28 changes: 27 additions & 1 deletion main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(to nil) to-p
(by 1) by-p
direction (limit-kind :unbounded))
'(values from to limit-kind step direction)
'(values from to limit-kind by direction)
(flet ((direction (new-direction)
(if direction
(unless (eq direction new-direction)
Expand Down Expand Up @@ -51,3 +51,29 @@
(duplicate "BY"))
(setf by value
by-p t)))))))

(defun %direction/limit-to-keywords (direction limit-kind)
'(values from-keyword to-keyword)
(cartesian-product-switch:cartesian-product-switch
((ecase direction
+ -)
(ecase limit-kind
:unbounded :inclusive :exclusive))
;; +
(values :from nil)
(values :from :to)
(values :from :below)
;; -
(values :downfrom nil)
(values :from :downto)
(values :from :above)))

(defun unparse (from to limit-kind by direction)
(multiple-value-bind (from-keyword to-keyword)
(%direction/limit-to-keywords direction limit-kind)
;; Backquote indented badly...
(nconc (list from-keyword from)
(when to-keyword
(list to-keyword to))
(when (and by (/= by 1))
(list :by by)))))
3 changes: 2 additions & 1 deletion package.lisp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(cl:defpackage #:parse-number-range
(:use #:cl)
(:export #:parse))
(:export #:parse
#:unparse))
2 changes: 2 additions & 0 deletions parse-number-range.asd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

:description "Parses LOOP's convenient \"for-as-arithmetic\" syntax into 5 simple values: from, to, limit-kind (:inclusive, :exclusive or nil if unbounded), by (step) and direction (+ or -)). Intended for easy implementation of analogous functionality in other constructs."

:depends-on (#:cartesian-product-switch)

:version "1.0"
:serial cl:t
:components ((:file "package")
Expand Down

0 comments on commit 1fde962

Please sign in to comment.