Advertisement
anechka_ne_plach

Untitled

Nov 14th, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. std::shared_ptr<Object> Read(Tokenizer* tokenizer);
  2.  
  3. std::shared_ptr<Cell> ReadList(Tokenizer* tokenizer) {
  4.     if (tokenizer->IsEnd()) {
  5.         throw SyntaxError("nothing more to parse");
  6.     }
  7.     auto t = tokenizer->GetToken();
  8.     // empty list
  9.     if (t.index() == 1 && (std::get<BracketToken>(t) == BracketToken::CLOSE)) {
  10.         tokenizer->Next();
  11.         return nullptr;
  12.     }
  13.     //not empty list or pair
  14.     auto first_elem = Read(tokenizer);
  15.     if (tokenizer->IsEnd()) {
  16.         throw SyntaxError("list unexpectedly finished");
  17.     }
  18.     std::vector<std::shared_ptr<Cell>> nodes;
  19.     std::shared_ptr<Cell> lst(new Cell(first_elem, nullptr));
  20.     nodes.push_back(lst);
  21.     while(true) {
  22.         auto nxt = tokenizer->GetToken();
  23.         //end of list
  24.         if (nxt.index() == 1 && (std::get<BracketToken>(nxt) == BracketToken::CLOSE)) {
  25.             tokenizer->Next();
  26.             break;
  27.         //pair
  28.         } else if (nxt.index() == 4) {
  29.             auto last = nodes.back();
  30.             nodes.pop_back();
  31.             tokenizer->Next(); //next after a dot
  32.             auto second_elem = Read(tokenizer);
  33.             std::shared_ptr<Cell> p(new Cell(last->head_, second_elem));
  34.             auto br = tokenizer->GetToken();
  35.             if (br.index() != 1 || (std::get<BracketToken>(br) != BracketToken::CLOSE)) {
  36.                 throw SyntaxError("invalid pair syntax");
  37.             }
  38.             nodes.push_back(p);
  39.         } else {
  40.             auto second_elem = Read(tokenizer);
  41.             nodes.emplace_back(new Cell(second_elem, nullptr));
  42.         }
  43.     }
  44.     auto rec = nodes.back();
  45.     while (nodes.size() > 1) {
  46.         nodes.pop_back();
  47.         nodes.back()->tail_ = rec;
  48.         rec = nodes.back();
  49.     }
  50.     return nodes.back();
  51. }
  52.  
  53. std::shared_ptr<Object> Read(Tokenizer* tokenizer) {
  54.     if (tokenizer->IsEnd()) {
  55.         throw SyntaxError("nothing to parse");
  56.     }
  57.     auto t = tokenizer->GetToken();
  58.     tokenizer->Next();
  59.     if (t.index() == 0) {
  60.         return std::shared_ptr<Number>(new Number(std::get<ConstantToken>(t).value_));
  61.     } else if (t.index() == 1) {
  62.         switch(std::get<BracketToken>(t)) {
  63.             case BracketToken::CLOSE : throw SyntaxError("unexpected )");
  64.             case BracketToken::OPEN : {
  65.                 std::cout << "reading a list: \n";
  66.                 return ReadList(tokenizer);
  67.             }
  68.         }
  69.     } else if (t.index() == 2) {
  70.         return std::shared_ptr<Symbol>(new Symbol(std::get<SymbolToken>(t).name_));
  71.     } else if (t.index() == 3) {
  72.         throw SyntaxError("unexpected '");
  73.     } else if (t.index() == 4) {
  74.         throw SyntaxError("unexpected .");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement