Hi Stefan,

Stefan Behnel wrote:
> Hi,
> 
> Martin C. Martin wrote:
>> I've been doing some thinking and prototyping of a transform system
>> inspired by Common Lisp macros.  You can see the results as the newest CEP:
>>
>> http://wiki.cython.org/enhancements/metaprogramming
>>
>> Briefly, it allows you to define a transform in the Cython source code.
>> The transform runs at compile time, and takes the *parse trees* of its
>> arguments.
> 
> I like the way this reads, yes.
> 
> I'd like to see the "deftrans" functions in a separate source file, maybe a
> ".pxt"? I wouldn't want to see them mixed with normal Cython code.

It's true they're an "advanced" feature, but they're often closely tied 
to other code you write.  For example, suppose you'd like to keep track 
of some statistics on a given expression.  It could be CPU time, memory 
allocated, or a simple count of the number of times you reach a line. 
And you'd like to put these throughout your code, to measure different 
sections.  (We do this so we know where both time and memory allocation 
are going.)  You could keep a hash table that maps the name of the 
section to the accumulating value, but if you don't want the hash table 
overhead, you could assign a numeric id to each name, and use that id to 
index into an array.  In the example below, I'm assuming names_to_ids 
and num_ids are available at compile time, and that the final value of 
num_ids makes it into the C code.

names_to_ids = {}
num_ids = 0

# Get names_to_ids[name] if it exists, otherwise add it to the hash
# table.
def getid(name):
    if name in names_to_ids:
       return names_to_ids[name]
    else:
       id = num_ids
       names_to_ids[name] = id
       num_ids += 1
       return id

record_array = [0]*num_ids # Created at runtime, ideally as a C array.

# Generating IDs and converting names to IDs happens at compile time.
deftrans record(name, *expressions):
    id = getid(name)
    return "record_internal(%c, *%c)" % (id, expressions)

# Get the start time, then evaluate the expressions, then get the
# end time, subtract to get the elapsed time, and accumulate that.
def record_internal(id, *expressions):
    ...
    record_array[id] += elapsed_time

So here there's a lot of mixing of transforms and regular functions.  It 
seems unnatural to me to put record() into a separate file.

What do you think?

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

Reply via email to