Friday, May 14, 2010

Understanding Metaprogramming as Scope Manipulation

The common understanding of metaprogramming is to use a programming language for the manipulation of a realized program. In this article, I elaborate the scope concept as the internal representation of a programs structure. Metaprogramming is then explained as manipulating a programs scope.

Programs as Multi-Dimensional Scopes

A scope is the set of modules, classes, functions, constants and variables (generally called entities) which have a well defined visibility. For example, the global scope usually contains the core classes of the used language (like strings, integers, arrays) and other well known entities. A program either extends the global scope with its entities, or provides additional scopes. The entities of one scope are usually private, but can be made accessible from other scopes too. How new scopes are defined is dependent on and special to the used programming language: Scopes are created inside modules, classes, function declarations, or further expressions like control structures, iterations, or anonymous code blocks. The visibility of these scopes depends on the language: Typically, the methods and modules of classes are visible, but narrower scopes are not. In summary, a program consists of a set of scopes, each declaring their own set of entities.

Scopes are initially created by defining new entities. A declaration uses several expressions and statements to define a new scope - for example the definition of inner classes or methods. Declarations either happen intentionally as execution of manually written statements, or automatically by importing packages or modules from another scope, mix-in entities from modules, or copy entities with expressions from one scope to the current. After execution of the expressions, the scope will be populated with a number of entities, and is ready to be used in the program.

Read more at http://dsl-engineer.com/2010/05/14/understanding-metaprogramming-as-scope-manipulation-2.

blog comments powered by Disqus