在 node.h 內可以看到 struct 定義。第一個問題是要知道如何解讀 flags
。
typedef struct RNode {
VALUE flags;
union {
struct RNode *node;
ID id;
VALUE value;
rb_ast_id_table_t *tbl;
} u1;
union {
struct RNode *node;
ID id;
long argc;
VALUE value;
} u2;
union {
struct RNode *node;
ID id;
long state;
struct rb_args_info *args;
struct rb_ary_pattern_info *apinfo;
struct rb_fnd_pattern_info *fpinfo;
VALUE value;
} u3;
rb_code_location_t nd_loc;
int node_id;
} NODE;
typedef struct rb_ast_body_struct {
const NODE *root;
VALUE compile_option;
VALUE script_lines;
// script_lines is either:
// - a Fixnum that represents the line count of the original source, or
// - an Array that contains the lines of the original source
} rb_ast_body_t;
typedef struct rb_ast_struct {
VALUE flags;
node_buffer_t *node_buffer;
rb_ast_body_t body;
} rb_ast_t;
先從 process_options
看,它有 delcare rb_ast_t *ast=0
與 const rb_iseq_t *iseq
,可以看到在 C code 裡,已經有使用 ruby array, hash, etc. 等 class。ast
的值有兩種來源:
ast = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
...
ast = load_file(parser, opt->script_name, f, 1, opt);
iseq 的來源:
iseq = rb_iseq_new_main(&ast->body, opt->script_name, path, vm_block_iseq(base_block), !(dump & DUMP_BIT(insns_without_opt)));
最後會 return (VALUE)iseq;