视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Python函数使用C语言模仿的实例
2020-11-27 14:24:22 责编:小OO
文档


下面小编就为大家带来一篇用C语言模仿Python函数的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

首先得说明一点,C 语言不是函数式编程语言,要想进行完全的函数式编程,还得先写个虚拟机,然后再写个解释器才行(相当于 CPython )。

下面我们提供一个例子,说明 C 语言函数可以“适度地模仿” Python 函数。

我们有如下的 Python 程序:

def line_conf(a, b):
 def line(x):
 return a*x + b
 return line

line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5), line2(5))

我们在C程序中适度地模拟其中的line_conf函数:

/* MIT License

Copyright (c) 2017 Yuandong-Chen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */

///////////////////////////////////////////////////////////////////////////////

// Note: The C program is almost equivalent to the Python program as follows:
// def line_conf(a, b):
// def line(x):
// return a*x + b
// return line
//
// line1 = line_conf(1, 1)
// line2 = line_conf(4, 5)
// print(line1(5), line2(5))

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <stdarg.h>

typedef int Func();

Func *line_conf(int x, int y,...)
{ 
 va_list ap; 
 va_start(ap, y);

 asm volatile(
 "push %%eax
	"
 "subl $40, %%esp
	"
 "movl 8(%%ebp), %%eax
	"
 "movl %%eax, -36(%%ebp)
	"
 "movl 12(%%ebp), %%eax
	"
 "movl %%eax, -40(%%ebp)
	"
 "addl $40, %%esp
	"
 "pop %%eax
	"
 :::"memory"
 );

if(va_arg(ap,int) == 1){

LINE:

 asm volatile(
 "push %%ebp
	"
 "movl %%esp, %%ebp
	"
 "movl 8(%%ebp), %%eax
	"
 "imul -36(%%ebp), %%eax
	"
 "addl -40(%%ebp), %%eax
	"
 "movl %%ebp, %%esp
	"
 "pop %%ebp
	"
 "ret
	"
 :::"memory","%eax"
 );
} 
END: 
 va_end(ap);
 return (Func *)(&&LINE);
}

int main(int argc, const char *argv[]){ 
 printf("====TEST START====
");
 printf("34*234+6 ?= %d
",line_conf(34,6)(234));
 printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d
",line_conf(1,2)(3),line_conf(324,3)(65),line_conf(13,2)(66));

 int fd = line_conf(1,6)(4);
 Func *fun = line_conf(3,3);
 int a = 1; // Limited point
 printf("3*3+3 ?= %d; 1*4+6 ?= %d
",fun(3),fd);
 printf("====TEST END====
");
 return 0; 
}

// Compile it by the following command:
// gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out
// The terminal output should looks like:
// ====TEST START====
// 34*234+6 ?= 7962
// 1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 860
// 3*3+3 ?= 12; 1*4+6 ?= 10
// ====TEST END====
//Note: The limitation happens between line 86 and line 88, we cannot insert any function here
// whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)

结果在MacOSX和Ubuntu上(i386)都能通过简单的测试。但是可以看到,仅仅是简单的模拟,我们也得用到大量(按比例)的汇编,可读性很差,而且模拟程度非常有限,代码长度也更长。相反,对于这类一般功能的函数,Python可以很容易地模拟C语言的函数,而且模拟程度很高。

下载本文
显示全文
专题