Continuining the LISP thread...

>> The problem is that the code that accesses the syntax tree has to be
>> written in Python and inserted into the compiler at launch time -- you
>> cannot analyse yourself.
>
> So, how hard would it be to provide a mechanism to do this in the same
> file?  There's a large subset of use cases for Lisp's macros which need

Heh, ok, starting to understand you. Well, I can think of a way to have
compile-time macro support in Cython that would make sense and be a
natural part of other optimizations, however that is *far* in the future.

This framework allows kind of the same thing, but it is so complicated and
will easily break if Cython changes so it would practically never be worth
the effort, especially when there is ample support for this in Python at
run-time already.

Python have for a long time allowed "run-time" macros, ie code generation,
of course (though perhaps not as powerful as LISP, and gives a slower
implementation):

def make_adder(howmuch):
  def adder(x): return x + howmuch
  return adder

add10_func = make_adder(10)
print add10_func(z) # prints the value of z+10

Now, adding two seperate (hard!) features to Cython:
1) Supporting inner functions, ie closures, at all
2) Automatically collapse function calls where one can know that the code
doesn't depend on anything that isn't known at compile time.

Once this is done, the make_adder call above should automatically collapse
and the add10 function be made directly compile-time. Try to read the
clock or anything similar in make_adder though and it will be delayed to
run-time. Of course one could have a @Macro annotation to make sure you
got compiler errors if it couldn't be collapsed.

(But NOTE: This is hard, and might never be done, I'm only dreaming. I
don't see any real showstoppers for this but some hard work is needed and
perhaps not as much return-on-investment as other exciting features one
could add.)

Dag Sverre

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to