diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-01-21 23:04:50 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-01-21 23:04:50 -0800 |
commit | 474790ae526c164b2852997501b78fab03dcf339 (patch) | |
tree | 850849c75c77d0a377bd4a52628606673669552d /stdlib | |
parent | 2f9f717738b6bf8e1acd728567a0f953fc8a3614 (diff) | |
download | txr-474790ae526c164b2852997501b78fab03dcf339.tar.gz txr-474790ae526c164b2852997501b78fab03dcf339.tar.bz2 txr-474790ae526c164b2852997501b78fab03dcf339.zip |
typecase: merge with etypecase, handle t differnetly.
* stdlib/type.tl (sys:typecase-expander): New function, formed
from body of typecase. Bad clause syntax now handled with
compile-error rather than (throwf 'eval-error). The t symbol
is handled specially: it turns into a t conditon in the
resulting cond rather than a typep test. The compiler will
nicely eliminate dead code after that. Now etypecase is handled
here also: if we are expanding etypecase, we just emit the
extra clause.
(typecase, etypecase): Reduced to sys:typecase-expander calls.
* tests/012/typecase.tl: New file.
* tests/012/compile.tl: Add type.tl to list of compile-tested
files.
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/type.tl | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/stdlib/type.tl b/stdlib/type.tl index 8a83a171..680a4061 100644 --- a/stdlib/type.tl +++ b/stdlib/type.tl @@ -24,27 +24,29 @@ ;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ;; POSSIBILITY OF SUCH DAMAGE. -(defmacro typecase (form . clauses) +(defun sys:typecase-expander (form obj clauses) (let* ((val (gensym)) (cond-pairs (collect-each ((cl clauses)) (tree-case cl ((type . body) - (if (symbolp type) - ^((typep ,val ',type) ,*(or body '(nil))) - :)) - (else (throwf 'eval-error - "~s: bad clause syntax: ~s" - 'typecase cl)))))) - ^(let ((,val ,form)) - (cond ,*cond-pairs)))) + (cond + ((eq type t) + ^(t ,*(or body '(nil)))) + ((symbolp type) + ^((typep ,val ',type) ,*(or body '(nil)))) + (t :))) + (else (compile-error form + "~s: bad clause syntax: ~s" + 'typecase cl)))))) + ^(let ((,val ,obj)) + (cond ,*cond-pairs + ,*(if (eq (car form) 'etypecase) + ^((t (throwf 'case-error + "~s: unhandled type: ~s" + 'etypecase (typeof ,val))))))))) -(defmacro etypecase (form . clauses) - (if [find t clauses eq car] - ^(typecase ,form ,*clauses) - (let ((val (gensym))) - ^(let ((,val ,form)) - (typecase ,val - ,*clauses - (t (throwf 'case-error - "~s: unhandled type: ~s" - 'etypecase (typeof ,val)))))))) +(defmacro typecase (:form f obj . clauses) + (sys:typecase-expander f obj clauses)) + +(defmacro etypecase (:form f obj . clauses) + (sys:typecase-expander f obj clauses)) |