1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

[Tolk] Get rid of ~tilda with mutate and self methods

This is a very big change.
If FunC has `.methods()` and `~methods()`, Tolk has only dot,
one and only way to call a `.method()`.
A method may mutate an object, or may not.
It's a behavioral and semantic difference from FunC.

- `cs.loadInt(32)` modifies a slice and returns an integer
- `b.storeInt(x, 32)` modifies a builder
- `b = b.storeInt()` also works, since it not only modifies, but returns
- chained methods also work, they return `self`
- everything works exactly as expected, similar to JS
- no runtime overhead, exactly same Fift instructions
- custom methods are created with ease
- tilda `~` does not exist in Tolk at all
This commit is contained in:
tolk-vm 2024-10-31 11:18:54 +04:00
parent 12ff28ac94
commit d9dba320cc
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
85 changed files with 2710 additions and 1965 deletions

View file

@ -323,8 +323,7 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
static TokenType maybe_keyword(std::string_view str) {
switch (str.size()) {
case 1:
if (str == "~") return tok_bitwise_not; // todo attention
if (str == "_") return tok_underscore; // todo attention
if (str == "_") return tok_underscore;
break;
case 2:
if (str == "do") return tok_do;
@ -347,6 +346,7 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
if (str == "void") return tok_void;
if (str == "bool") return tok_bool;
if (str == "auto") return tok_auto;
if (str == "self") return tok_self;
if (str == "tolk") return tok_tolk;
if (str == "type") return tok_type;
if (str == "enum") return tok_enum;
@ -368,6 +368,7 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
if (str == "assert") return tok_assert;
if (str == "import") return tok_import;
if (str == "global") return tok_global;
if (str == "mutate") return tok_mutate;
if (str == "repeat") return tok_repeat;
if (str == "struct") return tok_struct;
if (str == "export") return tok_export;
@ -394,8 +395,7 @@ struct ChunkIdentifierOrKeyword final : ChunkLexerBase {
lex->skip_chars(1);
while (!lex->is_eof()) {
char c = lex->char_at();
// the pattern of valid identifier first symbol is provided in trie, here we test for identifier middle
bool allowed_in_identifier = std::isalnum(c) || c == '_' || c == '$' || c == '?' || c == '!' || c == '\'';
bool allowed_in_identifier = std::isalnum(c) || c == '_' || c == '$';
if (!allowed_in_identifier) {
break;
}
@ -438,28 +438,6 @@ struct ChunkIdentifierInBackticks final : ChunkLexerBase {
}
};
// Handle ~`some_method` and .`some_method` todo to be removed later
struct ChunkDotTildeAndBackticks final : ChunkLexerBase {
bool parse(Lexer* lex) const override {
const char* str_begin = lex->c_str();
lex->skip_chars(2);
while (!lex->is_eof() && lex->char_at() != '`' && lex->char_at() != '\n') {
lex->skip_chars(1);
}
if (lex->char_at() != '`') {
lex->error("unclosed backtick `");
}
std::string_view in_backticks(str_begin + 2, lex->c_str() - str_begin - 2);
std::string full = std::string(1, *str_begin) + static_cast<std::string>(in_backticks);
std::string* allocated = new std::string(full);
lex->skip_chars(1);
std::string_view str_val(allocated->c_str(), allocated->size());
lex->add_token(tok_identifier, str_val);
return true;
}
};
//
// ----------------------------------------------------------------------
// Here we define a grammar of Tolk.
@ -500,11 +478,8 @@ struct TolkLanguageGrammar {
trie.add_prefix("\n", singleton<ChunkSkipWhitespace>());
trie.add_pattern("[0-9]", singleton<ChunkNumber>());
// todo think of . ~
trie.add_pattern("[a-zA-Z_$.~]", singleton<ChunkIdentifierOrKeyword>());
trie.add_pattern("[a-zA-Z_$]", singleton<ChunkIdentifierOrKeyword>());
trie.add_prefix("`", singleton<ChunkIdentifierInBackticks>());
// todo to be removed after ~ becomes invalid and . becomes a separate token
trie.add_pattern("[.~]`", singleton<ChunkDotTildeAndBackticks>());
register_token("+", 1, tok_plus);
register_token("-", 1, tok_minus);
@ -528,6 +503,8 @@ struct TolkLanguageGrammar {
register_token("&", 1, tok_bitwise_and);
register_token("|", 1, tok_bitwise_or);
register_token("^", 1, tok_bitwise_xor);
register_token("~", 1, tok_bitwise_not);
register_token(".", 1, tok_dot);
register_token("==", 2, tok_eq);
register_token("!=", 2, tok_neq);
register_token("<=", 2, tok_leq);