2 T_generator class and T_generator_registry class
The main function of this two class is to provide the basic information for generating code for all languages and to provide specific code generator objects, which is the method that invokes this two class to generate the code generator object for the specific language and the functional function that executes the generated code. The following main analysis of the function of two functions, one is the T_generator_registry class Get_generator function, this is a static function can be directly through the class call, and the other is the Generate_program function of the T_generator class.
(1) The Get_generator function of the T_generator_registry class
This function has two parameters, one is the object program to represent programs, and the other is the language string parameter (including a short string representing the language and a combination of options, some not). The function first parses the language string parameter, which is organized in the parameter string: preceded by a colon (:) is a string representing the language, followed by an optional parameter, with each optional argument separated by a comma (,), and each optional parameter is a key-value pair and the key and value are separated by an equal sign (=). According to the above string format to parse the parameters of the section can be, the option parameter with a map to save the key value pairs, the code implementation is as follows:
string::size_type colon = options.find (': ');
String language = Options.substr (0, colon);
Map<string, string> parsed_options;
if (colon!= string::npos) {string::size_type pos = colon+1; while (POS!= string::npos && pos < Options.size ()) {String::size_type Next_pos = Options.find (', '),
POS);
string option = Options.substr (pos, Next_pos-pos);
pos = ((Next_pos = = string::npos)? next_pos:next_pos+1);
String::size_type separator = option.find (' = ');
string key, value;
if (separator = = String::npos) {key = option;
Value = "";
else {key = Option.substr (0, separator);
Value = Option.substr (separator+1);
} Parsed_options[key] = value; }
}
Then call the Get_generator_map function to get a map object representing the language string and the factory object that generated the language generator object:gen_map_t& The_map = Get_generator_map (); The definition of gen_map_t is as follows:
typedef std::map<std::string, t_generator_factory*> gen_map_t;
The GET_GENERATOR_MAP function has only two lines of code, one defining a static local variable and initializing it (because static local variables must be initialized and initialized only for the first time, because there is an error when the linker is not initialized), and the second sentence is to return the static local variable to the caller. The code is as follows:
static gen_map_t* The_map = new gen_map_t ();
return *the_map;
Then locate the factory object in the corresponding language in the Map object, and then use the factory object to produce a code generator object of that language and return it to the caller, as shown in the following code:
Gen_map_t::iterator iter = the_map.find (language);
Return to iter->second->get_generator (program, parsed_options, Options);