2017-02-24 39 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