compiler/cpp/src/generate/t_hs_generator.cc
/**
 * Prints the value of a constant with the given type. Note that type checking
 * is NOT performed in this function as it is always run beforehand using the
 * validate_types method in main.cc
 */
Prints the value of a constant with the given type. Note that type checking
is NOT performed in this function as it is always run beforehand using the
validate_types method in main.cc
Prints the value of a constant with the given type . Note that type checking is NOT performed in this function as it is always run beforehand using the validate_types method in main . cc
string t_hs_generator::render_const_value(t_type* type, t_const_value* value) {
  type = get_true_type(type);
  ostringstream out;
  if (type->is_base_type()) {
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
    switch (tbase) {
    case t_base_type::TYPE_STRING:
      out << '"' << get_escaped_string(value) << '"';
      break;
    case t_base_type::TYPE_BOOL:
      out << (value->get_integer() > 0 ? "True" : "False");
      break;
    case t_base_type::TYPE_BYTE:
      out << "(" << value->get_integer() << " :: Int8)";
      break;
    case t_base_type::TYPE_I16:
      out << "(" << value->get_integer() << " :: Int16)";
      break;
    case t_base_type::TYPE_I32:
      out << "(" << value->get_integer() << " :: Int32)";
      break;
    case t_base_type::TYPE_I64:
      out << "(" << value->get_integer() << " :: Int64)";
      break;
    case t_base_type::TYPE_DOUBLE:
      if (value->get_type() == t_const_value::CV_INTEGER) {
        out << value->get_integer();
      } else {
        out << value->get_double();
      }
      break;
    default:
      throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
    }
  } else if (type->is_enum()) {
    t_enum* tenum = (t_enum*)type;
    vector<t_enum_value*> constants = tenum->get_constants();
    vector<t_enum_value*>::iterator c_iter;
    for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
      int val = (*c_iter)->get_value();
      if (val == value->get_integer()) {
        indent(out) << capitalize((*c_iter)->get_name());
        break;
      }
    }
  } else if (type->is_struct() || type->is_xception()) {
    string cname = type_name(type);
    indent(out) << cname << "{";
    const vector<t_field*>& fields = ((t_struct*)type)->get_members();
    vector<t_field*>::const_iterator f_iter;
    const map<t_const_value*, t_const_value*>& val = value->get_map();
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
    bool first = true;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      t_type* field_type = NULL;
      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter)
        if ((*f_iter)->get_name() == v_iter->first->get_string())
          field_type = (*f_iter)->get_type();
      if (field_type == NULL)
        throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
      string fname = v_iter->first->get_string();
      string const_value = render_const_value(field_type, v_iter->second);
      out << (first ? "" : ",");
      out << "f_" << cname << "_" << fname << " = Just (" << const_value << ")";
      first = false;
    }
    indent(out) << "}";
  } else if (type->is_map()) {
    t_type* ktype = ((t_map*)type)->get_key_type();
    t_type* vtype = ((t_map*)type)->get_val_type();
    const map<t_const_value*, t_const_value*>& val = value->get_map();
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
    out << "(Map.fromList [";
    bool first = true;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      string key = render_const_value(ktype, v_iter->first);
      string val = render_const_value(vtype, v_iter->second);
      out << (first ? "" : ",");
      out << "(" << key << "," << val << ")";
      first = false;
    }
    out << "])";
  } else if (type->is_list() || type->is_set()) {
    t_type* etype = type->is_list()
        ? ((t_list*) type)->get_elem_type()
        : ((t_set*) type)->get_elem_type();
    const vector<t_const_value*>& val = value->get_list();
    vector<t_const_value*>::const_iterator v_iter;
    if (type->is_set())
      out << "(Set.fromList [";
    else
      out << "(Vector.fromList ";
    bool first = true;
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
      out << (first ? "" : ",");
      out << render_const_value(etype, *v_iter);
      first = false;
    }
    out << "])";
  } else {
    throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
  }
  return out.str();
}
string t_hs_generator :: render_const_value ( t_type * type , t_const_value * value ) { type = get_true_type ( type ) ; ostringstream out ; if ( type -> is_base_type ( ) ) { t_base_type :: t_base tbase = ( ( t_base_type * ) type ) -> get_base ( ) ; switch ( tbase ) { case t_base_type :: TYPE_STRING : out << ' ' << get_escaped_string ( value ) << ' ' ; break ; case t_base_type :: TYPE_BOOL : out << ( value -> get_integer ( ) > 0 ? " " : " " ) ; break ; case t_base_type :: TYPE_BYTE : out << " " << value -> get_integer ( ) << " " ; break ; case t_base_type :: TYPE_I16 : out << " " << value -> get_integer ( ) << " " ; break ; case t_base_type :: TYPE_I32 : out << " " << value -> get_integer ( ) << " " ; break ; case t_base_type :: TYPE_I64 : out << " " << value -> get_integer ( ) << " " ; break ; case t_base_type :: TYPE_DOUBLE : if ( value -> get_type ( ) == t_const_value :: CV_INTEGER ) { out << value -> get_integer ( ) ; } else { out << value -> get_double ( ) ; } break ; default : throw " " + t_base_type :: t_base_name ( tbase ) ; } } else if ( type -> is_enum ( ) ) { t_enum * tenum = ( t_enum * ) type ; vector < t_enum_value * > constants = tenum -> get_constants ( ) ; vector < t_enum_value * > :: iterator c_iter ; for ( c_iter = constants . begin ( ) ; c_iter != constants . end ( ) ; ++ c_iter ) { int val = ( * c_iter ) -> get_value ( ) ; if ( val == value -> get_integer ( ) ) { indent ( out ) << capitalize ( ( * c_iter ) -> get_name ( ) ) ; break ; } } } else if ( type -> is_struct ( ) || type -> is_xception ( ) ) { string cname = type_name ( type ) ; indent ( out ) << cname << " " ; const vector < t_field * > & fields = ( ( t_struct * ) type ) -> get_members ( ) ; vector < t_field * > :: const_iterator f_iter ; const map < t_const_value * , t_const_value * > & val = value -> get_map ( ) ; map < t_const_value * , t_const_value * > :: const_iterator v_iter ; bool first = true ; for ( v_iter = val . begin ( ) ; v_iter != val . end ( ) ; ++ v_iter ) { t_type * field_type = NULL ; for ( f_iter = fields . begin ( ) ; f_iter != fields . end ( ) ; ++ f_iter ) if ( ( * f_iter ) -> get_name ( ) == v_iter -> first -> get_string ( ) ) field_type = ( * f_iter ) -> get_type ( ) ; if ( field_type == NULL ) throw " " + type -> get_name ( ) + " " + v_iter -> first -> get_string ( ) ; string fname = v_iter -> first -> get_string ( ) ; string const_value = render_const_value ( field_type , v_iter -> second ) ; out << ( first ? " " : " " ) ; out << " " << cname << " " << fname << " " << const_value << " " ; first = false ; } indent ( out ) << " " ; } else if ( type -> is_map ( ) ) { t_type * ktype = ( ( t_map * ) type ) -> get_key_type ( ) ; t_type * vtype = ( ( t_map * ) type ) -> get_val_type ( ) ; const map < t_const_value * , t_const_value * > & val = value -> get_map ( ) ; map < t_const_value * , t_const_value * > :: const_iterator v_iter ; out << " " ; bool first = true ; for ( v_iter = val . begin ( ) ; v_iter != val . end ( ) ; ++ v_iter ) { string key = render_const_value ( ktype , v_iter -> first ) ; string val = render_const_value ( vtype , v_iter -> second ) ; out << ( first ? " " : " " ) ; out << " " << key << " " << val << " " ; first = false ; } out << " " ; } else if ( type -> is_list ( ) || type -> is_set ( ) ) { t_type * etype = type -> is_list ( ) ? ( ( t_list * ) type ) -> get_elem_type ( ) : ( ( t_set * ) type ) -> get_elem_type ( ) ; const vector < t_const_value * > & val = value -> get_list ( ) ; vector < t_const_value * > :: const_iterator v_iter ; if ( type -> is_set ( ) ) out << " " ; else out << " " ; bool first = true ; for ( v_iter = val . begin ( ) ; v_iter != val . end ( ) ; ++ v_iter ) { out << ( first ? " " : " " ) ; out << render_const_value ( etype , * v_iter ) ; first = false ; } out << " " ; } else { throw " " + type -> get_name ( ) ; } return out . str ( ) ; }
Prints the value of a constant with the given type.
Prints the value of a constant with the given type .