From: Yusuke Endoh Date: 2009-12-08T03:46:08+09:00 Subject: [ruby-dev:39853] [Feature #2455] --dump=parsetree, --dump=parsetree_with_comment Feature #2455: --dump=parsetree, --dump=parsetree_with_comment http://redmine.ruby-lang.org/issues/show/2455 起票者: Yusuke Endoh ステータス: Open, 優先度: Normal 担当者: Yukihiro Matsumoto, カテゴリ: core, Target version: 1.9.x 遠藤です。 Ruby をデバッグする際、node の構造をざっと把握したい場合がしばしば あります。 --dump=insns にならって、--dump=parsetree を入れてもいいでしょうか。 $ ./ruby --dump=parsetree -e '1 + 2 + 3' ############################################### ## Do NOT use this node dump for any purpose ## ## other than debug and research. ## ############################################### # @ NODE_SCOPE (line: 1) # +- nd_tbl: (empty) # +- nd_args: # | (null node) # +- nd_body: # @ NODE_CALL (line: 1) # +- nd_mid: :+ # +- nd_recv: # | @ NODE_CALL (line: 1) # | +- nd_mid: :+ # | +- nd_recv: # | | @ NODE_LIT (line: 1) # | | +- nd_lit: 1 # | +- nd_args: # | @ NODE_ARRAY (line: 1) # | +- nd_alen: 1 # | +- nd_head: # | | @ NODE_LIT (line: 1) # | | +- nd_lit: 2 # | +- nd_next: # | (null node) # +- nd_args: # @ NODE_ARRAY (line: 1) # +- nd_alen: 1 # +- nd_head: # | @ NODE_LIT (line: 1) # | +- nd_lit: 3 # +- nd_next: # (null node) $ ./ruby --dump=parsetree_with_comment -e '1 + 2 + 3' ############################################### ## Do NOT use this node dump for any purpose ## ## other than debug and research. ## ############################################### # @ NODE_SCOPE (line: 1) # | # [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body # +- nd_tbl (local table): (empty) # +- nd_args (arguments): # | (null node) # +- nd_body (body): # @ NODE_CALL (line: 1) # | # [nd_mid]([nd_args]) # +- nd_mid (method id): :+ # +- nd_recv (receiver): # | @ NODE_CALL (line: 1) # | | # [nd_mid]([nd_args]) # | +- nd_mid (method id): :+ # | +- nd_recv (receiver): # | | @ NODE_LIT (line: 1) # | | | # [nd_lit](literal) # | | +- nd_lit (literal): 1 # | +- nd_args (arguments): # | @ NODE_ARRAY (line: 1) # | | # [ [nd_head], [nd_next].. ] (length: [nd_alen]) # | +- nd_alen (length): 1 # | +- nd_head (element): # | | @ NODE_LIT (line: 1) # | | | # [nd_lit](literal) # | | +- nd_lit (literal): 2 # | +- nd_next (next element): # | (null node) # +- nd_args (arguments): # @ NODE_ARRAY (line: 1) # | # [ [nd_head], [nd_next].. ] (length: [nd_alen]) # +- nd_alen (length): 1 # +- nd_head (element): # | @ NODE_LIT (line: 1) # | | # [nd_lit](literal) # | +- nd_lit (literal): 3 # +- nd_next (next element): # (null node) Ruby のデバッグ以外にも、Ruby のコードに習熟していない人が Ruby の コードを読む際の助けになると思います。 私が昔 compile.c を読み始めていたころを思い出すと、node の構造を 理解していないことが大きな障害になっていました。(ドキュメントはなく、 nd_head, nd_body などの名前が様々な意味で転用・乱用されているので) 「デバッグと研究目的以外に使うな」と明示することで、node が非公開 API であることを再度主張する効果もあるかもしれません。 -- Yusuke Endoh ---------------------------------------- http://redmine.ruby-lang.org