Implemented recursiv scope search
[fridhskrift.git] / variable / hash.cpp
bloba2a8832bdf28a743988da59631d594cf9fb464c3
1 #include <ail/exception.hpp>
2 #include <frith/variable.hpp>
3 #include <fnv.hpp>
5 namespace frith
7 uword variable::array_hash(uword previous_hash) const
9 uword hash = previous_hash;
10 for(types::vector::iterator i = array->begin(), end = array->end(); i != end; i++)
11 hash = i->hash(hash);
12 return hash;
15 uword variable::map_hash(uword previous_hash) const
17 uword hash = previous_hash;
18 for(types::map::iterator i = map->begin(), end = map->end(); i != end; i++)
20 hash = i->first.hash(hash);
21 hash = i->second.hash(hash);
23 return hash;
26 uword string_hash(std::string const & value, uword previous_hash)
28 return fnv1a_hash(value.c_str(), value.size(), previous_hash);
31 uword variable::hash(uword previous_hash) const
33 switch(type)
35 case variable_type_identifier::undefined:
36 throw ail::exception("Attempted to calculate the hash of an undefined variable");
38 case variable_type_identifier::nil:
39 return string_hash("nil", previous_hash);
41 case variable_type_identifier::none:
42 return string_hash("none", previous_hash);
44 case variable_type_identifier::boolean:
45 return fnv1a_hash(hash_pointer, sizeof(types::signed_integer), previous_hash);
47 case variable_type_identifier::signed_integer:
48 return fnv1a_hash(hash_pointer, sizeof(types::signed_integer), previous_hash);
50 case variable_type_identifier::unsigned_integer:
51 return fnv1a_hash(hash_pointer, sizeof(types::unsigned_integer), previous_hash);
53 case variable_type_identifier::floating_point_value:
54 return fnv1a_hash(hash_pointer, sizeof(types::floating_point_value), previous_hash);
56 case variable_type_identifier::string:
57 return fnv1a_hash(string->c_str(), string->size(), previous_hash);
59 case variable_type_identifier::array:
60 return array_hash(previous_hash);
62 case variable_type_identifier::map:
63 return map_hash(previous_hash);
65 case variable_type_identifier::function:
66 throw ail::exception("Hashing for functions has not been implemented yet");
68 case variable_type_identifier::object:
69 throw ail::exception("Hashing for objects has not been implemented yet");
72 throw ail::exception("Tried to hash an object of an unknown type");