123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- #include "code_generator.hpp"
- string generate(vector<string> &tokens, int &i, map<string, string> &aliases, map<string, Pointer*> &pointers,
- int &line_num, int ¤t_cell, bool &error)
- {
- string bfcode = "";
- if (tokens[i] == "newline")
- {
- ++line_num;
- return bfcode;
- }
- if (tokens[i] == "read")
- {
- return ",";
- }
- if (tokens[i] == "reads")
- {
- string come_back;
- int lenght;
- try
- {
- ++i;
- lenght = stoi(tokens[i]);
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument for 'reads'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- for (int i = 0; i < lenght; i++)
- {
- bfcode += ",>";
- come_back += "<";
- }
- return bfcode + "\n" + come_back + "\n";
- }
- if (tokens[i] == "sub")
- {
- int p1, p2, resultpos;
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- p1 = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- p1 = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- p2 = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- p2 = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- resultpos = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- resultpos = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- return sub(p1, p2, resultpos, current_cell);
- }
- if (tokens[i] == "sum")
- {
- int p1, p2, resultpos;
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- p1 = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- p1 = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 1 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- p2 = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- p2 = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 2 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- ++i;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- resultpos = pointers[tokens[i]]->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- else
- {
- try
- {
- ++i;
- resultpos = stoi(tokens[i]);
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument 3 for 'sum'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- return sum(p1, p2, resultpos, current_cell);
- }
- if (tokens[i] == "putchar")
- {
- return ".";
- }
- if (tokens[i] == "pointer")
- {
- ++i;
- try
- {
- pointers[tokens[i]] = new Pointer("pointer", stoi(tokens[i+1]));
- ++i;
- return bfcode;
- }
- catch(invalid_argument err)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument for 'pointer'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "array")
- {
- ++i;
- try
- {
- pointers[tokens[i]] = new Pointer("array", stoi(tokens[i+1]), stoi(tokens[i+1]) + stoi(tokens[i+2]));
- ++i;
- ++i;
- return bfcode;
- }
- catch(invalid_argument err)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")<<"invalid argument(s) for 'array'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "loadalias")
- {
- try
- {
- ++i;
- tokens[i] = aliases.at(tokens[i]);
- return bfcode;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("name error: ")
- <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "bypointer")
- {
- ++i;
- Pointer *pointer = nullptr;
- try
- {
- pointer = pointers.at(tokens[i]);
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
- <<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- ++i;
- if (tokens[i] == "assigment")
- {
- bfcode += go_to(current_cell, pointer->start);
- ++i;
- bfcode += setchar(tokens[i][0]);
- bfcode += go_to(pointer->start, current_cell);
- return bfcode;
- }
- if (tokens[i] == "clear")
- {
- bfcode += go_to(current_cell, pointer->start);
- ++i;
- bfcode += "[-]\n";
- bfcode += go_to(pointer->start, current_cell);
- return bfcode;
- }
- if (tokens[i] == "add")
- {
- bfcode += go_to(current_cell, pointer->start);
- ++i;
- try
- {
- bfcode += change(stoi(tokens[i]));
- bfcode += go_to(pointer->start, current_cell);
- return bfcode;
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for pointer's 'add' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "sub")
- {
- bfcode += go_to(current_cell, pointer->start);
- ++i;
- try
- {
- bfcode += change(-stoi(tokens[i]));
- bfcode += go_to(pointer->start, current_cell);
- return bfcode;
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for pointer's 'sub' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- }
- if (tokens[i] == "byarray")
- {
- ++i;
- Pointer *pointer = nullptr;
- try
- {
- pointer = pointers.at(tokens[i]);
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
- <<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- ++i;
- int at;
- try
- {
- at = pointer->start + stoi(tokens[i]);
- if (at > pointer->end || at < pointer->start)
- {
- cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("out of range error: ")<<"array index out of range"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("argument error: ")<<"invalid argument for 'byarray'"
- <<" (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- ++i;
- if (tokens[i] == "assigment")
- {
- bfcode += go_to(current_cell, at);
- ++i;
- bfcode += setchar(tokens[i][0]);
- bfcode += go_to(at, current_cell);
- return bfcode;
- }
- if (tokens[i] == "clear")
- {
- bfcode += go_to(current_cell, at);
- ++i;
- bfcode += "[-]\n";
- bfcode += go_to(at, current_cell);
- return bfcode;
- }
- if (tokens[i] == "add")
- {
- bfcode += go_to(current_cell, at);
- ++i;
- try
- {
- bfcode += change(stoi(tokens[i]));
- bfcode += go_to(at, current_cell);
- return bfcode;
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for pointer's 'add' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "sub")
- {
- bfcode += go_to(current_cell, at);
- ++i;
- try
- {
- bfcode += change(-stoi(tokens[i]));
- bfcode += go_to(at, current_cell);
- return bfcode;
- }
- catch (invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for pointer's 'sub' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- }
- if (tokens[i] == "goto")
- {
- ++i;
- int at;
- bool ploaded = false;
- if (tokens[i] == "loadpointer")
- {
- try
- {
- ++i;
- ploaded = true;
- at = pointers.at(tokens[i])->start;
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator")<<ERROR_MSG("name error: ")<<"pointer '"<<tokens[i]
- <<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- try
- {
- if (!ploaded) at = stoi(tokens[i]);
- go_to(current_cell, at);
- current_cell = stoi(tokens[i]);
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for 'goto' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
-
- }
- if (tokens[i] == "alias")
- {
- ++i;
- aliases[tokens[i]] = tokens[i+1];
- ++i;
- return bfcode;
- }
- if (tokens[i] == "setchar")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- bfcode += setchar(tokens[i][0]);
- return bfcode;
- }
- if (tokens[i] == "walk")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- try
- {
- bfcode += walk(stoi(tokens[i]));
- current_cell += stoi(tokens[i]);
- return bfcode;
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for 'walk' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "change")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- try
- {
- bfcode += change(stoi(tokens[i]));
- return bfcode;
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for 'change' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "mov")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- try
- {
- bfcode += mov(stoi(tokens[i]));
- return bfcode;
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for 'mov' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- if (tokens[i] == "print")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- try
- {
- bfcode += print(stoi(tokens[i]));
- return bfcode;
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript2: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument for 'print' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- return bfcode;
- }
- if (tokens[i] == "split")
- {
- ++i;
- if (tokens[i] == "loadalias")
- {
- bfcode += generate(tokens, i, aliases, pointers, line_num, current_cell, error);
- if (error) return bfcode;
- }
- bfcode += split(tokens[i]);
- return bfcode;
- }
- if (tokens[i] == "copy")
- {
- ++i;
- string arg1, arg2;
- if (tokens[i] == "loadalias")
- {
- ++i;
- try
- {
- arg1 = aliases[tokens[i]];
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("name error: ")
- <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- ++i;
- if (tokens[i] == "loadalias")
- {
- ++i;
- try
- {
- arg1 = aliases[tokens[i]];
- }
- catch(out_of_range)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("name error: ")
- <<"alias '"<<tokens[i]<<"' is not defined (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- }
- try
- {
- bfcode += copy(stoi(arg1), stoi(arg2));
- }
- catch(invalid_argument)
- {
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("argument error: ")
- <<"invalid argument(s) for 'copy' (line "<<line_num<<")\n";
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
- return bfcode;
- }
- cout<<ERROR_MSG("Error: ")<<"unexpected token at line "<<line_num<<": "<<BOLD_MSG(tokens[i])<<endl;
- cout<<BOLD_MSG("bfscript: code generator: ")<<ERROR_MSG("code generating terminated")<<endl;
- error = true;
- return bfcode;
- }
|