`

metaprogramming

阅读更多
One of the most under-used programming techniques is writing programs that generate programs or program parts. Learn why metaprogramming is necessary and look at some of the components of metaprogramming (textual macro languages, specialized code generators). See how to build a code generator and get a closer look at language-sensitive macro programming in Scheme.

<script type="text/javascript"><!----></script>

<!----> <!----> <!----> <!---->

Code-generating programs are sometimes called metaprograms ; writing such programs is called metaprogramming . Writing programs that write code has numerous applications.

This article explains why you might consider metaprogramming and looks at some of the components of this art -- we'll dive into textual macro languages, survey specialized code generators and discuss how to build them, and dissect language-sensitive macro programming with Scheme.

 

Many different uses for metaprogramming

 

First, you can write programs that will pre-generate tables of data for use at runtime. For example, if you are writing a game and want a quick lookup table for the sine of all 8-bit integers, you can either calculate each sine yourself and hand-code it, have your program build the table at startup at runtime, or write a program to build the custom code for the table before compile-time. While it may make sense to build the table at runtime for such a small set of numbers, other such tasks may cause program startup to be prohibitively slow. In such cases, writing a program to build static data tables is usually your best answer.

Second, if you have a large application where many of the functions include a lot of boilerplate code, you can create a mini-language that will do the boilerplate code for you and allow you to code only the important parts. Now, if you can, it's best to abstract out the boilerplate portions into a function. But often the boilerplate code isn't so pretty. Maybe there's a list of variables to be declared in every instance, maybe you need to register error handlers, or maybe there are several pieces of the boilerplate that have to have code inserted in certain circumstances. All of these make a simple function call impossible. In such cases, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.

Finally, a lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.

As languages acquire more features, code-generating programs get less appealing. What is available as a standard feature of one language may be available only through a code-generating program in another language. However, inadequate language design is not the only reason for needing code-generating programs. Easier maintenance is also a reason.

 

 

A good toturial for Groovy in metaprogamming is:

http://www.javaworld.com/javaworld/jw-09-2008/jw-09-dsls-in-java-4.html#resources

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics