123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184(* Copyright 2018-present Cornell University
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*)moduleSMap=Map.Make(String)typehas_params=booltypetid=Empty|Localofstring|Globalofstringtypeident_kind=|TypeNameofhas_params*namespace|Identofhas_params*tidandnamespace=ident_kindSMap.ttypet=namespacelist(* Current context, stored as a mutable global variable *)letcontext:tref=ref[SMap.empty]letbackup:tref=ref[](* Previously looked-up identifier *)letprevious_id:stringoptionref=refNone(* Namespace of a member's parent *)letparent_namespace:namespaceoptionref=refNone(* Resets context *)letreset()=context:=[SMap.empty];backup:=[];previous_id:=None;parent_namespace:=None(* Associates [id] with [k] in map for current scope *)letdeclare(id:string)(k:ident_kind):unit=match!contextwith|[]->failwith"ill-formed context"|m::l->context:=SMap.addidkm::lletdeclare_typeidhas_params=declareid(TypeName(has_params,SMap.empty))letdeclare_typestypes=List.iter(funs->declare_typesfalse)typesletdeclare_var?(tid=Empty)idhas_params=declareid(Ident(has_params,tid))letdeclare_varsvars=List.iter(funs->declare_varsfalse)varsletfind_opt(id:string)(ctx:t):ident_kindoption=letrecloop=function|[]->None|m::rest->(matchSMap.find_optidmwithNone->looprest|Somek->Somek)inloopctxletfind_type_opt(id:string)(ctx:t):(has_params*namespace)option=letrecloop=function|[]->None|m::rest->(matchSMap.find_optidmwith|Some(TypeName(has_params,namespace))->Some(has_params,namespace)|_->looprest)inloopctxletfind_var_opt(id:string)(ctx:t):(has_params*tid)option=letrecloop=function|[]->None|m::rest->(matchSMap.find_optidmwith|Some(Ident(has_params,tid))->Some(has_params,tid)|_->looprest)inloopctx(* Tests whether [id] is known as a type name. *)letget_kind(id:string):ident_kind=letctx=match!parent_namespacewithNone->!context|Somens->[ns]inletkind=matchfind_optidctxwithNone->Ident(false,Empty)|Somek->kinprevious_id:=Someid;kindletis_typename(id:string):bool=matchget_kindidwithTypeName_->true|_->false(* Takes a snapshot of the current context. *)letpush_scope()=context:=SMap.empty::!context(* Remove scope *)letpop_scope()=match!contextwith|[]->failwith"ill-formed context"|[_]->failwith"pop would produce ill-formed context"|s::l->context:=l;sletgo_toplevel()=letrecloopc=matchcwith|[]->failwith"ill-formed context"|[_]->context:=c|_::l->looplinbackup:=!context;loop!contextletgo_local()=context:=!backupletget_global_context()=letrecloopc=matchcwith|[]->failwith"ill-formed context"|[_]->c|_::l->looplinloop!contextletset_type_namespace(tid:string)(ns:namespace)=letrecloop=function|[]->[]|m::rest->(matchSMap.find_opttidmwith|Some(TypeName(has_params,_))->SMap.addtid(TypeName(has_params,ns))m::rest|_->m::looprest)incontext:=loop!contextletset_parent_namespace()=let(let*)=Option.bindinletnamespace=let*parent_id=!previous_idinlet*_,tid=find_var_optparent_id!contextinlet*tid,ctx=matchtidwith|Empty->None|Localtid->Some(tid,!context)|Globaltid->Some(tid,get_global_context())inOption.mapsnd(find_type_opttidctx)inletnamespace=Option.valuenamespace~default:SMap.emptyinparent_namespace:=Somenamespaceletclear_parent_namespace()=parent_namespace:=None(* Printing functions for debugging *)letprint_entryxk=matchkwith|TypeName(true,_)->Printf.printf"%s : type<...>"x|TypeName(false,_)->Printf.printf"%s : type"x|Ident(true,_)->Printf.printf"%s : ident<...>"x|Ident(false,_)->Printf.printf"%s : ident"xletprint_mapm=SMap.iter(funxk->print_entryxk;print_endline"")mletprint_context()=List.iter(funm->print_mapm;print_endline"----")!context