用C語言模仿Python函數

首先得說明一點,C 語言不是函數式編程語言,要想進行徹底的函數式編程,還得先寫個虛擬機,而後再寫個解釋器才行(至關於 CPython )。編程

下面咱們提供一個例子,說明即使不寫虛擬機, C 語言函數也是能夠「適度地模仿」 Python 函數。app

咱們有以下的 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函數:ide

  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)都能經過簡單的測試。可是能夠看到,僅僅是簡單的模擬,咱們也得用到大量(按比例)的彙編,可讀性不好,並且模擬程度很是有限,代碼長度也更長。函數式編程

注意:這只是個玩具,別這麼寫代碼,除非想搞破壞(充斥着各類漏洞,極易被侵入)。函數

相關文章
相關標籤/搜索