From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Re: Way to control the order of macro expansion Date: Fri, 3 Jan 2014 01:21:00 +0100 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1388708471 16938 80.91.229.3 (3 Jan 2014 00:21:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 3 Jan 2014 00:21:11 +0000 (UTC) To: "guile-user@gnu.org" Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jan 03 01:21:18 2014 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VysVp-0005bV-2a for guile-user@m.gmane.org; Fri, 03 Jan 2014 01:21:17 +0100 Original-Received: from localhost ([::1]:47634 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VysVo-0004eB-JB for guile-user@m.gmane.org; Thu, 02 Jan 2014 19:21:16 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59709) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VysVa-0004bU-Rx for guile-user@gnu.org; Thu, 02 Jan 2014 19:21:03 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VysVZ-00046z-N1 for guile-user@gnu.org; Thu, 02 Jan 2014 19:21:02 -0500 Original-Received: from mail-wg0-x22b.google.com ([2a00:1450:400c:c00::22b]:37348) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VysVZ-00046j-Ey for guile-user@gnu.org; Thu, 02 Jan 2014 19:21:01 -0500 Original-Received: by mail-wg0-f43.google.com with SMTP id k14so13079418wgh.10 for ; Thu, 02 Jan 2014 16:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=rwbKVH+LPU0Ga/VxHj5N2k6DbbitTjCD38+Md9LbX5Y=; b=HG4NDDx4JSJBC5lLa15ohnfGNxLcF3+WLqABE2+p7mIACdjyfLWFcfbfSCUY1H02PG dTrs84+8fyBm3BSM8+RxjOLK78c/PQTCu4/lumugF63OO4QpeWHX9WmpSABQ+xeJXBDd 38wUpYBo70EUsNpUj0jyH19YgcaKOawCEk9RrikactT3kTnk9brgtNXZ54J9M7YyVr/W huDEZl5okAZxk7o1kSb0arFSYU+jndyf8m76sRS6VpdhuNaTQdsRx8+gr2Yzpmey91gT K0QqnncYfGcNvz8jeDLXmqEK5g0ChHATU4yd0LbdHhIwBC2eCLBgPrie7Z7i1Uv7FXlW eZWQ== X-Received: by 10.194.187.101 with SMTP id fr5mr200199wjc.76.1388708460382; Thu, 02 Jan 2014 16:21:00 -0800 (PST) Original-Received: by 10.194.178.134 with HTTP; Thu, 2 Jan 2014 16:21:00 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c00::22b X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10955 Archived-At: It turned out, that the above solution doesn't work exactly as expected -- since the scopes of "private" and "interface" get separated (by the with-syntax form that I have been so proud of), it is impossible for the public forms to refer to private bindings. To solve that issue, I had to pass the private bindings to the function passed with-syntax and then retrieve them. For some reason (which is unclear to me) I had to convert them to datum and then back to syntax, because if they were given verbatim, they failed to get renamed properly. I'd appreciate if anyone could explain it to me, or reviewed the code below to tell whether the names and comments aren't misleading (e.g. I used the name "lexical-context" for a variable, but I'm not sure whether the entity that it refers to can properly be called "lexical context") (define-syntax private+public (lambda (stx) (define (interface+name+body specs lexical-context) ;; the second argument is the lexical context that we ;; want to preserve while extracting the specs (define (interface-name interface) (match interface ((head . tail) (interface-name head)) ((? symbol? name) name))) `(,(datum->syntax stx (syntax->datum lexical-context)) ;; for some reason we need to deconstruct and reconstruct ;; lexical-context here ,(map (lambda(spec) (syntax-case spec () ((interface . body) (datum->syntax stx `(,(syntax->datum #'interface) ,(interface-name (syntax->datum #'interface)) ,(syntax->datum #'body)))))) specs))) (syntax-case stx () ((_ (private ...) ((pub-define . pub-spec) ...)) ;; we pass the private definitions to the interface+name+body, ;; because we want to be able to refer to private definitions ;; from within the public ones (otherwise the macro processor ;; would treat them as if they were within separate contexts) (with-syntax ((((private ...) ((interface name body) ...)) (interface+name+body #'(pub-spec ...) #'(private ...)))) #'(begin (define name (and (defined? 'name) name)) ... (let () private ... (set! name (let () (pub-define interface . body) name)) ...)))))))