Ruby代码的解析、编译与执行机制揭秘
1. Ruby语法规则解析
1.1 示例代码与语法规则匹配
以简单的Ruby脚本 10.times do |n| puts n end
为例,Ruby的解析过程遵循一系列语法规则。以下是相关语法规则及对应代码示例:
| 语法规则 | 代码示例 |
| — | — |
| program: top_compstmt
| 匹配整个Ruby程序 |
| top_compstmt: top_stmts opt_terms
| - |
| top_stmts: ... \| top_stmt \| ...
| - |
| top_stmt: stmt \| ...
| - |
| stmt: ... \| expr
| - |
| expr: ... \| arg
| - |
| arg: ... \| primary
| - |
| primary: ... \| method_call brace_block \| ...
| 10.times do \|n\| puts n end
|
1.2 方法调用规则解析
-
method_call
规则 :