mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
integrating the existing state of TON Storage / TON Payments / CPS Fift development branches
This commit is contained in:
parent
040df63c98
commit
4e2624459b
153 changed files with 10760 additions and 1695 deletions
|
@ -20,20 +20,10 @@
|
|||
|
||||
namespace fift {
|
||||
|
||||
//
|
||||
// WordDef
|
||||
//
|
||||
void WordDef::run(IntCtx& ctx) const {
|
||||
auto next = run_tail(ctx);
|
||||
while (next.not_null()) {
|
||||
next = next->run_tail(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// StackWord
|
||||
//
|
||||
Ref<WordDef> StackWord::run_tail(IntCtx& ctx) const {
|
||||
Ref<FiftCont> StackWord::run_tail(IntCtx& ctx) const {
|
||||
f(ctx.stack);
|
||||
return {};
|
||||
}
|
||||
|
@ -41,7 +31,7 @@ Ref<WordDef> StackWord::run_tail(IntCtx& ctx) const {
|
|||
//
|
||||
// CtxWord
|
||||
//
|
||||
Ref<WordDef> CtxWord::run_tail(IntCtx& ctx) const {
|
||||
Ref<FiftCont> CtxWord::run_tail(IntCtx& ctx) const {
|
||||
f(ctx);
|
||||
return {};
|
||||
}
|
||||
|
@ -49,57 +39,125 @@ Ref<WordDef> CtxWord::run_tail(IntCtx& ctx) const {
|
|||
//
|
||||
// CtxTailWord
|
||||
//
|
||||
Ref<WordDef> CtxTailWord::run_tail(IntCtx& ctx) const {
|
||||
Ref<FiftCont> CtxTailWord::run_tail(IntCtx& ctx) const {
|
||||
return f(ctx);
|
||||
}
|
||||
|
||||
//
|
||||
// WordList
|
||||
//
|
||||
WordList::WordList(std::vector<Ref<WordDef>>&& _list) : list(std::move(_list)) {
|
||||
WordList::WordList(std::vector<Ref<FiftCont>>&& _list) : list(std::move(_list)) {
|
||||
}
|
||||
|
||||
WordList::WordList(const std::vector<Ref<WordDef>>& _list) : list(_list) {
|
||||
WordList::WordList(const std::vector<Ref<FiftCont>>& _list) : list(_list) {
|
||||
}
|
||||
|
||||
WordList& WordList::push_back(Ref<WordDef> word_def) {
|
||||
WordList& WordList::push_back(Ref<FiftCont> word_def) {
|
||||
list.push_back(std::move(word_def));
|
||||
return *this;
|
||||
}
|
||||
|
||||
WordList& WordList::push_back(WordDef& wd) {
|
||||
WordList& WordList::push_back(FiftCont& wd) {
|
||||
list.emplace_back(&wd);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ref<WordDef> WordList::run_tail(IntCtx& ctx) const {
|
||||
Ref<FiftCont> WordList::run_tail(IntCtx& ctx) const {
|
||||
if (list.empty()) {
|
||||
return {};
|
||||
}
|
||||
auto it = list.cbegin(), it2 = list.cend() - 1;
|
||||
while (it < it2) {
|
||||
(*it)->run(ctx);
|
||||
++it;
|
||||
if (list.size() > 1) {
|
||||
ctx.next = td::make_ref<ListCont>(std::move(ctx.next), Ref<WordList>(this), 1);
|
||||
}
|
||||
return *it;
|
||||
return list[0];
|
||||
}
|
||||
|
||||
void WordList::close() {
|
||||
list.shrink_to_fit();
|
||||
}
|
||||
|
||||
WordList& WordList::append(const std::vector<Ref<WordDef>>& other) {
|
||||
WordList& WordList::append(const std::vector<Ref<FiftCont>>& other) {
|
||||
list.insert(list.end(), other.begin(), other.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
WordList& WordList::append(const Ref<FiftCont>* begin, const Ref<FiftCont>* end) {
|
||||
list.insert(list.end(), begin, end);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool WordList::dump(std::ostream& os, const IntCtx& ctx) const {
|
||||
os << "{";
|
||||
for (auto entry : list) {
|
||||
os << ' ';
|
||||
entry->print_name(os, ctx);
|
||||
}
|
||||
os << " }" << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// ListCont
|
||||
//
|
||||
|
||||
Ref<FiftCont> ListCont::run_tail(IntCtx& ctx) const {
|
||||
auto sz = list->size();
|
||||
if (pos >= sz) {
|
||||
return std::move(ctx.next);
|
||||
} else if (ctx.next.not_null()) {
|
||||
ctx.next = td::make_ref<ListCont>(SeqCont::seq(next, std::move(ctx.next)), list, pos + 1);
|
||||
} else if (pos + 1 == sz) {
|
||||
ctx.next = next;
|
||||
} else {
|
||||
ctx.next = td::make_ref<ListCont>(next, list, pos + 1);
|
||||
}
|
||||
return list->at(pos);
|
||||
}
|
||||
|
||||
Ref<FiftCont> ListCont::run_modify(IntCtx& ctx) {
|
||||
auto sz = list->size();
|
||||
if (pos >= sz) {
|
||||
return std::move(ctx.next);
|
||||
}
|
||||
auto cur = list->at(pos++);
|
||||
if (ctx.next.not_null()) {
|
||||
next = SeqCont::seq(next, std::move(ctx.next));
|
||||
}
|
||||
if (pos == sz) {
|
||||
ctx.next = std::move(next);
|
||||
} else {
|
||||
ctx.next = self();
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
bool ListCont::dump(std::ostream& os, const IntCtx& ctx) const {
|
||||
std::string dict_name = list->get_dict_name(ctx);
|
||||
if (!dict_name.empty()) {
|
||||
os << "[in " << dict_name << ":] ";
|
||||
}
|
||||
std::size_t sz = list->size(), i, a = (pos >= 16 ? pos - 16 : 0), b = std::min(pos + 16, sz);
|
||||
if (a > 0) {
|
||||
os << "... ";
|
||||
}
|
||||
for (i = a; i < b; i++) {
|
||||
if (i == pos) {
|
||||
os << "**HERE** ";
|
||||
}
|
||||
list->at(i)->print_name(os, ctx);
|
||||
os << ' ';
|
||||
}
|
||||
if (b < sz) {
|
||||
os << "...";
|
||||
}
|
||||
os << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// DictEntry
|
||||
//
|
||||
|
||||
DictEntry::DictEntry(Ref<WordDef> _def, bool _act) : def(std::move(_def)), active(_act) {
|
||||
}
|
||||
|
||||
DictEntry::DictEntry(StackWordFunc func) : def(Ref<StackWord>{true, std::move(func)}), active(false) {
|
||||
}
|
||||
|
||||
|
@ -109,22 +167,6 @@ DictEntry::DictEntry(CtxWordFunc func, bool _act) : def(Ref<CtxWord>{true, std::
|
|||
DictEntry::DictEntry(CtxTailWordFunc func, bool _act) : def(Ref<CtxTailWord>{true, std::move(func)}), active(_act) {
|
||||
}
|
||||
|
||||
Ref<WordDef> DictEntry::get_def() const& {
|
||||
return def;
|
||||
}
|
||||
|
||||
Ref<WordDef> DictEntry::get_def() && {
|
||||
return std::move(def);
|
||||
}
|
||||
|
||||
void DictEntry::operator()(IntCtx& ctx) const {
|
||||
def->run(ctx);
|
||||
}
|
||||
|
||||
bool DictEntry::is_active() const {
|
||||
return active;
|
||||
}
|
||||
|
||||
//
|
||||
// Dictionary
|
||||
//
|
||||
|
@ -141,7 +183,7 @@ void Dictionary::def_ctx_word(std::string name, CtxWordFunc func) {
|
|||
}
|
||||
|
||||
void Dictionary::def_active_word(std::string name, CtxWordFunc func) {
|
||||
Ref<WordDef> wdef = Ref<CtxWord>{true, std::move(func)};
|
||||
Ref<FiftCont> wdef = Ref<CtxWord>{true, std::move(func)};
|
||||
def_word(std::move(name), {std::move(wdef), true});
|
||||
}
|
||||
|
||||
|
@ -166,17 +208,32 @@ void Dictionary::undef_word(td::Slice name) {
|
|||
words_.erase(it);
|
||||
}
|
||||
|
||||
bool Dictionary::lookup_def(const FiftCont* cont, std::string* word_ptr) const {
|
||||
if (!cont) {
|
||||
return false;
|
||||
}
|
||||
for (const auto& entry : words_) {
|
||||
if (entry.second.get_def().get() == cont) {
|
||||
if (word_ptr) {
|
||||
*word_ptr = entry.first;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void interpret_nop(vm::Stack& stack) {
|
||||
}
|
||||
|
||||
Ref<WordDef> Dictionary::nop_word_def = Ref<StackWord>{true, interpret_nop};
|
||||
Ref<FiftCont> Dictionary::nop_word_def = Ref<StackWord>{true, interpret_nop};
|
||||
|
||||
//
|
||||
// functions for wordef
|
||||
//
|
||||
Ref<WordDef> pop_exec_token(vm::Stack& stack) {
|
||||
Ref<FiftCont> pop_exec_token(vm::Stack& stack) {
|
||||
stack.check_underflow(1);
|
||||
auto wd_ref = stack.pop().as_object<WordDef>();
|
||||
auto wd_ref = stack.pop().as_object<FiftCont>();
|
||||
if (wd_ref.is_null()) {
|
||||
throw IntError{"execution token expected"};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue