From: Per Bothner <per@bothner.com>
To: kawa@sourceware.org
Subject: Re: no class-of ?
Date: Fri, 22 Sep 2017 19:23:00 -0000 [thread overview]
Message-ID: <51a642b1-f20d-9f32-645d-ea96b935e3c2@bothner.com> (raw)
In-Reply-To: <425B3BC8-31F3-4299-ADB1-9E064C1E8D3E@theptrgroup.com>
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 :: <Object[]>))
validate-apply: "kawa.lib.compile_misc:valuesValidateApply"
compile-apply: "kawa.lib.compile_misc:valuesCompile"
(invoke-static <gnu.mapping.Values> '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/
next prev parent reply other threads:[~2017-09-22 19:23 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-21 11:11 Sonny To
2017-09-21 15:24 ` Per Bothner
2017-09-22 18:29 ` Jamison Hope
2017-09-22 19:23 ` Per Bothner [this message]
2017-09-22 22:18 ` Jamison Hope
2017-09-22 22:26 ` Per Bothner
2017-09-23 14:31 ` Jamison Hope
2017-09-24 21:52 ` Per Bothner
2017-09-25 16:58 ` Jamison Hope
2017-09-25 17:19 ` Per Bothner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=51a642b1-f20d-9f32-645d-ea96b935e3c2@bothner.com \
--to=per@bothner.com \
--cc=kawa@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).