/* G-Cows - scripting language for creation of web sites Copyright (C) 2002, 2003, 2004, 2005, 2006 Andrea Sozzi This file is part of G-Cows. G-Cows is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. G-Cows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef LIB_VARIABLES_H #define LIB_VARIABLES_H #include #include #include #include "common.H" #include "error.H" using std::string; using std::vector; using std::map; class Gen_type; typedef vector Gen_type_vector; typedef Gen_type_vector::iterator Gen_type_iter; typedef Gen_type_vector::const_iterator Gen_type_c_iter; class Gen_type { public: Gen_type () : type ('u'), int_value (0), str_value ("") {} Gen_type (int int_in) : type ('i'), int_value (int_in), str_value ("") {} Gen_type (char *char_in) : type ('c'), int_value (0), str_value (char_in) {} Gen_type (string str_in) : type ('c'), int_value (0), str_value (str_in) {} Gen_type (char c) : type (c), int_value (0), str_value ("") {} // Functions releated to type identification bool is_int () const { return type == 'i'; } bool is_string () const { return type == 'c'; } bool is_array () const { return type == 'a'; } bool is_list () const { return type == 'l'; } bool is_undefined () const { return type == 'u'; } char get_type () const { return type; } void fix_list (); void push_items (Gen_type value); operator bool () const; Gen_type operator>(const Gen_type& right_val) const; Gen_type operator<(const Gen_type& right_val) const; Gen_type operator>=(const Gen_type& right_val) const; Gen_type operator<=(const Gen_type& right_val) const; Gen_type operator==(const Gen_type& right_val) const; Gen_type operator!=(const Gen_type& right_val) const; Gen_type operator+(const Gen_type& right_val) const; Gen_type operator-(const Gen_type& right_val) const; Gen_type operator*(const Gen_type& right_val) const; Gen_type operator/(const Gen_type& right_val) const; Gen_type operator%(const Gen_type& right_val) const; Gen_type operator^(const Gen_type& right_val) const; Gen_type operator-() const; Gen_type operator!() const { return Gen_type ( ! (bool)(*this)); } Gen_type weak_eq (const Gen_type& right_val) const; Gen_type weak_neq (const Gen_type& right_val) const; string get_string () const; int get_int () const; const Gen_type_vector *get_array () const; const Gen_type_vector *get_list () const; Gen_type get_element (Gen_type index); Gen_type set_element (Gen_type value); Gen_type set_element (Gen_type index, Gen_type value, bool shift=false); Gen_type insert_element (Gen_type index, Gen_type value) { return set_element (index, value, true); } bool to_bool (); int to_int (); string to_string (bool scalar_only=false) const; void push (Gen_type item) { arr_value.push_back (item); } void erase (unsigned int pos); void erase (unsigned int pos, unsigned int len); private: char type; int int_value; string str_value; Gen_type_vector arr_value; }; typedef map Var_container; typedef Var_container::iterator Var_iterator; class Variables { public: Variables (); Gen_type set_var (string name, Gen_type value); Gen_type set_var (string assignment); Gen_type get_var (string name); Gen_type get_array_element (string name, Gen_type index); Gen_type set_array_element (string name, Gen_type index, Gen_type value, bool shift=false); bool is_def (string name) { return (!( vars.find (name) == vars.end ()));}; bool undef (string name); private: Var_container vars; }; #endif // ! LIB_VARIABLES_H