Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19153

Re: template metaprogramming in C?

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: template metaprogramming in C?
Date 2012-03-30 00:55 +0000
Organization Aioe.org NNTP Server
Message-ID <20120329171933.707@kylheku.com> (permalink)
References (2 earlier) <jkt3o7$f1k$1@solani.org> <jkukir$cvn$1@speranza.aioe.org> <pan.2012.03.29.15.31.08.620000@nowhere.com> <jl2kq2$isg$1@speranza.aioe.org> <jl2o6o$gbe$1@dont-email.me>

Show all headers | View raw


On 2012-03-29, BartC <bc@freeuk.com> wrote:
> I'd imagine also that using the same language for the meta-programming as 
> both the input and output languages, things could get a trifle confusing.

It only gets confusing if you do it right.

If the metaprogramming language is the same as the metaprogrammed language, but
the trees of the metaprogrammed language appear only as encapsulated data
structures, there is no such confusion. But that situation extremely
inconvenient.

Early Lisp metaprogramming was done by writing very clumsy code which analyzed
and constructed code. If you wanted to construct the expression (+ left right)
but where the left and right are pieces of code that you have in variables
x and y, something like (LIST '+ X Y). Make a list of out of the symbol +, and
the values of X and Y.

Then the backquote was invented which allowed code to be generated with a
constructor notation that much more closely resembles the language:

  `(+ ,x ,y)

You write the template as a literal: you quote a piece of the meta-programming
program with the backquote, and then you nidicate the variable parts with
unquotes and splices denoted by , and ,@.

Backquotes can nest, and occur in the unquotes and splices, so very complicated
situations can be hard to understand because of the level confusion: what is
meta and what isn't.

But the overwhelming convenience and notational clarity of backquote trumps
these issues. (And the confusion can be combated by refactoring code into
small functional chunks instead of nesting a whole lot of stuff in one place.)

You basically want the meta-language to be the same as the language precisely
so that you can just quote any construct in the meta-language to obtain a piece
of AST, into which you can substitute things.

It's easier because you're just dealing with one language, and then keeping in
mind what is in the metaprogram and what is in the program. (Or, sometimes,
what is in the meta-meta-program, what is in the meta-program and what is in
the program.)

It's easier to shift between these levels, too. Somtimes you put something into
the program being generated, but then later you want to hoist it back into the
metaprogram: i.e. make it a compile-time calculation. If the two languages
are the same, it's easy to do this while keeping most of the code the same,
or very similar.

Also, sometimes you want to evaluate parts of the meta-material at
macro-expansion time. This is easy if the language is the same: just pass the
tree to the built-in eval function. You don't need a separate eval function for
the target language.

Probably the main reason for using one language is that there are multiple
levels of metaprogramming, not only one. The meta-programming macros themselves
use macros, and those macros can be written using macros: i.e. the
meta-language is applied to itself to create better metaprogramming: it is
turned in on itself.  You don't want N different languages for N different
meta-levels; it's simply not practical, and not even possible when N is
unbounded.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-24 11:57 +0000
  Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-24 07:25 -0700
    Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-26 11:46 +0200
      Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:09 +0200
        Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-28 09:25 +0200
          Re: template metaprogramming in C? James Kuyper <jameskuyper@verizon.net> - 2012-03-28 06:59 -0400
      Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-28 12:13 -0700
      Re: template metaprogramming in C? James Dow Allen <jdallen2000@yahoo.com> - 2012-04-01 02:47 -0700
  Re: template metaprogramming in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-24 18:01 +0100
  Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-24 22:57 +0000
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 18:29 -0700
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:03 +1300
      Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-25 05:32 -0700
  Re: template metaprogramming in C? BGB <cr88192@hotmail.com> - 2012-03-24 17:02 -0700
  Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-25 01:21 +0100
    Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:17 +0200
      Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 11:10 +0200
        Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 10:05 +0000
          Re: template metaprogramming in C? Willem <willem@toad.stack.nl> - 2012-03-28 13:36 +0000
            Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 14:40 +0000
        Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 18:32 +0000
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 22:27 +0200
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 21:02 +0000
        Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-29 16:30 +0100
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-29 23:39 +0200
            Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-29 23:36 +0100
              Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:55 +0000
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:32 +0000
              Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-30 10:52 +0100
                Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-04-01 09:36 +0200
            Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-30 16:32 +1300
              Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-31 01:52 -0700
    Re: template metaprogramming in C? ImpalerCore <jadill33@gmail.com> - 2012-03-28 08:48 -0700
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 12:18 +0200
        Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
          Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:36 +0200
            Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 18:43 -0400
              Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 19:20 -0400
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
  Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:01 -0700
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:10 -0700
  Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:25 +1300
    Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 15:13 +0100
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-26 07:10 +1300
        Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 20:31 +0100
          Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-26 00:54 -0700

csiph-web