Metaprogramming

Metaprogramming

https://en.wikipedia.org/wiki/Metaprogramming

愿编程, 是一中编程技术, 制造的计算机程序,具有能力, 对待程序为他们的数据。

针对程语言的编程技术。

Metaprogramming is a programming technique in which computer programs have the ability to treat programs as their data. It means that a program can be designed to read, generate, analyse or transform other programs, and even modify itself while running.[1][2] In some cases, this allows programmers to minimize the number of lines of code to express a solution, thus reducing the development time.[3] It also allows programs greater flexibility to efficiently handle new situations without recompilation.

Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the attribute-oriented programming language. The ability of a programming language to be its own metalanguage is called reflection or "reflexivity".[4] Reflection is a valuable language feature to facilitate metaprogramming.

Metaprogramming was popular in the 1970s and 1980s using list processing languages such as LISP. LISP hardware machines were popular in the 1980s and enabled applications that could process code. They were frequently used for artificial intelligence applications.

例子

A simple example of a metaprogram is this POSIX Shell script, which is an example of generative programming:

#!/bin/sh
# metaprogram
echo ‘#!/bin/sh‘ > program
for i in $(seq 992)
do
    echo "echo $i" >> program
done
chmod +x program

Uses in programming languages

Macro systems

Metaclasses

Metaclasses are provided by the following programming languages:

Template metaprogramming

Main article: Template metaprogramming

Staged metaprogramming

With dependent types

  • Usage of dependent types allows proving that generated code is never invalid.[12] However, this approach is bleeding-edge and is rarely found outside of research programming languages.

以此理解: C预处理、 lex yacc解析脚本程序 , 都是针对语言的编程。

Lua Macros

http://lua-users.org/wiki/LuaMacros

Implementing List Comprehensions

In PythonLists, FabienFleutot discusses a list comprehension syntax modelled on the Python one.

x = {i for i = 1,5}

{1,2,3,4,5}

Such a statement does not actually require much transformation to be valid Lua. We use anonymous functions:

x = (function() local ls={}; for i = 1,5 do ls[#ls+1] = i end; return ls end)()

However, to make it work as a macro, we need to choose a name (here ‘L‘) since we cannot look ahead to see the `for` token.

macro.define(‘L‘,{‘expr‘,‘loop_part‘,handle_parms=true},
    ‘ ((function() local t = {}; for loop_part do t[#t+1] = expr end; return t end)()) ‘,
    function(ls)
        local get = ls.getter
        local line,t = get()
        if t ~= ‘{‘ then macro.error("syntax: L{<expr> for <loop-part>}") end
        local expr = macro.grab_parameters(‘for‘)
        local loop_part = macro.grab_parameters(‘}‘,‘‘)
        return expr,loop_part
    end)

The substitution is pretty straightforward, but we have grab the parameters with a custom function. The first call to macro.grab_parameters grabs upto ‘for‘, and the second grabs upto ‘}‘. Here we have to be careful that commas are not treated as delimiters for this grab by setting the second argument to be the empty string.

Any valid for-loop part can be used:

 L{{k,v} for k,v in pairs{one=1,two=2}}

 { "one", 1 }, { "two", 2 } }

Nested comprehensions work as expected:

x = L{L{i+j for j=1,3} for i=1,3}

{ { 2, 3, 4 }, { 3, 4, 5 }, { 4, 5, 6 } }

A particularly cool idiom is to grab the whole of standard input as a list, in one line:

lines = L{line for line in io.lines()}

原文地址:https://www.cnblogs.com/lightsong/p/8491132.html

时间: 2024-11-08 02:04:32

Metaprogramming的相关文章

Template Metaprogramming with Modern C++: Introduction

Template Metaprogramming with Modern C++: Introduction Posted on September 10, 2014 by Manu Sánchez Template Metaprogramming with Modern C++: Introduction Any sufficiently complex C++ code is indistinguishable from trolling Arthur C. Clarke Preface T

CppCon - Modern Template Metaprogramming 杂记

2014年底才看到github和channel9上有CppCon2014的视频和资料,顿时激动不已.最近小生也一直在研习CppCon2014中令人兴奋的内容.这篇鄙文就是小生学习了<Modern Template Metaprogramming>之后,有对Unevaluated Operands更深刻的理解,有感而写. C++98标准中的Unevaluated Operands,只有sizeof操作符.C++11又引入了decltype,typeid和noexcept.Unevaluated

metaprogramming笔记

动态多态与静态多态 动态多态:允许我们通过单个基类指针或引用处理多个派生类型的对象. 模板元编程中强调静态多态,允许不同类型的对象以同样的方式被操纵,只要它们支持某种共通的语法即可. 动态多态,连同"延迟绑定"或"运行期派发",是面向对象程序的关键特性.静态多态(也称参数化多态),是泛型编程的本质要素. 在面向对象的编程文献中将一个带有大量成员的类称作blob.类的成员彼此"耦合",因为它们必须被声明在一起.为了避免耦合并提高模块化程度,应该避免

[Javascript] MetaProgramming: new.target

new.target is a new “magical” value available in all functions, thoughin normal functions it will always be undefined. In any constructor,new.target always points at the constructor that new actuallydirectly invoked. class Parent { constructor() { if

[Javascript] MetaProgramming: function name

Each function should have a 'name' property. It can be anonymous, empty, the same as function name, or class name. For example: const foo = function(){} console.log(foo.name); //foo function(){ .. }); // name: (function*(){ .. }); // name: window.foo

[Core Javascirpt] Basic Metaprogramming: Dynamic Method

Somehow it looks like reflect in Java. For example: We define an mothod on the Object, it called defineMethod(). It accepts two arguements, one is methodName andother is methodBody. Read More: https://developer.mozilla.org/en/docs/Web/JavaScript/Refe

RoR - MetaProgramming

ruby是动态语言,它有动态语言的优势与劣势 动态语言,像python与ruby 你不用提前去定义method - they need to only be "found" when invoked calling method dynamically: class Dog def bark puts "Woof, woof!' end def greet(greeting) puts greeting end end dog = Dog.new dog.bark # =&g

[转载]那些C++牛人的博客

现整理收集C++世界里那些“牛人”的个人博客.凡三类:一是令人高山仰止的大牛,对C++语言本身产生过深远的影响的人:二是C++运用炉火纯青的高手,有原创性的技术干货:三是中文世界里的C++牛人. C++大牛的博客 Bjarne Stroustrup的博客: Bjarne Stroustrup’s Homepage Bjarne Stroustrup,不认识的可以去面壁了,没有他,就没有我们现在的饭碗.Bjarne Stroustrup是丹麦人,目前任教于TAMU.他的Homepage和他的书籍T

Effective C++

01.视C++为一个语言联邦 C Object-Oriented C++ Template C++ STL 过程形式(procedural),面向对象形式(object-oriented),函数形式(functional),泛型形式(generic),元编程形式(metaprogramming) 问题: 泛型形式和元编程形式的区别? 过程形式和函数形式的区别? 结论: 对于内置类型而言pass-by-value通常比pass-by-reference高效. 02.尽量以const,enum,in