Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::shared_ptr<Object> Read(Tokenizer* tokenizer);
- std::shared_ptr<Cell> ReadList(Tokenizer* tokenizer) {
- if (tokenizer->IsEnd()) {
- throw SyntaxError("nothing more to parse");
- }
- auto t = tokenizer->GetToken();
- // empty list
- if (t.index() == 1 && (std::get<BracketToken>(t) == BracketToken::CLOSE)) {
- tokenizer->Next();
- return nullptr;
- }
- //not empty
- auto first_elem = Read(tokenizer);
- auto t1 = tokenizer->GetToken(); //check a dot
- //pair
- if (t1.index() == 4) {
- tokenizer->Next(); //next after a dot
- auto second_elem = Read(tokenizer);
- std::shared_ptr<Cell> lst(new Cell(first_elem, second_elem));
- auto br = tokenizer->GetToken();
- if (br.index() != 1 || (std::get<BracketToken>(br) != BracketToken::CLOSE)) {
- throw SyntaxError("invalid pair syntax");
- } else {
- tokenizer->Next(); //next after a bracket
- return lst;
- }
- }
- //list
- if (tokenizer->IsEnd()) {
- throw SyntaxError("list unexpectedly finished");
- }
- //continued list
- std::vector<std::shared_ptr<Cell>> nodes;
- std::shared_ptr<Cell> lst(new Cell(first_elem, nullptr));
- nodes.push_back(lst);
- while(true) {
- auto nxt = tokenizer->GetToken();
- if (nxt.index() == 1 && (std::get<BracketToken>(nxt) == BracketToken::CLOSE)) {
- break;
- }
- auto second_elem = Read(tokenizer);
- nodes.emplace_back(new Cell(second_elem, nullptr));
- }
- auto rec = nodes.back();
- while (nodes.size() > 1) {
- nodes.pop_back();
- nodes.back()->tail_ = rec;
- rec = nodes.back();
- }
- return nodes.back();
- }
- std::shared_ptr<Object> Read(Tokenizer* tokenizer) {
- if (tokenizer->IsEnd()) {
- throw SyntaxError("nothing to parse");
- }
- auto t = tokenizer->GetToken();
- if (t.index() == 0) {
- tokenizer->Next();
- return std::shared_ptr<Number>(new Number(std::get<ConstantToken>(t).value_));
- } else if (t.index() == 1) {
- switch(std::get<BracketToken>(t)) {
- case BracketToken::CLOSE : throw SyntaxError("unexpected )");
- case BracketToken::OPEN : {
- std::cout << "reading a list: \n";
- tokenizer->Next();
- return ReadList(tokenizer);
- }
- }
- } else if (t.index() == 2) {
- tokenizer->Next();
- return std::shared_ptr<Symbol>(new Symbol(std::get<SymbolToken>(t).name_));
- } else if (t.index() == 3) {
- throw SyntaxError("unexpected '");
- } else if (t.index() == 4) {
- throw SyntaxError("unexpected .");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement