File: build/library.ml (return to index)



Statistics:  
kind coverage
binding 27 / 29 (93 %)
sequence 5 / 5 (100 %)
for 0 / 0 (- %)
if/then 0 / 0 (- %)
try 1 / 1 (100 %)
while 0 / 0 (- %)
match/function 12 / 19 (63 %)
kind coverage
class expression 0 / 0 (- %)
class initializer 0 / 0 (- %)
class method 0 / 0 (- %)
class value 0 / 0 (- %)
toplevel expression 0 / 0 (- %)
lazy operator 0 / 0 (- %)



Source:

fold all unfold all
000001| (**
000002| This program is free software; you can redistribute it and / or modify
000003| it under the terms of the GNU General Public License as published by
000004| the Free Software Foundation; version 3 of the License.
000005|  
000006| This program is distributed in the hope that it will be useful,
000007| but WITHOUT ANY WARRANTY; without even the implied warranty of
000008| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
000009| GNU General Public License for more details.
000010|  
000011| Registration of libraries
000012|  
000013| @author Tony BenBrahim < tony.benbrahim at gmail.com >
000014|  
000015| *)
000016|  
000017| open Ast
000018| open Environment
000019|  
000020| (**
000021| Registers all library functions and params in an analysis environment
000022| @param env analysis environment
000023| @return a modified environment with all library functions registered
000024| *)
000025| let register_for_analysis env =
000026|         (*[2]*)let env = Environment.declare_variable_and_value env "args" (RMapValue(Hashtbl.create 10, ArraySubtype))
000027|         in (*[2]*)let env = Environment.declare_variable_and_value env "String" (RMapValue(Hashtbl.create 10, MapSubtype))
000028|         in (*[2]*)let env = Environment.declare_variable_and_value env "Integer" (RMapValue(Hashtbl.create 10, MapSubtype))
000029|         in (*[2]*)let h=Hashtbl.create 10 
000030|         in (*[2]*)let _ =Hashtbl.add h "infinity" (RFloatValue(infinity))
000031|         in (*[2]*)let _ =Hashtbl.add h "nan" (RFloatValue(nan))
000032|         in (*[2]*)let _ =Hashtbl.add h "negativeInfinity" (RFloatValue(neg_infinity))
000033|         in (*[2]*)let env = Environment.declare_variable_and_value env "Float" (RMapValue(h, MapSubtype))
000034|         in (*[2]*)let env = Environment.declare_variable_and_value env "Boolean" (RMapValue(Hashtbl.create 10, MapSubtype))
000035|         in (*[2]*)let env = Environment.declare_variable_and_value env "Function" (RMapValue(Hashtbl.create 10, MapSubtype))
000036|         in (*[2]*)let env = Environment.declare_variable_and_value env "Void" (RMapValue(Hashtbl.create 10, MapSubtype))
000037|         in (*[2]*)let env = Environment.declare_variable_and_value env "Nan" (RMapValue(Hashtbl.create 10, MapSubtype))
000038|         in (*[2]*)let env = Environment.declare_variable_and_value env "Array" (RMapValue(Hashtbl.create 10, MapSubtype))
000039|         in (*[2]*)let env = Environment.declare_variable_and_value env "Map" (RMapValue(Hashtbl.create 10, MapSubtype))
000040|         in (*[2]*)let env = Environment.declare_variable_and_value env "void" RVoid
000041|         in (*[2]*)let rec define_map_lib_call h libdef = function
000042|                 | name::[] -> (*[84]*)Hashtbl.replace h name libdef
000043|                 | name:: tl ->
000044|                                 (*[46]*)let h = (try
000045|                                                 match Hashtbl.find h name with
000046|                                                 | RMapValue(h, MapSubtype) -> (*[36]*)h
000047|                                                 | _ -> (*[0]*)raise (RuntimeError.InternalError "inconsistent library call definition")
000048|                                         with Not_found ->
000049|                                                         (*[10]*)let nh = Hashtbl.create 10
000050|                                                         in (*[10]*)Hashtbl.replace h name (RMapValue(nh, MapSubtype)); (*[10]*)nh)
000051|                                 in (*[46]*)define_map_lib_call h libdef tl
000052|                 | [] -> (*[0]*)raise (RuntimeError.InternalError "invalid library call definition")
000053|         in (*[2]*)let rec loop env = function
000054|                 | [] -> (*[6]*)env
000055|                 | def:: tl ->
000056|                                 (match def.name with
000057|                                         | name::[] ->
000058|                                                         (*[10]*)let env = Environment.declare_variable_and_value env name (RLibraryFunction(def))
000059|                                                         in (*[10]*)loop env tl
000060|                                         | name:: name_rest ->
000061|                                                         (*[84]*)let (h, env) = (try
000062|                                                                         match resolve_variable_value name env with
000063|                                                                         | (RMapValue(h, MapSubtype), loc) -> ((*[84]*)h, env)
000064|                                                                         | _ -> (*[0]*)raise (RuntimeError.InternalError "inconsistent library call definition")
000065|                                                                 with
000066|                                                                 | Variable_not_found(_) ->
000067|                                                                                 (*[0]*)let h = Hashtbl.create 10
000068|                                                                                 in (*[0]*)let env = Environment.declare_variable_and_value env name (RMapValue(h, MapSubtype))
000069|                                                                                 in ((*[0]*)h, env)
000070|                                                                 )
000071|                                                         in (*[84]*)define_map_lib_call h (RLibraryFunction(def)) name_rest;
000072|                                                         (*[84]*)loop env tl
000073|                                         | [] -> (*[0]*)raise (RuntimeError.InternalError "invalid library call definition")
000074|                                 )
000075|         in (*[2]*)let (exported, env) = Library_builtin.initialize env
000076|         in (*[2]*)let env = loop env exported
000077|         in (*[2]*)let (exported, env) = Library_string.initialize env
000078|         in (*[2]*)let env = loop env exported
000079|         in (*[2]*)let (exported, env) = Library_io.initialize env
000080|         in (*[2]*)loop env exported
000081|  
000082| (**
000083| Registers library functions into a runtime environment
000084| @param env analysis environment from which definitions will be transferred
000085| @param renv runtime environment into which definitions will be transferred
000086| @return unit
000087| *)
000088| let register_for_runtime env renv =
000089|         (*[2]*)let rec process rmap =
000090|                 (*[2]*)StringMap.fold(fun k v _ ->
000091|                                                 (*[48]*)let (ind, uid) = v
000092|                                                 in (*[48]*)try
000093|                                                         (*[48]*)let value = get_constant_value env uid
000094|                                                         in (*[48]*)renv.heap.(ind) <- (uid, value)
000095|                                                 with Not_found -> (*[0]*)()
000096|                         ) rmap.variable_map ();
000097|                 match rmap.parent with
000098|                 | None -> (*[2]*)()
000099|                 | Some m -> (*[0]*)process m
000100|         in (*[2]*)process env.globals
000101|         

Legend:
   some code - line containing no point
   some code - line containing only visited points
   some code - line containing only unvisited points
   some code - line containing both visited and unvisited points