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