I am writing a compiler that takes in my AST and outputs an OCaml AST. When compiling:
(List.length '(1 2 3))
to
List.length [1; 2; 3]
I get the following output 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
]
]
]
]
]
Upon inspection, this seems nearly identical to the output of ocamlc -dparsetree
on the above OCaml program, which compiles successfully.
Instead, my program does not compile with the following error:
Error: Unbound value List.length
What am I doing wrong?
Wild guess: You are building a Lident "List.length"
, which is wrong, Lident
are only unqualified identifiers. You should use Longident.parse "List.length"
which will give you Ldot (Lident "List", "length")
Side note: you should really output better locations. ;)