1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#if TRY_PARAMETRIC_ENV
open Ihashtab
type environment (i : int) =
let mutable hashtab = Hashtbl.create i
interface IHashtab<string,valeur> with
member self.add key value =
Hashtbl.add hashtab key value
member self.replace key value =
Hashtbl.replace hashtab key value
member self.remove key =
Hashtbl.remove hashtab key
member self.find key =
Hashtbl.find hashtab key
member self.iter f =
Hashtbl.iter f hashtab
/// environment can be seen as a set of (name,value)
module Environment =
/// Creates a new environment
let create i = environment(i) :> IHashtab<string,valeur>
/// Adds a variable in the environment, and assigns it the given value
let add (env:IHashtab<string,valeur>) key value =
env.add key value
/// Replaces the value of a variable
let replace (env:IHashtab<string,valeur>) key value =
env.replace key value
/// Removes the entry corresponding to a variable
let remove (env:IHashtab<string,valeur>) key =
env.remove key
/// Finds the value of a variable
let find (env:IHashtab<string,valeur>) key =
env.find key
/// Iterates a function on all the elements
let iter f (env:IHashtab<string,valeur>) =
env.iter f
/// Tries to replace value, but if it doesn't exist, it creates it
let assign (env:IHashtab<string,valeur>) key value =
try
let _ = find env key
replace env key value
with Not_found ->
add env key value
#else
type environment = (string,valeur) Myhashtab.t //Hashtbl.t
/// environment can be seen as a set of (name,value)
module Environment =
/// Creates a new environment
let create i : environment = Myhashtab.create i
/// Finds the value of a variable
let find (env : environment) key : valeur = Myhashtab.find env key
/// Iterates a function on all the elements
let iter f (env : environment) = Myhashtab.iter f env
/// Adds a variable in the environment, and assigns it the given value
let add (env : environment) key value = Myhashtab.add env key value
/// Replaces the value of a variable
let replace (env : environment) key value = Myhashtab.replace env key value
/// Tries to replace value, but if it doesn't exist, it creates it
let assign (env : environment) key value =
try
let _ = find env key
replace env key value
with Not_found ->
add env key value
#endif |
Partager