public inbox for
 help / color / mirror / Atom feed
From: Per Bothner <>
Subject: Re: no class-of ?
Date: Fri, 22 Sep 2017 19:23:00 -0000	[thread overview]
Message-ID: <> (raw)
In-Reply-To: <>

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/, 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

However, that doesn't give you the type on an expression *in a lexical context*.)
	--Per Bothner

  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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \

* 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).