#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <algorithm>
#include <stack>
#include <ctype.h>
using namespace std;
// nazev typu a-zA-Z0-9_
// musi zacinat pismenem
// case sensitive
// deklarace max jednou, definice prave jednou
// potrebne casty uz musi byt deklarovany
// whitespacy muzou byt kdekoliv
// kontrola:
// 1) nesmi byt bordel
// 2) spravne nazvy typu
// 3) nesmi byt vicenasobna definice typu
// 4) odkaz na neznamy typ
// 5) nesmi existovat implicitni a zaroven explicitni cast na stejny typ
// nesmi byt 2x stejny cast
// nesmi byt cast na sebe
// 6)
// chybu hlasit takto:
// cislo radku: chyba
// prvni chybu a skoncit
// program nacita vstup a hlasi chyby
// parametr "-w" nebo "-W"
// zkontrolovat jestli v grafu IMPLICITNICH castovani existuje cyklus a kdyztak vypise warning a ten cyklus (staci jeden)
// vstup cin, vystup cout
// warningy a chyby na cerr
// vystup je prepsat typy do C++, mapovat na tridy
// vystup musi jit zkompilovat (ale asi nepude slinkovat)
struct Type
{
bool defined;
vector<string> cast_to_implicit;
vector<string> cast_to_explicit;
vector<string> cast_from_implicit;
vector<string> cast_from_explicit;
// for loop checking
int color;
Type() : defined(false), color(0) { };
};
string trim(const string & s)
{
string tmp = s;
string whitespace = " \n\t";
for (unsigned int i = 0; i < tmp.length(); ++i)
{
if (whitespace.find(tmp[i]) != string::npos)
tmp[i] = ' ';
}
while (tmp != "" && tmp[0] == ' ')
{
tmp = tmp.substr(1);
}
while (tmp.find(" ") != string::npos)
{
tmp = tmp.replace(tmp.find(" "), 2, " ");
}
while (tmp != "" && tmp[tmp.length() - 1] == ' ')
{
tmp = tmp.substr(0, tmp.length() - 1);
}
return tmp;
}
bool is_char_alpha(char c)
{
if (c >= 'a' && c <= 'z') return true;
if (c >= 'A' && c <= 'Z') return true;
return false;
}
bool is_char_alphanum(char c)
{
if (c >= 'a' && c <= 'z') return true;
if (c >= 'A' && c <= 'Z') return true;
if (c >= '0' && c <= '9') return true;
return false;
}
void parse_line(const string & line, string & keyword, string & identifier)
{
if (line.find(' ') == string::npos)
throw invalid_argument("Invalid line.");
keyword = line.substr(0, line.find(' '));
identifier = line.substr(line.find(' ') + 1);
}
void check_identifier(const string & identifier)
{
if (identifier.find(' ') != string::npos)
throw invalid_argument("Invalid identifier.");
if (identifier.length() < 1)
throw invalid_argument("Invalid identifier.");
if (!is_char_alpha(identifier[0]))
throw invalid_argument("Invalid identifier.");
for (unsigned int i = 1; i < identifier.length(); ++i)
{
if ((!is_char_alphanum(identifier[i])) && identifier[i] != '_')
throw invalid_argument("Invalid identifier.");
}
}
bool find_loops_process(map<string, Type> & types, const string & identifier, stack<string> & loop_stack)
{
types[identifier].color = 1;
loop_stack.push(identifier);
for (vector<string>::const_iterator i = types[identifier].cast_to_implicit.begin(); i != types[identifier].cast_to_implicit.end(); ++i)
{
const string & cast_to = *i;
if (types[cast_to].color == 1)
{
loop_stack.push(cast_to);
return true;
}
if (find_loops_process(types, cast_to, loop_stack) == true)
return true;
}
types[identifier].color = 2;
loop_stack.pop();
return false;
}
bool find_loops(map<string, Type> & types)
{
stack<string> loop_stack;
while (true)
{
string found_type = "";
for (map<string, Type>::iterator i = types.begin(); i != types.end(); ++i)
{
if (i->second.color == 0)
{
found_type = i->first;
break;
}
}
if (found_type == "")
break;
if (find_loops_process(types, found_type, loop_stack) == true)
{
cerr << "warning: the following implicit casting loop was found:" << endl;
cerr << " ";
string last = loop_stack.top();
loop_stack.pop();
cerr << last << " <- ";
while (!loop_stack.empty())
{
string s = loop_stack.top();
loop_stack.pop();
if (s == last) break;
cerr << s << " <- ";
}
cerr << last;
cerr << endl;
return true;
}
}
return false;
}
int main(int argc, char * argv[])
{
map<string, Type> types;
int line_number = 0;
bool check_loops = false;
for (int i = 1; i < argc; ++i)
{
if (_stricmp(argv[i], "-w") == 0)
check_loops = true;
}
try
{
string last_type;
while (true)
{
string line;
getline(cin, line);
line_number++;
if (cin.fail())
break;
if (line == "")
continue;
line = trim(line);
string keyword;
string identifier;
parse_line(line, keyword, identifier);
check_identifier(identifier);
if (keyword == "type")
{
if (types.find(identifier) != types.end())
{
// already exists
if (types[identifier].defined)
throw invalid_argument("Type already defined");
types[identifier].defined = true;
}
else
{
types[identifier] = Type();
}
last_type = identifier;
}
else if (keyword == "implicit")
{
if (last_type != "")
{
if (types.find(identifier) == types.end())
throw invalid_argument("Type not declared.");
if (find(types[last_type].cast_to_implicit.begin(), types[last_type].cast_to_implicit.end(), identifier)
!= types[last_type].cast_to_implicit.end())
throw invalid_argument("Cast already defined.");
if (last_type == identifier)
throw invalid_argument("Self-casting forbidden.");
types[last_type].cast_to_implicit.push_back(identifier);
}
}
else if (keyword == "explicit")
{
if (last_type != "")
{
if (types.find(identifier) == types.end())
throw invalid_argument("Type not declared.");
if (find(types[last_type].cast_to_explicit.begin(), types[last_type].cast_to_explicit.end(), identifier)
!= types[last_type].cast_to_explicit.end())
throw invalid_argument("Cast already defined.");
if (last_type == identifier)
throw invalid_argument("Self-casting forbidden.");
types[last_type].cast_to_explicit.push_back(identifier);
}
}
else
{
throw invalid_argument("Invalid keyword.");
}
}
// mapping
for (map<string, Type>::iterator i = types.begin(); i != types.end(); ++i)
{
const string & identifier = i->first;
const Type & type = i->second;
for (vector<string>::const_iterator j = type.cast_to_implicit.begin(); j != type.cast_to_implicit.end(); ++j)
{
const string & cast_to = *j;
types[cast_to].cast_from_implicit.push_back(identifier);
}
for (vector<string>::const_iterator j = type.cast_to_explicit.begin(); j != type.cast_to_explicit.end(); ++j)
{
const string & cast_to = *j;
types[cast_to].cast_from_explicit.push_back(identifier);
}
}
if (check_loops)
{
find_loops(types);
}
// output
for (map<string, Type>::iterator i = types.begin(); i != types.end(); ++i)
{
const string & identifier = i->first;
cout << "class " << identifier << ";" << endl;
}
for (map<string, Type>::iterator i = types.begin(); i != types.end(); ++i)
{
const string & identifier = i->first;
const Type & type = i->second;
cout << "class " << identifier << " {" << endl;
for (vector<string>::const_iterator j = type.cast_from_implicit.begin(); j != type.cast_from_implicit.end(); ++j)
{
cout << " " << identifier << "(const " << *j << " &) { };" << endl;
}
for (vector<string>::const_iterator j = type.cast_from_explicit.begin(); j != type.cast_from_explicit.end(); ++j)
{
cout << " explicit " << identifier << "(const " << *j << " &) { };" << endl;
}
cout << "};" << endl;
}
}
catch (exception & e)
{
cerr << "error at line " << line_number << ": " << e.what() << endl;
return -1;
}
return 0;
}