2024-10-31 06:51:07 +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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
#include "platform-utils.h"
|
|
|
|
#include "src-file.h"
|
|
|
|
#include <string>
|
2024-10-31 06:51:07 +00:00
|
|
|
|
|
|
|
namespace tolk {
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
enum TokenType {
|
|
|
|
tok_empty,
|
|
|
|
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_fun,
|
|
|
|
tok_get,
|
|
|
|
tok_type,
|
|
|
|
tok_enum,
|
|
|
|
tok_struct,
|
|
|
|
tok_operator,
|
|
|
|
tok_infix,
|
|
|
|
|
|
|
|
tok_global,
|
|
|
|
tok_const,
|
|
|
|
tok_var,
|
|
|
|
tok_val,
|
|
|
|
tok_redef,
|
2024-10-31 07:18:54 +00:00
|
|
|
tok_mutate,
|
|
|
|
tok_self,
|
2024-10-31 07:11:41 +00:00
|
|
|
|
|
|
|
tok_annotation_at,
|
|
|
|
tok_colon,
|
|
|
|
tok_asm,
|
|
|
|
tok_builtin,
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_int_const,
|
|
|
|
tok_string_const,
|
|
|
|
tok_string_modifier,
|
2024-10-31 07:03:33 +00:00
|
|
|
tok_true,
|
|
|
|
tok_false,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_null,
|
|
|
|
|
|
|
|
tok_identifier,
|
2024-10-31 07:18:54 +00:00
|
|
|
tok_dot,
|
2024-10-31 07:03:33 +00:00
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_plus,
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
tok_set_plus,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_minus,
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
tok_set_minus,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_mul,
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
tok_set_mul,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_div,
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
tok_set_div,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_mod,
|
[Tolk] AST-based semantic analysis, get rid of Expr
This is a huge refactoring focusing on untangling compiler internals
(previously forked from FunC).
The goal is to convert AST directly to Op (a kind of IR representation),
doing all code analysis at AST level.
Noteable changes:
- AST-based semantic kernel includes: registering global symbols,
scope handling and resolving local/global identifiers,
lvalue/rvalue calc and check, implicit return detection,
mutability analysis, pure/impure validity checks,
simple constant folding
- values of `const` variables are calculated NOT based on CodeBlob,
but via a newly-introduced AST-based constant evaluator
- AST vertices are now inherited from expression/statement/other;
expression vertices have common properties (TypeExpr, lvalue/rvalue)
- symbol table is rewritten completely, SymDef/SymVal no longer exist,
lexer now doesn't need to register identifiers
- AST vertices have references to symbols, filled at different
stages of pipeline
- the remaining "FunC legacy part" is almost unchanged besides Expr
which was fully dropped; AST is converted to Ops (IR) directly
2024-12-16 18:19:45 +00:00
|
|
|
tok_set_mod,
|
|
|
|
tok_lshift,
|
|
|
|
tok_set_lshift,
|
|
|
|
tok_rshift,
|
|
|
|
tok_set_rshift,
|
|
|
|
tok_rshiftR,
|
|
|
|
tok_rshiftC,
|
|
|
|
tok_bitwise_and,
|
|
|
|
tok_set_bitwise_and,
|
|
|
|
tok_bitwise_or,
|
|
|
|
tok_set_bitwise_or,
|
|
|
|
tok_bitwise_xor,
|
|
|
|
tok_set_bitwise_xor,
|
|
|
|
tok_bitwise_not,
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_question,
|
|
|
|
tok_comma,
|
|
|
|
tok_semicolon,
|
|
|
|
tok_oppar,
|
|
|
|
tok_clpar,
|
|
|
|
tok_opbracket,
|
|
|
|
tok_clbracket,
|
|
|
|
tok_opbrace,
|
|
|
|
tok_clbrace,
|
|
|
|
tok_assign,
|
|
|
|
tok_underscore,
|
|
|
|
tok_lt,
|
|
|
|
tok_gt,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_logical_not,
|
|
|
|
tok_logical_and,
|
|
|
|
tok_logical_or,
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
tok_eq,
|
|
|
|
tok_neq,
|
|
|
|
tok_leq,
|
|
|
|
tok_geq,
|
|
|
|
tok_spaceship,
|
|
|
|
tok_divR,
|
|
|
|
tok_divC,
|
|
|
|
|
|
|
|
tok_return,
|
|
|
|
tok_repeat,
|
|
|
|
tok_do,
|
|
|
|
tok_while,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_break,
|
|
|
|
tok_continue,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_try,
|
|
|
|
tok_catch,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_throw,
|
|
|
|
tok_assert,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_if,
|
|
|
|
tok_else,
|
|
|
|
|
|
|
|
tok_int,
|
|
|
|
tok_cell,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_bool,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_slice,
|
|
|
|
tok_builder,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_continuation,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_tuple,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_void,
|
|
|
|
tok_arrow,
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
tok_as,
|
2024-10-31 06:59:23 +00:00
|
|
|
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_tolk,
|
2024-10-31 06:59:23 +00:00
|
|
|
tok_semver,
|
2024-10-31 07:11:41 +00:00
|
|
|
tok_import,
|
|
|
|
tok_export,
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
tok_eof
|
2024-10-31 06:51:07 +00:00
|
|
|
};
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
// All tolk language is parsed into tokens.
|
|
|
|
// Lexer::next() returns a Token.
|
|
|
|
struct Token {
|
|
|
|
TokenType type = tok_empty;
|
|
|
|
std::string_view str_val;
|
|
|
|
|
|
|
|
Token() = default;
|
|
|
|
Token(TokenType type, std::string_view str_val): type(type), str_val(str_val) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Lexer::next() is a method to be used externally (while parsing tolk file to AST).
|
|
|
|
// It's streaming: `next()` parses a token on demand.
|
|
|
|
// For comments, see lexer.cpp, a comment above Lexer constructor.
|
2024-10-31 06:51:07 +00:00
|
|
|
class Lexer {
|
2024-10-31 06:59:23 +00:00
|
|
|
Token tokens_circularbuf[8]{};
|
|
|
|
int last_token_idx = -1;
|
|
|
|
int cur_token_idx = -1;
|
|
|
|
Token cur_token; // = tokens_circularbuf[cur_token_idx & 7]
|
2024-10-31 06:54:05 +00:00
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
const SrcFile* file;
|
|
|
|
const char *p_start, *p_end, *p_next;
|
|
|
|
SrcLocation location;
|
2024-10-31 06:54:05 +00:00
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
void update_location() {
|
|
|
|
location.char_offset = static_cast<int>(p_next - p_start);
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
struct SavedPositionForLookahead {
|
|
|
|
const char* p_next = nullptr;
|
|
|
|
int cur_token_idx = 0;
|
|
|
|
Token cur_token;
|
|
|
|
};
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
explicit Lexer(const SrcFile* file);
|
|
|
|
Lexer(const Lexer&) = delete;
|
|
|
|
Lexer &operator=(const Lexer&) = delete;
|
|
|
|
|
|
|
|
void add_token(TokenType type, std::string_view str) {
|
|
|
|
tokens_circularbuf[++last_token_idx & 7] = Token(type, str);
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
void skip_spaces() {
|
|
|
|
while (std::isspace(*p_next)) {
|
|
|
|
++p_next;
|
|
|
|
}
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
void skip_line() {
|
|
|
|
while (p_next < p_end && *p_next != '\n' && *p_next != '\r') {
|
|
|
|
++p_next;
|
|
|
|
}
|
|
|
|
while (*p_next == '\n' || *p_next == '\r') {
|
|
|
|
++p_next;
|
|
|
|
}
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
void skip_chars(int n) {
|
|
|
|
p_next += n;
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
bool is_eof() const {
|
|
|
|
return p_next >= p_end;
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
char char_at() const { return *p_next; }
|
|
|
|
char char_at(int shift) const { return *(p_next + shift); }
|
|
|
|
const char* c_str() const { return p_next; }
|
|
|
|
|
|
|
|
TokenType tok() const { return cur_token.type; }
|
|
|
|
std::string_view cur_str() const { return cur_token.str_val; }
|
|
|
|
SrcLocation cur_location() const { return location; }
|
2024-10-31 07:02:01 +00:00
|
|
|
const SrcFile* cur_file() const { return file; }
|
2024-10-31 06:59:23 +00:00
|
|
|
|
|
|
|
void next();
|
|
|
|
void next_special(TokenType parse_next_as, const char* str_expected);
|
|
|
|
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
SavedPositionForLookahead save_parsing_position() const;
|
|
|
|
void restore_position(SavedPositionForLookahead saved);
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
void check(TokenType next_tok, const char* str_expected) const {
|
|
|
|
if (cur_token.type != next_tok) {
|
2024-10-31 07:11:41 +00:00
|
|
|
unexpected(str_expected); // unlikely path, not inlined
|
2024-10-31 06:59:23 +00:00
|
|
|
}
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
2024-10-31 06:59:23 +00:00
|
|
|
void expect(TokenType next_tok, const char* str_expected) {
|
|
|
|
if (cur_token.type != next_tok) {
|
2024-10-31 07:11:41 +00:00
|
|
|
unexpected(str_expected);
|
2024-10-31 06:59:23 +00:00
|
|
|
}
|
|
|
|
next();
|
2024-10-31 06:51:07 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 07:11:41 +00:00
|
|
|
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
|
|
|
|
void unexpected(const char* str_expected) const;
|
2024-10-31 06:59:23 +00:00
|
|
|
GNU_ATTRIBUTE_NORETURN GNU_ATTRIBUTE_COLD
|
|
|
|
void error(const std::string& err_msg) const;
|
2024-10-31 06:51:07 +00:00
|
|
|
};
|
|
|
|
|
2024-10-31 06:59:23 +00:00
|
|
|
void lexer_init();
|
|
|
|
|
|
|
|
// todo #ifdef TOLK_PROFILING
|
[Tolk] Rewrite the type system from Hindley-Milner to static typing
FunC's (and Tolk's before this PR) type system is based on Hindley-Milner.
This is a common approach for functional languages, where
types are inferred from usage through unification.
As a result, type declarations are not necessary:
() f(a,b) { return a+b; } // a and b now int, since `+` (int, int)
While this approach works for now, problems arise with the introduction
of new types like bool, where `!x` must handle both int and bool.
It will also become incompatible with int32 and other strict integers.
This will clash with structure methods, struggle with proper generics,
and become entirely impractical for union types.
This PR completely rewrites the type system targeting the future.
1) type of any expression is inferred and never changed
2) this is available because dependent expressions already inferred
3) forall completely removed, generic functions introduced
(they work like template functions actually, instantiated while inferring)
4) instantiation `<...>` syntax, example: `t.tupleAt<int>(0)`
5) `as` keyword, for example `t.tupleAt(0) as int`
6) methods binding is done along with type inferring, not before
("before", as worked previously, was always a wrong approach)
2024-12-30 15:31:27 +00:00
|
|
|
void lexer_measure_performance(const AllRegisteredSrcFiles& files_to_just_parse);
|
2024-10-31 06:59:23 +00:00
|
|
|
|
2024-10-31 06:51:07 +00:00
|
|
|
} // namespace tolk
|