Permalink
Browse files

Add UNPARSE.

  • Loading branch information...
1 parent b8213f6 commit 1fde962cee16ddc033bcce9486e32fa962aee77d @Hexstream committed Oct 16, 2012
Showing with 31 additions and 2 deletions.
  1. +27 −1 main.lisp
  2. +2 −1 package.lisp
  3. +2 −0 parse-number-range.asd
View
28 main.lisp
@@ -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)
@@ -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)))))
View
3 package.lisp
@@ -1,3 +1,4 @@
(cl:defpackage #:parse-number-range
(:use #:cl)
- (:export #:parse))
+ (:export #:parse
+ #:unparse))
View
2 parse-number-range.asd
@@ -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")

0 comments on commit 1fde962

Please sign in to comment.