Advertisement
anechka_ne_plach

readreadread

Nov 14th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 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.             break;
  41.         }
  42.         auto second_elem = Read(tokenizer);
  43.         nodes.emplace_back(new Cell(second_elem, nullptr));
  44.     }
  45.     auto rec = nodes.back();
  46.     while (nodes.size() > 1) {
  47.         nodes.pop_back();
  48.         nodes.back()->tail_ = rec;
  49.         rec = nodes.back();
  50.     }
  51.     return nodes.back();
  52. }
  53.  
  54. std::shared_ptr<Object> Read(Tokenizer* tokenizer) {
  55.     if (tokenizer->IsEnd()) {
  56.         throw SyntaxError("nothing to parse");
  57.     }
  58.     auto t = tokenizer->GetToken();
  59.     if (t.index() == 0) {
  60.         tokenizer->Next();
  61.         return std::shared_ptr<Number>(new Number(std::get<ConstantToken>(t).value_));
  62.     } else if (t.index() == 1) {
  63.         switch(std::get<BracketToken>(t)) {
  64.             case BracketToken::CLOSE : throw SyntaxError("unexpected )");
  65.             case BracketToken::OPEN : {
  66.                 std::cout << "reading a list: \n";
  67.                 tokenizer->Next();
  68.                 return ReadList(tokenizer);
  69.             }
  70.         }
  71.     } else if (t.index() == 2) {
  72.         tokenizer->Next();
  73.         return std::shared_ptr<Symbol>(new Symbol(std::get<SymbolToken>(t).name_));
  74.     } else if (t.index() == 3) {
  75.         throw SyntaxError("unexpected '");
  76.     } else if (t.index() == 4) {
  77.         throw SyntaxError("unexpected .");
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement