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

[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)
This commit is contained in:
tolk-vm 2024-12-30 22:31:27 +07:00
parent 3540424aa1
commit 799e2d1265
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
101 changed files with 5402 additions and 2713 deletions

View file

@ -30,7 +30,7 @@ struct SrcFile {
std::string_view line_str;
};
struct ImportStatement {
struct ImportDirective {
const SrcFile* imported_file;
};
@ -39,7 +39,7 @@ struct SrcFile {
std::string abs_filename; // absolute from root
std::string text; // file contents loaded into memory, every Token::str_val points inside it
AnyV ast = nullptr; // when a file has been parsed, its ast_tolk_file is kept here
std::vector<ImportStatement> imports; // to check strictness (can't use a symbol without importing its file)
std::vector<ImportDirective> imports; // to check strictness (can't use a symbol without importing its file)
SrcFile(int file_id, std::string rel_filename, std::string abs_filename, std::string&& text)
: file_id(file_id)
@ -95,21 +95,20 @@ public:
std::ostream& operator<<(std::ostream& os, SrcLocation loc);
using AllSrcFiles = std::vector<const SrcFile*>;
class AllRegisteredSrcFiles {
std::vector<SrcFile*> all_src_files;
std::vector<const SrcFile*> all_src_files;
int last_registered_file_id = -1;
int last_parsed_file_id = -1;
public:
SrcFile *find_file(int file_id) const;
SrcFile* find_file(const std::string& abs_filename) const;
const SrcFile* find_file(int file_id) const;
const SrcFile* find_file(const std::string& abs_filename) const;
SrcFile* locate_and_register_source_file(const std::string& rel_filename, SrcLocation included_from);
const SrcFile* locate_and_register_source_file(const std::string& rel_filename, SrcLocation included_from);
SrcFile* get_next_unparsed_file();
AllSrcFiles get_all_files() const;
auto begin() const { return all_src_files.begin(); }
auto end() const { return all_src_files.end(); }
};
struct Fatal final : std::exception {