<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>This sounds in part similar to what the Diameter application (part of OTP R14B03) does.</div><div><br></div><div>The Diameter application uses a parse_transform called diameter_exprecs, which is essentially the same as the exprecs module in <a href="http://github.com/esl/parse_trans">http://github.com/esl/parse_trans</a></div><div><br></div><div>For one thing, this allows users to work with record definitions generated at compile-time, without having to deal with include files. It also allows the diameter_service agent to check ubiquitous attributes like 'Destination-Host', 'Destination-Realm' in any record, as long as that record is known by the encode-decode ("dictionary") module:</div><div><br></div><div><div>get_avp_value(Dict, Name, Rec)</div><div>  when is_tuple(Rec) -></div><div>    try</div><div>        Dict:'#get-'(Name, Rec)</div><div>    catch</div><div>        error:_ -></div><div>            undefined</div><div>    end.</div></div><div><br></div><div>(from diameter/src/app/diameter_service.erl)</div><div><br></div><div>Another thing that might help is parse_trans_codegen.erl, which provides a (IMHO) more readable way to generate code.</div><div>The idea is that you simply wrap the code you want to generate in a fun(), possibly using some simple expansion techniques to import values, etc. If records are known at generation time, they can simply be included and used as-is, with normal record syntax.</div><div><br></div><div><a href="https://github.com/esl/parse_trans/blob/master/doc/parse_trans_codegen.md">https://github.com/esl/parse_trans/blob/master/doc/parse_trans_codegen.md</a></div><div><br></div><div>See also <a href="https://github.com/esl/parse_trans/blob/master/examples/ex_codegen.erl">https://github.com/esl/parse_trans/blob/master/examples/ex_codegen.erl</a> for some (admittedly pretty corny) examples.</div><div><br></div><div>BR,</div><div>Ulf W</div><br><div><div>On 16 Jun 2011, at 21:48, Dan Kelley wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><br><div>Hi,</div><div><br></div><div>I'm working on a router of sorts.  I'd like to have a config file which specifies how to classify messages, what channels are available, and routing rules based on classifications and channels.</div>
<div><br></div><div>When my application starts, I'd like to transform the configuration into a module (let's call it 'router') so I can do someting like 'router:route(Message)'.  Message will be a record that encapsulates a bunch of things about the message, and the returned value will be the channel to which the message should be delivered.  The Message record is reasonably complicated - the top level thing contains several layers of different records.  I also have a large number of macros that I use to extract different fields/subrecords of a top-level Message.</div>
<div><br></div><div>When I started looking at how to do this, I quickly found erl_scan, erl_parse, and friends.  I can easily create a module on the fly with these, but everything falls apart when I need to get at the Message record and macro definitions.  Despite the access provided to compiler-ish things by erl_scan, erl_parse and friends, it looks like the only way to get at the preprocessor is to use epp on files.  This is a problem because the code I want to dynamically create needs to use the record definitions and macros from the Message header file.</div>
<div><br></div><div>Trying to figure a way around this led me to aleppo, which neatly solved the problem of including the header file, but aleppo doesn't process records.  For a brief and exciting moment, it looked like erl_expand_records might save the day, but it expects forms, and I can't get anything with records through erl_parse:parse_form.</div>
<div><br></div><div>Am I missing an easier way to dynamically create code that references macros and records?</div><div><br></div><div>Thanks,</div><div><br></div><div>Dan</div><div><br></div><div><br></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote></div><br><div>
<div>Ulf Wiger, CTO, Erlang Solutions, Ltd.</div><div><a href="http://erlang-solutions.com">http://erlang-solutions.com</a></div><div><br></div><br class="Apple-interchange-newline">
</div>
<br></body></html>