Parsetree中定义的常量会覆盖Asttypes中定义的常量吗?

时间:2018-10-16 09:54:45

标签: compiler-construction ocaml

我发现Asttypes和Parsetree都定义了类型常量:

Parsetree:

type constant = 
| Pconst_integer of string * char option 
| Pconst_char of char 
| Pconst_string of string * string option 
| Pconst_float of string * char option 

Asttypes:

type constant = 
| Const_int of int 
| Const_char of char 
| Const_string of string * string option 
| Const_float of string 
| Const_int32 of int32 
| Const_int64 of int64 
| Const_nativeint of nativeint 

并且Parsetree将在ocaml / parsing / parsetree.mli中打开模块Asttypes:

open Asttypes

那么Parsetree中定义的常量会覆盖Asttypes中定义的常量吗?

我有这个测试程序:

let ()=
 let filename = "/home/wk/prog/LocationTest/c.ml" in
 Location.input_name := filename ;
 let readhandle = open_in filename in
 let buf = Lexing.from_channel readhandle in
 Location.init buf filename ;
 let ast = Parse.implementation buf in
 Printf.printf "%d" buf.lex_buffer_len;
 let a=(List.nth ast 0).pstr_desc in
 match a with
 |Pstr_eval (x,y)->
  match x.pexp_desc with
  |Pexp_constant z->
    match z with 
    |Pconst_integer (x,y)->
     Printf.printf "%d" x;

c.ml只有一行,定义了一个数字

该程序无法工作,编译器告诉我它需要类型为Asttypes.constant

如果我将最后两行更改为:

|Const_int q->
   Printf.printf "%d" q;

它工作正常,并以c.ml显示数字

1 个答案:

答案 0 :(得分:2)

它不会覆盖它,但是会使其阴影。因此,这两种类型对于编译器仍然是已知的,并且仍然存在,但是,当您使用不合格的constant时,它将引用在最后打开的模块中定义的类型构造函数。基本上,open语句仅启用不合格的访问。您仍然可以从其他模块访问值和类型,只要您使用模块名称来限制它们的名称,例如Asttypes.constantParsetree.constant。构造函数(例如Asttypes.Const_int,值,模块,类和模块中定义的其他项)也是如此。

相关问题