1 / 13

第 7 回 RHG の逆襲

第 7 回 RHG の逆襲. 2008/8/24 澤田 淳二. 今日のテーマ. 第 12 章 構文木の構築. if. 構文木とは. ↓ こんなのに対する. ↓ こんなの. if foo > 0 bar = 0 else bar = foo + 1 end. >. =. =. foo. 0. bar. 0. bar. +. foo. 1. NODE. flags にノードのタイプを格納 ノードのタイプによって共用体の使い方が決まる if の場合 u1 条件式 u2 真の本体 u3 偽の本体

kristy
Download Presentation

第 7 回 RHG の逆襲

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第7回 RHGの逆襲 2008/8/24 澤田 淳二

  2. 今日のテーマ • 第12章 構文木の構築

  3. if 構文木とは ↓こんなのに対する ↓こんなの if foo > 0 bar = 0 else bar = foo + 1 end > = = foo 0 bar 0 bar + foo 1

  4. NODE • flagsにノードのタイプを格納 • ノードのタイプによって共用体の使い方が決まる • ifの場合 • u1 条件式 • u2 真の本体 • u3 偽の本体 • 共用体メンバーのアクセスはマクロが用意されている typedef struct RNode { unsigned long flags; char *nd_file; union { struct RNode *node; … } u1; union { struct RNode *node; … } u2; union { struct RNode *node; … } u3; } NODE;

  5. ファイル名と行番号 • ファイル名 • nd_fileは使われなくなった • rb_iseq_tに格納されている • 行番号 • flagsの空き領域を使って行番号が埋め込まれている • Ruby1.8:13ビット • Ruby1.9:17ビットのはずだが、行番号65537が1になった??

  6. 構文木の構築 • yaccのアクションとして構文木を構築 primary : keyword_if expr_value then compstmt if_tail keyword_end { $$ = NEW_IF(cond($2), $4, $5); fixpos($$, $2); }

  7. 構文木表示ツール • ソースからどんな構文木ができるかはダンプしてみるのが一番 • nodedump • 1.8, 1.9ともに動かず… • ルビーの木(http://aikelab.net/rubytree/) • リストはフラットになる • 1.8では動いた • 1.9では動かず…

  8. 意味解析 • エラーチェックが主 • エラーの場合、yyerror()を呼び出す • 値が必要な場所に値を持たない式(value_expr()でチェック) • 同じ名前の引数 • etc… • 一部のエラーチェックはYARVコードへのコンパイル時に移動 • メソッド外return

  9. ローカル変数の処理 • パーサ解析中に定義 • ソース的に出てきたときに定義される if false lvar = “この代入は決して実行されない” end p lvar # nilと表示される

  10. ローカル変数のデータ構造 • Ruby1.8から構造が変更されている struct vtable { ID *tbl; int pos; int capa; struct vtable *prev; }; struct local_vars { struct vtable *args; struct vtable *vars; struct local_vars *prev; };

  11. ローカル変数操作関数 • ローカル変数関連関数 • local_push(), local_pop(), local_id(), local_tbl() • ブロック変数関連関数 • dyna_push(), dyna_pop(), dyna_in_block(), dvar_curr(), dvar_defined() • 変数追加 • arg_var(), local_var(), dyna_var() • vtable操作関数 • vtable_alloc(), vtable_free(), vtable_add(), vtable_included()

  12. データ構造図解(vtable) tbl id id pos capa

  13. データ構造図解(local_vars) ブロック変数 local_vars args vars prev lvtbl ブロック変数 vtable prev ローカル変数 vtable prev vtable prev vtable prev vtable prev vtable prev 一つ前のlvtbl

More Related