Stay Hungry,Stay Foolish!

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

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()}

 

posted @   lightsong  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2016-03-01 tapping of data 词义来源
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示