登录  /  注册

Python函数使用C语言模仿的实例

黄舟
发布: 2017-05-14 11:25:27
原创
1184人浏览过

下面小编就为大家带来一篇用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\n\t"
    "subl $40, %%esp\n\t"
    "movl 8(%%ebp), %%eax\n\t"
    "movl %%eax, -36(%%ebp)\n\t"
    "movl 12(%%ebp), %%eax\n\t"
    "movl %%eax, -40(%%ebp)\n\t"
    "addl $40, %%esp\n\t"
    "pop %%eax\n\t"
    :::"memory"
    );

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

LINE:

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

int main(int argc, const char *argv[]){ 
  printf("====TEST START====\n");
  printf("34*234+6 ?= %d\n",line_conf(34,6)(234));
  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));

  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\n",fun(3),fd);
  printf("====TEST END====\n");
  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语言的函数,而且模拟程度很高。

以上就是Python函数使用C语言模仿的实例的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号