简介
出于个人爱好和某种需求,我再16年对python的解释器产生了浓厚兴趣,并且下定决心重新实现一个版本。我个人再游戏服务器开发中,对c++嵌入lua和python都有着丰富应用经验,自认为对二者的优劣有着深刻的理解。
python针对lua的最大优势是python是完备的程序语言,类、模块包括丰富的库和方便好用的字符串操作,可以说python用来实现功能会优雅很多,而lua最大的优势就是小巧高效,另外lua的lua_state是可以有多个实例的,这样就可以多线程使用lua(一个线程单独一个lua_state),而python解释器因为有全局解释器锁,所以无法实现多python解释器实例。
考虑到在嵌入python的应用场景中,所用到python的功能都是比较简单通用的功能,比如类、模块,函数,一些复杂的类库也不常用,所以我就想实现一个不使用全局解释器锁,可以有多个python解释器锁的解释器。所以16年底,我自己实现了一下python解释器第一版,第一版是使用AST虚拟语法树直接解析的,虽然做了必要的优化,但是性能。。。。仍然不忍直视。
平常我一直吐槽python跑的没有lua快,但是吐槽是一码事,自己实现真的就是另一码事了。我仔细分析了第一版性能低的原因是选错了路!python的虚拟机是讲语法树翻译成ByteCode,然后有个Virtual Machine不断的解释bytecode,而vm的运行又分堆栈模式和寄存器模式,python就是堆栈模式的,而lua是寄存器模式的,寄存器模式是现在的趋势,这也是lua跑到更快的重要原因。我的第一版VM用AST直接跑,选错了路,无论如何也太快不了。
但是我仍然把这个第一版打了个分支,分享出来,因为当我实现用寄存器模式的VM的时候,感觉无论如何也无法设计的像AST直接解析的VM那样优雅、直接。AST直接解析的方式真的太直观了,虽然效率很低,但是其仍然有很大的应用价值。
比如protocolbuff、thrift这些通过定义语法文件生成代码的这类工具,对语法解析的效率要求不高,那么这个版本的VM再这些领域还是有很大的参考价值。
内部实现层次:
Python BNF
一提到实现脚本解释器,估计很多人都会挠头,不知道从何入手。刚开始我也是这样,我把大学里的编译原理从床底下一堆打入冷宫的数量翻出来,一顿猛看。但是仍然没有找到很大头绪,后来我就在python.org上一顿逛,也下载了python的源码分析,源码目录有python的BNF描述文件,因为我已经看过一遍编译原理了,BNF就看的很懂,从头到尾读了一遍了以后,灵光乍现啊!BNF就是完整的解析python语法的流程说明啊!截取一小段做个说明:
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] while_stmt: 'while' test ':' suite ['else' ':' suite] for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] try_stmt: ('try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)) with_stmt: 'with' with_item (',' with_item)* ':' suite with_item: test ['as' expr] # NB compile.c makes sure that the default except clause is last except_clause: 'except' [test [('as' | ',') test]] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
简单解释下,python的Grammar BNF是从顶之下递归描述的。上面最上边定义的是compound_stmt复杂语句,而compound_stmt有if、while、for、try、with、函数定义、类定义、修饰器定义几种,
下面紧接着定义了if语句if_stmt的语法规则,这样在c++实现解析python语法的时候,就可以从顶向下按照这个BNF尝试解析,如果不满足这个BNF语法要求的就报错。我为了生成跟这个BNF一致的代码结构,写了个python脚本解析这个BNF自动生成C++的解析函数。生成的C++代码示例如下:
class Parser{ public: ExprASTPtr parse(Scanner& scanner); //! single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE ExprASTPtr parse_single_input(); //! file_input: (NEWLINE | stmt)* ENDMARKER ExprASTPtr parse_file_input(); //! eval_input: testlist NEWLINE* ENDMARKER ExprASTPtr parse_eval_input(); //! decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE ExprASTPtr parse_decorator(); //! decorators: decorator+ ExprASTPtr parse_decorators(); //! decorated: decorators (classdef | funcdef) ExprASTPtr parse_decorated(); //! funcdef: 'def' NAME parameters ':' suite ExprASTPtr parse_funcdef(); //! parameters: '(' [varargslist] ')' ExprASTPtr parse_parameters(); //! varargslist: ((fpdef ['=' test] ',')* //! fpdef ['=' test] (',' fpdef ['=' test])* [',']) ExprASTPtr parse_varargslist(); //! fpdef: NAME | '(' fplist ')' ExprASTPtr parse_fpdef(); //! fplist: fpdef (',' fpdef)* [','] ExprASTPtr parse_fplist(); //! stmt: simple_stmt | compound_stmt ExprASTPtr parse_stmt(); //! simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE ExprASTPtr parse_simple_stmt(); //! small_stmt: (expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt | //! import_stmt | global_stmt | exec_stmt | assert_stmt) ExprASTPtr parse_small_stmt(); //! expr_stmt: testlist (augassign (yield_expr|testlist) | ExprASTPtr parse_expr_stmt(); .................................