dawrehxyz

Mostworking

Nov 17th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <regex>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. struct Token
  11. {
  12.     Token(int n, string c)
  13.     {
  14.         s = c;
  15.         nr = n;
  16.     }
  17.     string s;
  18.     int nr;
  19. };
  20.  
  21. struct Command
  22. {
  23.     Command(string p1, string p2)
  24.     {
  25.         s1 = p1;
  26.         s2 = p2;
  27.     }
  28.     string s1;
  29.     string s2;
  30. };
  31.  
  32. struct V2
  33. {
  34.     V2(float px, float py)
  35.     {
  36.         x = px;
  37.         y = py;
  38.     }
  39.     float x;
  40.     float y;
  41.     float rotate = 0;
  42. };
  43.  
  44. void lexicalanalysator();
  45. char caseupper(char c);
  46. bool hexletter(char c);
  47. bool parser();
  48. void calc();
  49. void syntaxerror(int nr);
  50. bool c1(string token);
  51. bool c2(string token);
  52. bool h6(string token);
  53. bool D(string token);
  54.  
  55. vector<string> rows;
  56. vector<Token> tokens;
  57. vector<Command> commands;
  58.  
  59. const double pi = 3.1415926535897;
  60. string outputColor = "#0000FF";
  61. V2 position = V2(0, 0);
  62. vector<string> outputVector;
  63.  
  64. int main()
  65. {
  66.     lexicalanalysator();
  67.     if (!parser()) return 0;
  68.     /*for (int a = 0; a < commands.size(); a++)
  69.     {
  70.         cout << commands.at(a).s1 << " " << commands.at(a).s2 << endl;
  71.     }*/
  72.     calc();
  73. }
  74.  
  75. void calc()
  76. {
  77.     bool stateUP = true;
  78.     for (int a = 0; a < commands.size(); a++)
  79.     {
  80.         Command TI = commands.at(a);
  81.         float d = atoi(TI.s2.c_str());
  82.         if (TI.s1 == "DOWN") stateUP = false;
  83.         else if (TI.s1 == "UP") stateUP = true;
  84.         else if (TI.s1 == "COLOR") outputColor = TI.s2;
  85.         else if (TI.s1 == "LEFT") position.rotate += d;
  86.         else if (TI.s1 == "RIGHT") position.rotate -= d;
  87.         else if (TI.s1 == "FORW")
  88.         {
  89.             string otemp = "";
  90.             otemp += outputColor + " " + to_string(position.x) + " " + to_string(position.y);
  91.             position.x += d*cos(pi*position.rotate / 180);
  92.             position.y += d*sin(pi*position.rotate / 180);
  93.             otemp += " " + to_string(position.x) + " " + to_string(position.y);
  94.             if (!stateUP) cout << otemp << endl;
  95.         }
  96.         else if (TI.s1 == "BACK")
  97.         {
  98.             string otemp = "";
  99.             otemp += outputColor + " " + to_string(position.x) + " " + to_string(position.y);
  100.             position.x -= d*cos(pi*position.rotate / 180);
  101.             position.y -= d*sin(pi*position.rotate / 180);
  102.             otemp += " " + to_string(position.x) + " " + to_string(position.y);
  103.             if(!stateUP) cout << otemp << endl;
  104.         }
  105.     }
  106. }
  107.  
  108. bool parser()
  109. {
  110.     vector<string> citations;
  111.     vector<bool> repactivated;
  112.     vector<int> repnumbers;
  113.     vector<int> oldCommandSize;
  114.     string expected = "none";
  115.     int repmult = 1;
  116.     bool singlerep = false;
  117.     for (int a = 0; a < tokens.size(); a++)
  118.     {
  119.         Token token = tokens.at(a);
  120.         if (expected == "none")
  121.         {
  122.             if (c1(token.s))
  123.             {
  124.                 expected = ".";
  125.                 commands.push_back(Command(token.s, "-1"));
  126.             }
  127.             else if (c2(token.s))
  128.             {
  129.                 expected = "c2nr";
  130.                 commands.push_back(Command(token.s, "-1"));
  131.             }
  132.             else if (token.s == "COLOR")
  133.             {
  134.                 expected = "colorhex";
  135.                 commands.push_back(Command(token.s, "-1"));
  136.             }
  137.             else if (token.s == "REP")
  138.             {
  139.                 expected = "repnr";
  140.             }
  141.             else if (token.s == "\"" && repactivated.size() && repactivated.back())
  142.             {
  143.                 singlerep = false;
  144.                 expected = "none";
  145.                 if (citations.back() == "\"")
  146.                 {
  147.                     int commandsSize = commands.size();
  148.                     for (int b = 0; b < repnumbers.back() - 1; b++)
  149.                     {
  150.                         for (int c = oldCommandSize.back(); c < commandsSize; c++)
  151.                         {
  152.                             commands.push_back(commands.at(c));
  153.                         }
  154.                     }
  155.                     if (oldCommandSize.back() == commandsSize)
  156.                     {
  157.                         syntaxerror(token.nr);
  158.                         return false;
  159.                     }
  160.                     oldCommandSize.pop_back();
  161.                     citations.pop_back();
  162.                     repnumbers.pop_back();
  163.                     repactivated.pop_back();
  164.                 }
  165.                 else
  166.                 {
  167.                     citations.back() = "\"";
  168.                 }
  169.             }
  170.             else
  171.             {
  172.                 syntaxerror(token.nr);
  173.                 return false;
  174.             }
  175.         }
  176.         else
  177.         {
  178.             if (expected == ".")
  179.             {
  180.                 expected = "none";
  181.                 if (singlerep)
  182.                 {
  183.                     Command tempCommand = commands.at(commands.size() - 1);
  184.                     for (int b = 0; b < repnumbers.back() - 1; b++)
  185.                     {
  186.                         commands.push_back(tempCommand);
  187.                     }
  188.                     singlerep = false;
  189.                     oldCommandSize.pop_back();
  190.                     citations.pop_back();
  191.                     repnumbers.pop_back();
  192.                     repactivated.pop_back();
  193.                 }
  194.             }
  195.             else if (expected == "c2nr")
  196.             {
  197.                 expected = ".";
  198.                 if (D(token.s))
  199.                 {
  200.                     commands.back().s2 = token.s;
  201.                 }
  202.                 else
  203.                 {
  204.                     syntaxerror(token.nr);
  205.                     return false;
  206.                 }
  207.             }
  208.             else if (expected == "colorhex")
  209.             {
  210.                 expected = ".";
  211.                 if (h6(token.s))
  212.                 {
  213.                     commands.back().s2 = token.s;
  214.                 }
  215.                 else
  216.                 {
  217.                     syntaxerror(token.nr);
  218.                     return false;
  219.                 }
  220.             }
  221.             else if (expected == "repnr")
  222.             {
  223.                 expected = "none";
  224.                 if (D(token.s))
  225.                 {
  226.                     repmult *= atoi(token.s.c_str());
  227.                     if (tokens.at(a + 1).s == "REP")
  228.                     {
  229.                         continue;
  230.                     }
  231.                     repactivated.push_back(true);
  232.                     repnumbers.push_back(repmult);
  233.                     citations.push_back("");
  234.                     oldCommandSize.push_back(commands.size());
  235.                     singlerep = true;
  236.                     repmult = 1;
  237.                 }
  238.                 else
  239.                 {
  240.                     syntaxerror(token.nr);
  241.                     return false;
  242.                 }
  243.             }
  244.         }
  245.     }
  246.     if (repactivated.size())
  247.     {
  248.         syntaxerror(tokens.at(tokens.size() - 1).nr);
  249.         return false;
  250.     }
  251.     if (expected != "none")
  252.     {
  253.         syntaxerror(tokens.at(tokens.size() - 1).nr);
  254.         return false;
  255.     }
  256.  
  257.     return true;
  258. }
  259.  
  260. bool c1(string token)
  261. {
  262.     if (token == "UP" || token == "DOWN")
  263.         return true;
  264.     return false;
  265. }
  266.  
  267. bool c2(string token)
  268. {
  269.     if (token == "FORW" || token == "BACK" || token == "LEFT" || token == "RIGHT")
  270.         return true;
  271.     return false;
  272. }
  273.  
  274. bool h6(string token)
  275. {
  276.     if (token.length() == 7 && token[0] == '#' && hexletter(token[1]) && hexletter(token[2]) && hexletter(token[3]) && hexletter(token[4]) && hexletter(token[5]) && hexletter(token[6]))
  277.         return true;
  278.     return false;
  279. }
  280.  
  281. bool D(string token)
  282. {
  283.     if (!(token[0] > 48 && token[0] < 58))
  284.         return false;
  285.     for (int a = 0; a < token.length(); a++)
  286.     {
  287.         if (!(token[a] > 47 && token[a] < 58))
  288.             return false;
  289.     }
  290.     return true;
  291. }
  292.  
  293. bool hexletter(char c)
  294. {
  295.     if ((c > 47 && c < 58) || (c > 64 && c < 71))
  296.         return true;
  297.     return false;
  298. }
  299.  
  300. void lexicalanalysator()
  301. {
  302.     string row;
  303.     string word;
  304.  
  305.     ios_base::sync_with_stdio(false);
  306.     while (getline(cin, row)) rows.push_back(row);
  307.  
  308.     for (int a = 0; a < rows.size(); a++)
  309.     {
  310.         bool bc = false;
  311.         row = rows.at(a);
  312.         word = "";
  313.         for (int b = 0; b < row.length(); b++)
  314.         {
  315.             char c = row.at(b);
  316.  
  317.             if (c == '%')
  318.             {
  319.                 if (bc)
  320.                 {
  321.                     bc = false;
  322.                     tokens.push_back(Token(a + 1, word));
  323.                 }
  324.                 break;
  325.             }
  326.             else if (c == ' ' || c == '\t')
  327.             {
  328.                 if (bc)
  329.                 {
  330.                     bc = false;
  331.                     tokens.push_back(Token(a + 1, word));
  332.                 }
  333.                 word = "";
  334.                 continue;
  335.             }
  336.             else if (c == '.')
  337.             {
  338.                     if(word != "")
  339.                         tokens.push_back(Token(a + 1, word));
  340.                     tokens.push_back(Token(a + 1, "."));
  341.                     word = "";
  342.                     bc = false;
  343.                     continue;
  344.             }
  345.             else if (word == "\"")
  346.             {
  347.                 if (bc)
  348.                 {
  349.                     bc = false;
  350.                     tokens.push_back(Token(a + 1, word));
  351.                 }
  352.                 word = "";
  353.             }
  354.  
  355.             bc = true;
  356.             word += caseupper(row.at(b));
  357.  
  358.             if(b == row.length() - 1) tokens.push_back(Token(a + 1, word));
  359.         }
  360.     }
  361.  
  362.     //for (int a = 0; a < tokens.size(); a++)
  363.         //cout << tokens.at(a).nr << " " << tokens.at(a).s << endl;
  364. }
  365.  
  366. char caseupper(char c)
  367. {
  368.     if (c > 96 && c < 123)
  369.         return char(c - 32);
  370.     return c;
  371. }
  372.  
  373. void syntaxerror(int nr)
  374. {
  375.     cout << "syntaxfel på rad " << nr << endl;
  376. }
Advertisement
Add Comment
Please, Sign In to add comment