用C语言模仿Python函数

2018-06-18 04:03:36来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

下面我们提供一个例子,说明即便不写虚拟机, C 语言函数也是可以“适度地模仿” Python 函数。

我们有如下的 Python 程序:

1 def line_conf(a, b):
2     def line(x):
3         return a*x + b
4     return line
5 
6 line1 = line_conf(1, 1)
7 line2 = line_conf(4, 5)
8 print(line1(5), line2(5))
Python Code

 

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

  1 /* MIT License
  2 
  3 Copyright (c) 2017 Yuandong-Chen
  4 
  5 Permission is hereby granted, free of charge, to any person obtaining a copy
  6 of this software and associated documentation files (the "Software"), to deal
  7 in the Software without restriction, including without limitation the rights
  8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9 copies of the Software, and to permit persons to whom the Software is
 10 furnished to do so, subject to the following conditions:
 11 
 12 The above copyright notice and this permission notice shall be included in all
 13 copies or substantial portions of the Software.
 14 
 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 21 SOFTWARE. */
 22 
 23 ///////////////////////////////////////////////////////////////////////////////
 24 
 25 // Note: The C program is almost equivalent to the Python program as follows:
 26 // def line_conf(a, b):
 27 //     def line(x):
 28 //         return a*x + b
 29 //     return line
 30 //
 31 // line1 = line_conf(1, 1)
 32 // line2 = line_conf(4, 5)
 33 // print(line1(5), line2(5))
 34 
 35 #include <stdio.h> 
 36 #include <stdlib.h> 
 37 #include <unistd.h> 
 38 #include <stdarg.h>
 39 
 40 typedef int Func();
 41 
 42 Func *line_conf(int x, int y,...)
 43 { 
 44     va_list ap; 
 45     va_start(ap, y);
 46 
 47     asm volatile(
 48         "push %%eax\n\t"
 49         "subl $40, %%esp\n\t"
 50         "movl 8(%%ebp), %%eax\n\t"
 51         "movl %%eax, -36(%%ebp)\n\t"
 52         "movl 12(%%ebp), %%eax\n\t"
 53         "movl %%eax, -40(%%ebp)\n\t"
 54         "addl $40, %%esp\n\t"
 55         "pop %%eax\n\t"
 56         :::"memory"
 57         );
 58 
 59 if(va_arg(ap,int) == 1){
 60 
 61 LINE:
 62 
 63     asm volatile(
 64         "push %%ebp\n\t"
 65         "movl %%esp, %%ebp\n\t"
 66         "movl 8(%%ebp), %%eax\n\t"
 67         "imul -36(%%ebp), %%eax\n\t"
 68         "addl -40(%%ebp), %%eax\n\t"
 69         "movl %%ebp, %%esp\n\t"
 70         "pop %%ebp\n\t"
 71         "ret\n\t"
 72         :::"memory","%eax"
 73         );
 74 }   
 75 __END:  
 76     va_end(ap);
 77     return (Func *)(&&LINE);
 78 }
 79 
 80 int main(int argc, const char *argv[]){  
 81     printf("====TEST START====\n");
 82     printf("34*234+6 ?= %d\n",line_conf(34,6)(234));
 83     printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d\n",line_conf(1,2)(3),line_conf(324,3)(65),line_conf(13,2)(66));
 84 
 85     int fd = line_conf(1,6)(4);
 86     Func *fun = line_conf(3,3);
 87     int a = 1;  // Limited point
 88     printf("3*3+3 ?= %d; 1*4+6 ?= %d\n",fun(3),fd);
 89     printf("====TEST END====\n");
 90     return 0;  
 91 }
 92 
 93 // Compile it by the following command:
 94 // gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out
 95 // The terminal output should looks like:
 96 // ====TEST START====
 97 // 34*234+6 ?= 7962
 98 // 1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 860
 99 // 3*3+3 ?= 12; 1*4+6 ?= 10
100 // ====TEST END====
101 //Note: The limitation happens between line 86 and line 88, we cannot insert any function here
102 // whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)
C Code

结果在MacOSX和Ubuntu上(i386)都能通过简单的测试。但是可以看到,仅仅是简单的模拟,我们也得用到大量(按比例)的汇编,可读性很差,而且模拟程度非常有限,代码长度也更长。

注意:这只是个玩具,别这么写代码,除非想搞破坏(充斥着各种漏洞,极易被侵入)。

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C - 基础 - 函数/数组/指针

下一篇:c语言-单链表(一)