手工创建的 OCaml AST 中的未绑定值
Unbound value in hand-created OCaml AST
我正在编写一个编译器,它接受我的 AST 并输出一个 OCaml AST。编译时:
(List.length '(1 2 3))
到
List.length [1; 2; 3]
我得到以下输出:
[
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
]
]
]
]
]
经检查,这似乎与上述 OCaml 程序中 ocamlc -dparsetree
的输出几乎相同,后者编译成功。
相反,我的程序没有编译并出现以下错误:
Error: Unbound value List.length
我做错了什么?
胡乱猜测:您正在构建一个 Lident "List.length"
,这是错误的,Lident
只是不合格的标识符。你应该使用 Longident.parse "List.length"
这会给你 Ldot (Lident "List", "length")
旁注:你真的应该输出更好的位置。 ;)
我正在编写一个编译器,它接受我的 AST 并输出一个 OCaml AST。编译时:
(List.length '(1 2 3))
到
List.length [1; 2; 3]
我得到以下输出:
[
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
]
]
]
]
]
经检查,这似乎与上述 OCaml 程序中 ocamlc -dparsetree
的输出几乎相同,后者编译成功。
相反,我的程序没有编译并出现以下错误:
Error: Unbound value List.length
我做错了什么?
胡乱猜测:您正在构建一个 Lident "List.length"
,这是错误的,Lident
只是不合格的标识符。你应该使用 Longident.parse "List.length"
这会给你 Ldot (Lident "List", "length")
旁注:你真的应该输出更好的位置。 ;)