2015-12-31 9 views
5

OCaml拡張ポイントについてもっと学びたいと思っており、ASTのレコードタイプの表現に続く問題があります。 ocamlc -dparsetree fool.mlのOCaml ASTレコードタイプの表現

let _ = [%getenv "USER"] 

そして出力:ソースファイル(foo.ml)を使用

http://whitequark.org/blog/2014/04/16/a-guide-to-extension-points-in-ocaml/

:私はこのブログの記事から以下の例を盗んでい

[ 
    structure_item (test.ml[1,0+0]..[1,0+24]) 
    Pstr_eval 
    expression (test.ml[1,0+8]..[1,0+24]) 
     Pexp_extension "getenv" 
     [ 
      structure_item (test.ml[1,0+17]..[1,0+23]) 
      Pstr_eval 
      expression (test.ml[1,0+17]..[1,0+23]) 
       Pexp_constant Const_string("USER",None) 
     ] 
    ] 

asttypes.mliとparsetree.mliから、私は解析に従うことができますツリーのパターンマッチング

Pexp_constant Const_string("USER",None) 

しかし、パーズツリーがレコードタイプを表すとき、私はもはや何が起こっているのかはわかりません。レコードフィールドは型定義に現れる順序と同じ順序では表現されず、すべてのフィールドが構文解析ツリーに必要(または表示)されていないようです。

parsetree.mliから:

type expression = { 
    pexp_desc: expression_desc; 
    pexp_loc: Location.t; 
    pexp_attributes: attributes; 
} 

解析木のみ出力場所とペイロードを示しているようだが、私はおそらく間違ってこれを読んでいます。

レコードタイプのASTを正しく読み取るにはどうすればよいですか?型発現のために、それは次のようになります。

(* record type declaration and pexp_loc field *) 
expression (test.ml[1,0+8]..[1,0+24]) 
    (* pexp_desc field *) 
    Pexp_extension "getenv" 
    [ 
     ... 
    ] 
+1

質問がわかりません。 pexp_descはASTの実際の記述です(これは大きな和型です)。 -dsourcetreeは単なる略図であり、ASTの実際のOCaml値ではありません。 – Drup

答えて

2

あなたがのASTを研究し、拡張ポイントを使用することが不可欠なツールが不足しているようです。これらのツールは、アラン・フリッシュによって書かれたppx_toolsです。これらのツールの1つは、ASTの具体的な表現を探求するように正確に設計されています。その名前はdumpastです。次のファイルast_record.mliに適用してみましょう:

type card = { 
    name: string; 
    address: string; 
    age: int; 
} 

出力はレコードレーベルの順序が保持されていることを確認し

ocamlfind ppx_tools/dumpast ast_record.mli   
ast_record.mli 
==> 
[{psig_desc = 
    Psig_type 
    [{ptype_name = {txt = "card"}; ptype_params = []; ptype_cstrs = []; 
     ptype_kind = 
     Ptype_record 
     [{pld_name = {txt = "name"}; pld_mutable = Immutable; 
      pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "string"}, [])}}; 
     {pld_name = {txt = "address"}; pld_mutable = Immutable; 
      pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "string"}, [])}}; 
     {pld_name = {txt = "age"}; pld_mutable = Immutable; 
      pld_type = {ptyp_desc = Ptyp_constr ({txt = Lident "int"}, [])}}]; 
     ptype_private = Public; ptype_manifest = None}]}] 
========= 

です。

ところで、私はあなたがこれらのppx_tools多分またLWTに同梱されてPPX拡張のソースコードを勉強することをお勧めしましょう。彼らはかなり短く、非常に良く書かれており、インスピレーションの好きな源です。

関連する問題