/* 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 . Copyright 2017-2020 Telegram Systems LLP */ #pragma once #include #include #include "IntCtx.h" #include "Continuation.h" namespace fift { using td::Ref; /* * * WORD CLASSES * */ typedef std::function StackWordFunc; typedef std::function CtxWordFunc; class StackWord : public FiftCont { StackWordFunc f; public: StackWord(StackWordFunc _f) : f(std::move(_f)) { } ~StackWord() override = default; Ref run_tail(IntCtx& ctx) const override; }; class CtxWord : public FiftCont { CtxWordFunc f; public: CtxWord(CtxWordFunc _f) : f(std::move(_f)) { } ~CtxWord() override = default; Ref run_tail(IntCtx& ctx) const override; }; typedef std::function(IntCtx&)> CtxTailWordFunc; class CtxTailWord : public FiftCont { CtxTailWordFunc f; public: CtxTailWord(CtxTailWordFunc _f) : f(std::move(_f)) { } ~CtxTailWord() override = default; Ref run_tail(IntCtx& ctx) const override; }; class WordList : public FiftCont { std::vector> list; public: ~WordList() override = default; WordList() = default; WordList(std::vector>&& _list); WordList(const std::vector>& _list); WordList& push_back(Ref word_def); WordList& push_back(FiftCont& wd); Ref run_tail(IntCtx& ctx) const override; void close(); bool is_list() const override { return true; } long long list_size() const override { return (long long)list.size(); } std::size_t size() const { return list.size(); } const Ref& at(std::size_t idx) const { return list.at(idx); } const Ref* get_list() const override { return list.data(); } WordList& append(const std::vector>& other); WordList& append(const Ref* begin, const Ref* end); WordList* make_copy() const override { return new WordList(list); } bool dump(std::ostream& os, const IntCtx& ctx) const override; }; class ListCont : public FiftCont { Ref next; Ref list; std::size_t pos; public: ListCont(Ref nxt, Ref wl, std::size_t p = 0) : next(std::move(nxt)), list(std::move(wl)), pos(p) { } ~ListCont() override = default; Ref run_tail(IntCtx& ctx) const override; Ref run_modify(IntCtx& ctx) override; Ref up() const override { return next; } bool dump(std::ostream& os, const IntCtx& ctx) const override; }; class DictEntry { Ref def; bool active; public: DictEntry() = delete; DictEntry(const DictEntry& ref) = default; DictEntry(DictEntry&& ref) = default; DictEntry(Ref _def, bool _act = false) : def(std::move(_def)), active(_act) { } DictEntry(StackWordFunc func); DictEntry(CtxWordFunc func, bool _act = false); DictEntry(CtxTailWordFunc func, bool _act = false); //DictEntry(const std::vector>& word_list); //DictEntry(std::vector>&& word_list); DictEntry& operator=(const DictEntry&) = default; DictEntry& operator=(DictEntry&&) = default; Ref get_def() const& { return def; } Ref get_def() && { return std::move(def); } bool is_active() const { return active; } }; /* DictEntry::DictEntry(const std::vector>& word_list) : def(Ref{true, word_list}) { } DictEntry::DictEntry(std::vector>&& word_list) : def(Ref{true, std::move(word_list)}) { } */ /* * * DICTIONARIES * */ class Dictionary { public: DictEntry* lookup(td::Slice name); void def_ctx_word(std::string name, CtxWordFunc func); void def_ctx_tail_word(std::string name, CtxTailWordFunc func); void def_active_word(std::string name, CtxWordFunc func); void def_stack_word(std::string name, StackWordFunc func); void def_word(std::string name, DictEntry word); void undef_word(td::Slice name); bool lookup_def(const FiftCont* cont, std::string* word_ptr = nullptr) const; bool lookup_def(Ref cont, std::string* word_ptr = nullptr) const { return lookup_def(cont.get(), word_ptr); } auto begin() const { return words_.begin(); } auto end() const { return words_.end(); } static Ref nop_word_def; private: std::map> words_; }; /* * * AUX FUNCTIONS FOR WORD DEFS * */ Ref pop_exec_token(vm::Stack& stack); Ref pop_word_list(vm::Stack& stack); void push_argcount(vm::Stack& stack, int args); } // namespace fift