mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
minor improvements and bugfixes
This commit is contained in:
parent
eecf05ca59
commit
040df63c98
24 changed files with 665 additions and 344 deletions
|
@ -94,41 +94,41 @@ WordList& WordList::append(const std::vector<Ref<WordDef>>& other) {
|
|||
}
|
||||
|
||||
//
|
||||
// WordRef
|
||||
// DictEntry
|
||||
//
|
||||
|
||||
WordRef::WordRef(Ref<WordDef> _def, bool _act) : def(std::move(_def)), active(_act) {
|
||||
DictEntry::DictEntry(Ref<WordDef> _def, bool _act) : def(std::move(_def)), active(_act) {
|
||||
}
|
||||
|
||||
WordRef::WordRef(StackWordFunc func) : def(Ref<StackWord>{true, std::move(func)}), active(false) {
|
||||
DictEntry::DictEntry(StackWordFunc func) : def(Ref<StackWord>{true, std::move(func)}), active(false) {
|
||||
}
|
||||
|
||||
WordRef::WordRef(CtxWordFunc func, bool _act) : def(Ref<CtxWord>{true, std::move(func)}), active(_act) {
|
||||
DictEntry::DictEntry(CtxWordFunc func, bool _act) : def(Ref<CtxWord>{true, std::move(func)}), active(_act) {
|
||||
}
|
||||
|
||||
WordRef::WordRef(CtxTailWordFunc func, bool _act) : def(Ref<CtxTailWord>{true, std::move(func)}), active(_act) {
|
||||
DictEntry::DictEntry(CtxTailWordFunc func, bool _act) : def(Ref<CtxTailWord>{true, std::move(func)}), active(_act) {
|
||||
}
|
||||
|
||||
Ref<WordDef> WordRef::get_def() const & {
|
||||
Ref<WordDef> DictEntry::get_def() const& {
|
||||
return def;
|
||||
}
|
||||
|
||||
Ref<WordDef> WordRef::get_def() && {
|
||||
Ref<WordDef> DictEntry::get_def() && {
|
||||
return std::move(def);
|
||||
}
|
||||
|
||||
void WordRef::operator()(IntCtx& ctx) const {
|
||||
void DictEntry::operator()(IntCtx& ctx) const {
|
||||
def->run(ctx);
|
||||
}
|
||||
|
||||
bool WordRef::is_active() const {
|
||||
bool DictEntry::is_active() const {
|
||||
return active;
|
||||
}
|
||||
|
||||
//
|
||||
// Dictionary
|
||||
//
|
||||
WordRef* Dictionary::lookup(td::Slice name) {
|
||||
DictEntry* Dictionary::lookup(td::Slice name) {
|
||||
auto it = words_.find(name);
|
||||
if (it == words_.end()) {
|
||||
return nullptr;
|
||||
|
@ -153,7 +153,7 @@ void Dictionary::def_ctx_tail_word(std::string name, CtxTailWordFunc func) {
|
|||
def_word(std::move(name), std::move(func));
|
||||
}
|
||||
|
||||
void Dictionary::def_word(std::string name, WordRef word) {
|
||||
void Dictionary::def_word(std::string name, DictEntry word) {
|
||||
auto res = words_.emplace(name, std::move(word));
|
||||
LOG_IF(FATAL, !res.second) << "Cannot redefine word: " << name;
|
||||
}
|
||||
|
|
|
@ -110,34 +110,34 @@ class WordList : public WordDef {
|
|||
}
|
||||
};
|
||||
|
||||
class WordRef {
|
||||
class DictEntry {
|
||||
Ref<WordDef> def;
|
||||
bool active;
|
||||
|
||||
public:
|
||||
WordRef() = delete;
|
||||
WordRef(const WordRef& ref) = default;
|
||||
WordRef(WordRef&& ref) = default;
|
||||
WordRef(Ref<WordDef> _def, bool _act = false);
|
||||
WordRef(StackWordFunc func);
|
||||
WordRef(CtxWordFunc func, bool _act = false);
|
||||
WordRef(CtxTailWordFunc func, bool _act = false);
|
||||
//WordRef(const std::vector<Ref<WordDef>>& word_list);
|
||||
//WordRef(std::vector<Ref<WordDef>>&& word_list);
|
||||
WordRef& operator=(const WordRef&) = default;
|
||||
WordRef& operator=(WordRef&&) = default;
|
||||
Ref<WordDef> get_def() const &;
|
||||
DictEntry() = delete;
|
||||
DictEntry(const DictEntry& ref) = default;
|
||||
DictEntry(DictEntry&& ref) = default;
|
||||
DictEntry(Ref<WordDef> _def, bool _act = false);
|
||||
DictEntry(StackWordFunc func);
|
||||
DictEntry(CtxWordFunc func, bool _act = false);
|
||||
DictEntry(CtxTailWordFunc func, bool _act = false);
|
||||
//DictEntry(const std::vector<Ref<WordDef>>& word_list);
|
||||
//DictEntry(std::vector<Ref<WordDef>>&& word_list);
|
||||
DictEntry& operator=(const DictEntry&) = default;
|
||||
DictEntry& operator=(DictEntry&&) = default;
|
||||
Ref<WordDef> get_def() const&;
|
||||
Ref<WordDef> get_def() &&;
|
||||
void operator()(IntCtx& ctx) const;
|
||||
bool is_active() const;
|
||||
~WordRef() = default;
|
||||
~DictEntry() = default;
|
||||
};
|
||||
|
||||
/*
|
||||
WordRef::WordRef(const std::vector<Ref<WordDef>>& word_list) : def(Ref<WordList>{true, word_list}) {
|
||||
DictEntry::DictEntry(const std::vector<Ref<WordDef>>& word_list) : def(Ref<WordList>{true, word_list}) {
|
||||
}
|
||||
|
||||
WordRef::WordRef(std::vector<Ref<WordDef>>&& word_list) : def(Ref<WordList>{true, std::move(word_list)}) {
|
||||
DictEntry::DictEntry(std::vector<Ref<WordDef>>&& word_list) : def(Ref<WordList>{true, std::move(word_list)}) {
|
||||
}
|
||||
*/
|
||||
|
||||
|
@ -149,12 +149,12 @@ WordRef::WordRef(std::vector<Ref<WordDef>>&& word_list) : def(Ref<WordList>{true
|
|||
|
||||
class Dictionary {
|
||||
public:
|
||||
WordRef* lookup(td::Slice name);
|
||||
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, WordRef word);
|
||||
void def_word(std::string name, DictEntry word);
|
||||
void undef_word(td::Slice name);
|
||||
|
||||
auto begin() const {
|
||||
|
@ -167,7 +167,7 @@ class Dictionary {
|
|||
static Ref<WordDef> nop_word_def;
|
||||
|
||||
private:
|
||||
std::map<std::string, WordRef, std::less<>> words_;
|
||||
std::map<std::string, DictEntry, std::less<>> words_;
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -171,26 +171,26 @@ void IntCtx::skipspc(bool skip_eol) {
|
|||
} while (load_next_line());
|
||||
}
|
||||
|
||||
void check_compile(const IntCtx& ctx) {
|
||||
if (ctx.state <= 0) {
|
||||
void IntCtx::check_compile() const {
|
||||
if (state <= 0) {
|
||||
throw IntError{"compilation mode only"};
|
||||
}
|
||||
}
|
||||
|
||||
void check_execute(const IntCtx& ctx) {
|
||||
if (ctx.state != 0) {
|
||||
void IntCtx::check_execute() const {
|
||||
if (state != 0) {
|
||||
throw IntError{"interpret mode only"};
|
||||
}
|
||||
}
|
||||
|
||||
void check_not_int_exec(const IntCtx& ctx) {
|
||||
if (ctx.state < 0) {
|
||||
void IntCtx::check_not_int_exec() const {
|
||||
if (state < 0) {
|
||||
throw IntError{"not allowed in internal interpret mode"};
|
||||
}
|
||||
}
|
||||
|
||||
void check_int_exec(const IntCtx& ctx) {
|
||||
if (ctx.state >= 0) {
|
||||
void IntCtx::check_int_exec() const {
|
||||
if (state >= 0) {
|
||||
throw IntError{"internal interpret mode only"};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,12 +141,12 @@ struct IntCtx {
|
|||
Savepoint(IntCtx& _ctx, std::string new_filename, std::string new_current_dir, std::istream* new_input_stream);
|
||||
~Savepoint();
|
||||
};
|
||||
};
|
||||
|
||||
void check_compile(const IntCtx& ctx);
|
||||
void check_execute(const IntCtx& ctx);
|
||||
void check_not_int_exec(const IntCtx& ctx);
|
||||
void check_int_exec(const IntCtx& ctx);
|
||||
void check_compile() const;
|
||||
void check_execute() const;
|
||||
void check_not_int_exec() const;
|
||||
void check_int_exec() const;
|
||||
};
|
||||
|
||||
td::StringBuilder& operator<<(td::StringBuilder& os, const IntCtx& ctx);
|
||||
std::ostream& operator<<(std::ostream& os, const IntCtx& ctx);
|
||||
|
|
|
@ -1782,7 +1782,7 @@ void interpret_wordlist_begin_aux(vm::Stack& stack) {
|
|||
}
|
||||
|
||||
void interpret_wordlist_begin(IntCtx& ctx) {
|
||||
check_not_int_exec(ctx);
|
||||
ctx.check_not_int_exec();
|
||||
interpret_wordlist_begin_aux(ctx.stack);
|
||||
push_argcount(ctx, 0);
|
||||
++(ctx.state);
|
||||
|
@ -1795,20 +1795,20 @@ void interpret_wordlist_end_aux(vm::Stack& stack) {
|
|||
}
|
||||
|
||||
void interpret_wordlist_end(IntCtx& ctx) {
|
||||
check_compile(ctx);
|
||||
ctx.check_compile();
|
||||
interpret_wordlist_end_aux(ctx.stack);
|
||||
push_argcount(ctx, 1);
|
||||
--(ctx.state);
|
||||
}
|
||||
|
||||
void interpret_internal_interpret_begin(IntCtx& ctx) {
|
||||
check_compile(ctx);
|
||||
ctx.check_compile();
|
||||
push_argcount(ctx, 0);
|
||||
ctx.state = -ctx.state;
|
||||
}
|
||||
|
||||
void interpret_internal_interpret_end(IntCtx& ctx) {
|
||||
check_int_exec(ctx);
|
||||
ctx.check_int_exec();
|
||||
ctx.state = -ctx.state;
|
||||
ctx.stack.push({vm::from_object, Dictionary::nop_word_def});
|
||||
}
|
||||
|
@ -1830,9 +1830,9 @@ void interpret_create_aux(IntCtx& ctx, int mode) {
|
|||
bool active = (mode & 1);
|
||||
auto entry = ctx.dictionary->lookup(word);
|
||||
if (entry) {
|
||||
*entry = WordRef{wd_ref, active}; // redefine word
|
||||
*entry = DictEntry{std::move(wd_ref), active}; // redefine word
|
||||
} else {
|
||||
ctx.dictionary->def_word(std::move(word), {wd_ref, active});
|
||||
ctx.dictionary->def_word(std::move(word), {std::move(wd_ref), active});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue