From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 44797 invoked by alias); 22 Sep 2017 19:23:25 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 44787 invoked by uid 89); 22 Sep 2017 19:23:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=sk:specifi, wraps, validate, H*r:sk:kawa@so X-HELO: aibo.runbox.com Received: from aibo.runbox.com (HELO aibo.runbox.com) (91.220.196.211) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 22 Sep 2017 19:23:23 +0000 Received: from [10.9.9.210] (helo=mailfront10.runbox.com) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1dvTXj-0005xK-HF for kawa@sourceware.org; Fri, 22 Sep 2017 21:23:19 +0200 Received: from 70-36-239-144.dsl.dynamic.fusionbroadband.com ([70.36.239.144] helo=localhost.localdomain) by mailfront10.runbox.com with esmtpsa (uid:757155 ) (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.82) id 1dvTW4-0001wm-MX for kawa@sourceware.org; Fri, 22 Sep 2017 21:21:37 +0200 Subject: Re: no class-of ? To: kawa@sourceware.org References: <425B3BC8-31F3-4299-ADB1-9E064C1E8D3E@theptrgroup.com> From: Per Bothner Message-ID: <51a642b1-f20d-9f32-645d-ea96b935e3c2@bothner.com> Date: Fri, 22 Sep 2017 19:23:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <425B3BC8-31F3-4299-ADB1-9E064C1E8D3E@theptrgroup.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2017-q3/txt/msg00076.txt.bz2 On 09/22/2017 11:28 AM, Jamison Hope wrote: > On a somewhat related note, is there a way to define a macro that can > determine the type of an expression at macro expansion time, without > actually evaluating the expression? Note that expressions do not have types at macro-expansion time - that happens at a later stage. What you can do is write a procedure and associate either a 'validate' or a 'compile' property with it. There are lots of examples in kawa/lib. Look for the search string 'validate-apply:'. For example in characters.scm: (define (char->integer ch::character) ::int validate-apply: "kawa.lib.compile_misc:charToIntegerValidateApply" (as int ch)) You'll find charToIntegerValidateApply in compile-misc.scm: (define-validate charToIntegerValidateApply (exp required proc) ((exp:isSimple 1 1) (let ((e0 (visit-exp (exp:getArg 0) character))) (apply-exp as int (apply-exp gnu.kawa.functions.Convert:cast character e0))))) This mean is exp is a simple application (no splices or keywords) with one argument, then apply the following transformation. This is evaluated during the InlineCall phase of the compilation. When it comes to charToIntegerValidateApply it first "validates" argument 0 in the context of the "required type" character (which is a 32-bit "primitive" character type). It then wraps the result in a call to the low-lever Convert:cast procedure. Assuming the types are correct, this will be a no-op. Then that result is cast to an int. If all goes well and the incoming value is a 32-bit unboxed character or a 16-bit unboxed char, no code actually needs to be generated - which is the reason for using the charToIntegerValidateApply transformer in the first place. Otherwise, you either get a compile-time error of the appropriate run-time conversion. You can use (exp:getType) to get the type of the expression. There is also the compile-apply property: (define (values #!rest (args :: )) validate-apply: "kawa.lib.compile_misc:valuesValidateApply" compile-apply: "kawa.lib.compile_misc:valuesCompile" (invoke-static 'make args)) This causes the valuesCompile method in class kawa.lib.compile_misc to be called at code-generation time. This is more low-level, and most of the code-generation are actually written in Java to generate custom byte-code depending on the argument expressions and their types. (If I wanted to get the type of a standalone expression, I'd look at the implementation of eval (see kawa/lang/Eval.java), and do the same, but stop before code-generation. Specifically,you would probably want to do compilation.process(Compilation.WALKED). Then you'd do something like compilation.getModule().body.getType(). However, that doesn't give you the type on an expression *in a lexical context*.) -- --Per Bothner per@bothner.com http://per.bothner.com/