Advertisement
anechka_ne_plach

readread

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