2017-02-24 49 views
2

我正在編寫一個編譯器,它接受我的AST並輸出OCaml AST。當編譯:手工創建的OCaml中的未綁定值AST

(List.length '(1 2 3)) 

List.length [1; 2; 3] 

我得到以下輸出AST:

[ 
    structure_item (_none_[1,0+-1]..[1,0+-1]) ghost 
    Pstr_eval 
    expression (_none_[1,0+-1]..[1,0+-1]) ghost 
     Pexp_apply 
     expression (_none_[1,0+-1]..[1,0+-1]) ghost 
     Pexp_ident "List.length" (_none_[1,0+-1]..[1,0+-1]) ghost 
     [ 
     <label> "" 
      expression (_none_[1,0+-1]..[1,0+-1]) ghost 
      Pexp_construct "::" (_none_[1,0+-1]..[1,0+-1]) ghost 
      Some 
       expression (_none_[1,0+-1]..[1,0+-1]) ghost 
       Pexp_tuple 
       [ 
        expression (_none_[1,0+-1]..[1,0+-1]) ghost 
        Pexp_constant Const_int 1 
        expression (_none_[1,0+-1]..[1,0+-1]) ghost 
        Pexp_construct "::" (_none_[1,0+-1]..[1,0+-1]) ghost 
        Some 
         expression (_none_[1,0+-1]..[1,0+-1]) ghost 
         Pexp_tuple 
         [ 
          expression (_none_[1,0+-1]..[1,0+-1]) ghost 
          Pexp_constant Const_int 2 
          expression (_none_[1,0+-1]..[1,0+-1]) ghost 
          Pexp_construct "::" (_none_[1,0+-1]..[1,0+-1]) ghost 
          Some 
           expression (_none_[1,0+-1]..[1,0+-1]) ghost 
           Pexp_tuple 
           [ 
            expression (_none_[1,0+-1]..[1,0+-1]) ghost 
            Pexp_constant Const_int 3 
            expression (_none_[1,0+-1]..[1,0+-1]) ghost 
            Pexp_construct "[]" (_none_[1,0+-1]..[1,0+-1]) ghost 
            None 
           ] 
         ] 
       ] 
     ] 
] 

經檢查,這似乎是幾乎相同的ocamlc -dparsetree上述OCaml的程序的輸出,編譯成功。

相反,我的程序不與下面的錯誤編譯:

Error: Unbound value List.length 

我在做什麼錯?

回答

4

瘋狂猜測:您正在構建一個Lident "List.length",這是錯誤的,Lident只是非限定標識符。你應該使用Longident.parse "List.length"這將給你Ldot (Lident "List", "length")

備註:你應該輸出更好的位置。 ;)

+0

你是完全正確的。我會看一看。 – tekknolagi

+0

關於輸出位置 - 不幸的是,我不支持AST。還沒。 – tekknolagi

+1

我會建議快速做到這一點。除此之外,調試類型錯誤將成爲一場噩夢。 :p – Drup