mirror of
				https://github.com/ton-blockchain/ton
				synced 2025-03-09 15:40:10 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			398 lines
		
	
	
	
		
			9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			398 lines
		
	
	
	
		
			9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|     This file is part of TON Blockchain Library.
 | |
| 
 | |
|     TON Blockchain Library is free software: you can redistribute it and/or modify
 | |
|     it under the terms of the GNU Lesser General Public License as published by
 | |
|     the Free Software Foundation, either version 2 of the License, or
 | |
|     (at your option) any later version.
 | |
| 
 | |
|     TON Blockchain Library is distributed in the hope that it will be useful,
 | |
|     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
|     GNU Lesser General Public License for more details.
 | |
| 
 | |
|     You should have received a copy of the GNU Lesser General Public License
 | |
|     along with TON Blockchain Library.  If not, see <http://www.gnu.org/licenses/>.
 | |
| 
 | |
|     Copyright 2020 Telegram Systems LLP
 | |
| */
 | |
| #include "Continuation.h"
 | |
| #include "IntCtx.h"
 | |
| #include "Dictionary.h"
 | |
| 
 | |
| namespace fift {
 | |
| 
 | |
| //
 | |
| // FiftCont
 | |
| //
 | |
| bool FiftCont::print_dict_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   std::string word_name;
 | |
|   if (ctx.dictionary && ctx.dictionary->lookup_def(this, &word_name)) {
 | |
|     if (word_name.size() && word_name.back() == ' ') {
 | |
|       word_name.pop_back();
 | |
|     }
 | |
|     os << word_name;
 | |
|     return true;
 | |
|   }
 | |
|   return false;
 | |
| }
 | |
| 
 | |
| std::string FiftCont::get_dict_name(const IntCtx& ctx) const {
 | |
|   std::string word_name;
 | |
|   if (ctx.dictionary && ctx.dictionary->lookup_def(this, &word_name)) {
 | |
|     if (word_name.size() && word_name.back() == ' ') {
 | |
|       word_name.pop_back();
 | |
|     }
 | |
|     return word_name;
 | |
|   }
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| bool FiftCont::print_dummy_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<continuation " << (const void*)this << ">";
 | |
|   return false;
 | |
| }
 | |
| 
 | |
| bool FiftCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   return print_dict_name(os, ctx) || print_dummy_name(os, ctx);
 | |
| }
 | |
| 
 | |
| bool FiftCont::dump(std::ostream& os, const IntCtx& ctx) const {
 | |
|   bool ok = print_name(os, ctx);
 | |
|   os << std::endl;
 | |
|   return ok;
 | |
| }
 | |
| 
 | |
| //
 | |
| // QuitCont
 | |
| //
 | |
| Ref<FiftCont> QuitCont::run_tail(IntCtx& ctx) const {
 | |
|   ctx.set_exit_code(exit_code);
 | |
|   ctx.next.clear();
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| bool QuitCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<quit " << exit_code << ">";
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| //
 | |
| // SeqCont
 | |
| //
 | |
| Ref<FiftCont> SeqCont::run_tail(IntCtx& ctx) const {
 | |
|   ctx.next = seq(second, std::move(ctx.next));
 | |
|   return first;
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> SeqCont::run_modify(IntCtx& ctx) {
 | |
|   if (ctx.next.is_null()) {
 | |
|     ctx.next = std::move(second);
 | |
|     return std::move(first);
 | |
|   } else {
 | |
|     auto res = std::move(first);
 | |
|     first = std::move(second);
 | |
|     second = std::move(ctx.next);
 | |
|     ctx.next = self();
 | |
|     return res;
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool SeqCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   if (first.not_null()) {
 | |
|     return first->print_name(os, ctx);
 | |
|   } else {
 | |
|     return true;
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool SeqCont::dump(std::ostream& os, const IntCtx& ctx) const {
 | |
|   if (first.not_null()) {
 | |
|     return first->dump(os, ctx);
 | |
|   } else {
 | |
|     return true;
 | |
|   }
 | |
| }
 | |
| 
 | |
| //
 | |
| // TimesCont
 | |
| //
 | |
| Ref<FiftCont> TimesCont::run_tail(IntCtx& ctx) const {
 | |
|   if (count > 1) {
 | |
|     ctx.next = td::make_ref<TimesCont>(body, SeqCont::seq(after, std::move(ctx.next)), count - 1);
 | |
|   } else {
 | |
|     ctx.next = SeqCont::seq(after, std::move(ctx.next));
 | |
|   }
 | |
|   return body;
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> TimesCont::run_modify(IntCtx& ctx) {
 | |
|   if (ctx.next.not_null()) {
 | |
|     after = SeqCont::seq(std::move(after), std::move(ctx.next));
 | |
|   }
 | |
|   if (count > 1) {
 | |
|     --count;
 | |
|     ctx.next = self();
 | |
|     return body;
 | |
|   } else {
 | |
|     ctx.next = std::move(after);
 | |
|     return std::move(body);
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool TimesCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<repeat " << count << " times>";
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool TimesCont::dump(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<repeat " << count << " times:> ";
 | |
|   return body->dump(os, ctx);
 | |
| }
 | |
| 
 | |
| //
 | |
| // UntilCont
 | |
| //
 | |
| Ref<FiftCont> UntilCont::run_tail(IntCtx& ctx) const {
 | |
|   if (ctx.stack.pop_bool()) {
 | |
|     return after;
 | |
|   } else if (ctx.next.not_null()) {
 | |
|     ctx.next = td::make_ref<UntilCont>(body, SeqCont::seq(after, std::move(ctx.next)));
 | |
|     return body;
 | |
|   } else {
 | |
|     ctx.next = self();
 | |
|     return body;
 | |
|   }
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> UntilCont::run_modify(IntCtx& ctx) {
 | |
|   if (ctx.stack.pop_bool()) {
 | |
|     return std::move(after);
 | |
|   } else {
 | |
|     if (ctx.next.not_null()) {
 | |
|       after = SeqCont::seq(after, std::move(ctx.next));
 | |
|     }
 | |
|     ctx.next = self();
 | |
|     return body;
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool UntilCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<until loop continuation>";
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool UntilCont::dump(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<until loop continuation:> ";
 | |
|   return body->dump(os, ctx);
 | |
| }
 | |
| 
 | |
| //
 | |
| // WhileCont
 | |
| //
 | |
| Ref<FiftCont> WhileCont::run_tail(IntCtx& ctx) const {
 | |
|   if (!stage) {
 | |
|     ctx.next = td::make_ref<WhileCont>(cond, body, SeqCont::seq(after, std::move(ctx.next)), true);
 | |
|     return cond;
 | |
|   }
 | |
|   if (!ctx.stack.pop_bool()) {
 | |
|     return after;
 | |
|   } else {
 | |
|     ctx.next = td::make_ref<WhileCont>(cond, body, SeqCont::seq(after, std::move(ctx.next)));
 | |
|     return body;
 | |
|   }
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> WhileCont::run_modify(IntCtx& ctx) {
 | |
|   if (!stage) {
 | |
|     if (ctx.next.not_null()) {
 | |
|       after = SeqCont::seq(std::move(after), std::move(ctx.next));
 | |
|     }
 | |
|     stage = true;
 | |
|     ctx.next = self();
 | |
|     return cond;
 | |
|   }
 | |
|   if (!ctx.stack.pop_bool()) {
 | |
|     return std::move(after);
 | |
|   } else {
 | |
|     if (ctx.next.not_null()) {
 | |
|       after = SeqCont::seq(std::move(after), std::move(ctx.next));
 | |
|     }
 | |
|     stage = false;
 | |
|     ctx.next = self();
 | |
|     return body;
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool WhileCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<while loop " << (stage ? "body" : "condition") << ">";
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool WhileCont::dump(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<while loop " << (stage ? "body" : "condition") << ":> ";
 | |
|   return (stage ? body : cond)->dump(os, ctx);
 | |
| }
 | |
| 
 | |
| //
 | |
| // LoopCont
 | |
| //
 | |
| Ref<FiftCont> LoopCont::run_tail(IntCtx& ctx) const {
 | |
|   return Ref<FiftCont>(clone());
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> LoopCont::run_modify(IntCtx& ctx) {
 | |
|   if (ctx.next.not_null()) {
 | |
|     after = SeqCont::seq(std::move(after), std::move(ctx.next));
 | |
|   }
 | |
|   switch (state) {
 | |
|     case 0:
 | |
|       if (!init(ctx)) {
 | |
|         return std::move(after);
 | |
|       }
 | |
|       state = 1;
 | |
|       // fallthrough
 | |
|     case 1:
 | |
|       if (!pre_exec(ctx)) {
 | |
|         state = 3;
 | |
|         if (finalize(ctx)) {
 | |
|           return std::move(after);
 | |
|         } else {
 | |
|           return {};
 | |
|         }
 | |
|       }
 | |
|       state = 2;
 | |
|       ctx.next = self();
 | |
|       return func;
 | |
|     case 2:
 | |
|       if (post_exec(ctx)) {
 | |
|         state = 1;
 | |
|         return self();
 | |
|       }
 | |
|       state = 3;
 | |
|       // fallthrough
 | |
|     case 3:
 | |
|       if (finalize(ctx)) {
 | |
|         return std::move(after);
 | |
|       } else {
 | |
|         return {};
 | |
|       }
 | |
|     default:
 | |
|       throw IntError{"invalid LoopCont state"};
 | |
|   }
 | |
| }
 | |
| 
 | |
| bool LoopCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   os << "<generic loop continuation state " << state << ">";
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| //
 | |
| // GenericLitCont
 | |
| //
 | |
| bool GenericLitCont::print_name(std::ostream& os, const IntCtx& ctx) const {
 | |
|   auto list = get_literals();
 | |
|   bool sp = false;
 | |
|   for (auto entry : list) {
 | |
|     if (sp) {
 | |
|       os << sp;
 | |
|     }
 | |
|     sp = true;
 | |
|     int tp = entry.type();
 | |
|     if (entry.is_int() || entry.is(vm::StackEntry::t_string) || entry.is(vm::StackEntry::t_bytes)) {
 | |
|       entry.dump(os);
 | |
|     } else {
 | |
|       auto cont_lit = entry.as_object<FiftCont>();
 | |
|       if (cont_lit.not_null()) {
 | |
|         os << "{ ";
 | |
|         cont_lit->print_name(os, ctx);
 | |
|         os << " }";
 | |
|       } else {
 | |
|         os << "<literal of type " << tp << ">";
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| //
 | |
| // SmallIntLitCont
 | |
| //
 | |
| Ref<FiftCont> SmallIntLitCont::run_tail(IntCtx& ctx) const {
 | |
|   ctx.stack.push_smallint(value_);
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| std::vector<vm::StackEntry> SmallIntLitCont::get_literals() const {
 | |
|   return {td::make_refint(value_)};
 | |
| }
 | |
| 
 | |
| //
 | |
| // IntLitCont
 | |
| //
 | |
| Ref<FiftCont> IntLitCont::run_tail(IntCtx& ctx) const {
 | |
|   ctx.stack.push_int(value_);
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> IntLitCont::run_modify(IntCtx& ctx) {
 | |
|   ctx.stack.push_int(std::move(value_));
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> IntLitCont::literal(td::RefInt256 int_value) {
 | |
|   if (int_value->signed_fits_bits(64)) {
 | |
|     return literal(int_value->to_long());
 | |
|   } else {
 | |
|     return td::make_ref<IntLitCont>(std::move(int_value));
 | |
|   }
 | |
| }
 | |
| 
 | |
| //
 | |
| // LitCont
 | |
| //
 | |
| Ref<FiftCont> LitCont::run_tail(IntCtx& ctx) const {
 | |
|   ctx.stack.push(value_);
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> LitCont::run_modify(IntCtx& ctx) {
 | |
|   ctx.stack.push(std::move(value_));
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> LitCont::literal(vm::StackEntry value) {
 | |
|   if (value.is_int()) {
 | |
|     return literal(std::move(value).as_int());
 | |
|   } else {
 | |
|     return td::make_ref<LitCont>(std::move(value));
 | |
|   }
 | |
| }
 | |
| 
 | |
| //
 | |
| // MultiLitCont
 | |
| //
 | |
| Ref<FiftCont> MultiLitCont::run_tail(IntCtx& ctx) const {
 | |
|   for (auto& value : values_) {
 | |
|     ctx.stack.push(value);
 | |
|   }
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| Ref<FiftCont> MultiLitCont::run_modify(IntCtx& ctx) {
 | |
|   for (auto& value : values_) {
 | |
|     ctx.stack.push(std::move(value));
 | |
|   }
 | |
|   return {};
 | |
| }
 | |
| 
 | |
| MultiLitCont& MultiLitCont::push_back(vm::StackEntry new_literal) {
 | |
|   values_.push_back(std::move(new_literal));
 | |
|   return *this;
 | |
| }
 | |
| 
 | |
| vm::StackEntry MultiLitCont::at(int idx) const {
 | |
|   return values_.at(idx);
 | |
| }
 | |
| 
 | |
| }  // namespace fift
 |