Python_ast.h相关代码的详细介绍

开发 后端
咋Python_ast.h的相关实际应用中你如果对相关的代码或是实际相关应用操作过程中的具体操作有不解的地方,你就可以借鉴以下的文章对其进行了解。

在Python_ast.h中AST所用到的类型定义,我们以stmt_ty类型的相关代码为例来介绍Python_ast.h中AST所用到的类型,希望大姐在浏览完以下的文章会在Python_ast.h中AST的相关实际应用中有所收获。

AST所用到的类型均定义在Python_ast.h中,以stmt_ty类型为例:

 

  1. enum _stmt_kind {FunctionDef_kind=1ClassDef_kind=2Return_kind=3,  
  2. Delete_kind=4Assign_kind=5AugAssign_kind=6Print_kind=7,  
  3. For_kind=8While_kind=9If_kind=10With_kind=11,  
  4. Raise_kind=12TryExcept_kind=13TryFinally_kind=14,  
  5. Assert_kind=15Import_kind=16ImportFrom_kind=17,  
  6. Exec_kind=18Global_kind=19Expr_kind=20Pass_kind=21,  
  7. Break_kind=22Continue_kind=23};  
  8. struct _stmt {  
  9. enum _stmt_kind kind;  
  10. union {  
  11. struct {  
  12. identifier name;  
  13. arguments_ty args;  
  14. asdl_seq *body;  
  15. asdl_seq *decorators;  
  16. } FunctionDef;  
  17. struct {  
  18. identifier name;  
  19. asdl_seq *bases;  
  20. asdl_seq *body;  
  21. } ClassDef;  
  22. struct {  
  23. expr_ty value;  
  24. } Return;  

 

 

过长,中间从略

 

 

  1. struct {  
  2. expr_ty value;  
  3. } Expr;  
  4. } v;  
  5. int lineno;  
  6. int col_offset;  
  7. };  
  8. typedef struct _stmt *stmt_ty;      

stmt_ty是语句结点类型,实际上是_stmt结构的指针。_stmt结构比较长,但有着很清晰的Pattern:

1. 第一个Field为kind,代表语句的类型。_stmt_kind定义了_stmt的所有可能的语句类型,从函数定义语句,类定义语句直到Continue语句共有23种类型。

2. 接下来是一个union v,每个成员均为一个struct,分别对应_stmt_kind中的一种类型,如_stmt.v.FunctionDef对应了_stmt_kind枚举中的FunctionDef_Kind,也就是说,当_stmt.kind == FunctionDef_Kind时,_stmt.v.FunctionDef中保存的就是对应的函数定义语句的具体内容。#t#

3. 其他数据,如lineno和col_offset

大部分AST结点类型均是按照类似的pattern来定义的,不再赘述。除此之外,另外有一种比较简单的AST类型如operator_ty,expr_context_ty等,由于这些类型仍以_ty结尾,因此也可以认为是AST的结点,但实际上,这些类型只是简单的枚举类型,并非指针。因此在以后的文章中,并不把此类AST类型作为结点看待,而是作为简单的枚举处理

以上就是对AST所用到的类型均定义在Python_ast.h中,以stmt_ty类型为例相关的内容的介绍,以及其具有清晰的Pattern的具体体现,忘你会有所收获。

责任编辑:佚名 来源: 互联网
相关推荐

2010-03-25 13:42:37

Python CST

2010-03-25 16:51:12

Python程序

2010-03-25 11:30:25

2010-03-03 17:21:42

Python操作注册表

2010-03-19 14:44:30

Python模块级函数

2009-12-25 16:51:37

ADO参数

2009-07-07 17:01:09

MyServlet

2010-03-23 16:16:21

Python文件

2009-12-25 09:37:18

ASP.NETWeb

2010-03-18 14:55:17

Python数据库连接

2010-03-29 17:37:17

Nginx resin

2010-04-08 09:27:44

Oracle 安装脚本

2009-11-30 11:14:57

VS2003 WebS

2010-03-18 14:27:53

Java Thread

2010-03-25 10:13:03

Python代码

2010-03-25 12:50:45

Python代码

2010-03-26 16:17:24

Python嵌入

2010-03-25 12:33:14

Python脚本文件

2010-03-23 08:56:38

Python随机数模块

2010-01-05 09:36:40

ADO超时
点赞
收藏

51CTO技术栈公众号