CSC/ECE 517 Fall 2012/1w41 as: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 26: Line 26:


==Uses of Meta-programming==
==Uses of Meta-programming==
Let us look at some of the uses of meta-programming:  
Let us look at some of the uses of meta-programming:
1) The major use of meta-programming is that it pre-generates the code at run-time. For example, if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup during runtime.  
 
1)The major use of meta-programming is that it pre-generates the code at run-time. For example, if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup during runtime.  
 
 
2) It is the key ingredient for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.  
2) It is the key ingredient for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.  
3) Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code.
3) Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code.

Revision as of 22:12, 3 October 2012

Introduction

In this page, the concept of meta-programming is discussed. We first give the definition of meta-programming, along with a brief description. Next, we give an overview of the concept of meta-program in general i.e., its evolution and implementation in various languages is discussed. We then focus on the languages in which Meta -programming is prevalent namely Ruby, Java, C, C++ and Python. We then follow it up with the advantages and disadvantages of the same before concluding.

Definition

Meta-programming is the discipline of writing programs that represent and manipulate other programs or themselves. An established example of meta-program is a compiler. We can also define a meta-program as a program which generates code and writing such programs is called meta-programming. This allows programmers to get more work done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation. The ability of a programming language to be its own meta-language is called reflection or reflexivity. This reflection ability is a feature that is very valuable to facilitate meta-programming.

Evolution of Meta-programming

Meta-programming was first introduced in Lisp. In Lisp, instead of just programming towards the language, we usually build the language up to our program. This is where the meta-programming comes into picture. For meta-programming purpose, Lisp provides macros as a standard facility to write code that will be directly be read and compiled by the system at run time.

For Meta programming in C-language,one of the popular examples is the Lex/Yacc system for generation of parsers and lexical scanners. That is, it reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in C.

For Meta programming in C++, it is done with C++ templates i.e., we define the program’s content by writing code that generates it. The code is generally written in between question marks.

Similar programs exist for Java and C#. The general idea, however, is that such an approach can be used anywhere we have a formal specification for which we know how to generate code.

Prolog, a generic purpose programming language, which uses the same data structures as the meta-programming and also provides simple clause expansions, is generally used for meta-programming.

How Meta-programming works

Meta-programming usually works in one of three ways. The first way is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs).

The second approach is dynamic execution of expressions that contain programming commands, often composed from strings, but can also be from other methods using arguments and/or context. Thus, "programs can write programs."

The third way is to step outside the language entirely. General purpose program transformation systems, which accept language descriptions and can carry out arbitrary transformations on those languages, are direct implementations of general meta-programming. This allows meta-programming to be applied to virtually any target language without regard to whether that target language has any meta-programming abilities of its own.

Uses of Meta-programming

Let us look at some of the uses of meta-programming:


1)The major use of meta-programming is that it pre-generates the code at run-time. For example, if we are writing an application and we want a quick lookup table containing keys and values associated with the keys, we can have the program build the table at startup during runtime.


2) It is the key ingredient for Domain Specific Languages. That is we use meta-programming to encapsulate domain-specific knowledge.


3) Using meta-programming we can easily reduce the cost and time of development for a system by one order of magnitude, while improving the overall quality of the resulting code.