From 5a64f187264eef2ef9a8bcffac6ad3adc0ca5a3d Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Mon, 13 Jan 2014 20:37:25 -0500 Subject: [PATCH] Refactor (loop) in terms of fnr; change to use decorator. --- hy/contrib/loop.hy | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/hy/contrib/loop.hy b/hy/contrib/loop.hy index 91b24d9..2c2690a 100644 --- a/hy/contrib/loop.hy +++ b/hy/contrib/loop.hy @@ -1,6 +1,7 @@ ;;; Hy tail-call optimization ;; ;; Copyright (c) 2014 Clinton Dreisbach +;; Copyright (c) 2014 Paul R. Tagliamonte ;; ;; Permission is hereby granted, free of charge, to any person obtaining a ;; copy of this software and associated documentation files (the "Software"), @@ -60,19 +61,19 @@ (let [[new-body (recursive-replace 'recur g!recur-fn body)]] `(do (import [hy.contrib.loop [--trampoline--]]) - (def ~g!recur-fn - (--trampoline-- (fn [~@signature] - ~@new-body))) + (with-decorator + --trampoline-- + (def ~g!recur-fn (fn [~@signature] ~@new-body))) ~g!recur-fn))) + (defmacro defnr [name lambda-list &rest body] (if (not (= (type name) HySymbol)) (macro-error name "defnr takes a name as first argument")) `(setv ~name (fnr ~lambda-list ~@body))) - -(defmacro loop [bindings &rest body] +(defmacro/g! loop [bindings &rest body] ;; Use inside functions like so: ;; (defun factorial [n] ;; (loop [[i n] @@ -84,13 +85,7 @@ ;; If recur is used in a non-tail-call position, None is returned, which ;; causes chaos. Fixing this to detect if recur is in a tail-call position ;; and erroring if not is a giant TODO. - (with-gensyms [recur-fn] - (let [[fnargs (map (fn [x] (first x)) bindings)] - [initargs (map second bindings)] - [new-body (recursive-replace 'recur recur-fn body)]] - `(do - (import [hy.contrib.loop [--trampoline--]]) - (def ~recur-fn - (--trampoline-- (fn [~@fnargs] - ~@new-body))) - (~recur-fn ~@initargs))))) + (let [[fnargs (map (fn [x] (first x)) bindings)] + [initargs (map second bindings)]] + `(do (defnr ~g!recur-fn [~@fnargs] ~@body) + (~g!recur-fn ~@initargs))))