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

@ -38,7 +38,7 @@
namespace tolk {
AllSrcFiles pipeline_discover_and_parse_sources(const std::string& stdlib_filename, const std::string& entrypoint_filename) {
void pipeline_discover_and_parse_sources(const std::string& stdlib_filename, const std::string& entrypoint_filename) {
G.all_src_files.locate_and_register_source_file(stdlib_filename, {});
G.all_src_files.locate_and_register_source_file(entrypoint_filename, {});
@ -46,27 +46,25 @@ AllSrcFiles pipeline_discover_and_parse_sources(const std::string& stdlib_filena
tolk_assert(!file->ast);
file->ast = parse_src_file_to_ast(file);
// file->ast->debug_print();
// if (!file->is_stdlib_file()) file->ast->debug_print();
for (AnyV v_toplevel : file->ast->as<ast_tolk_file>()->get_toplevel_declarations()) {
if (auto v_import = v_toplevel->try_as<ast_import_statement>()) {
if (auto v_import = v_toplevel->try_as<ast_import_directive>()) {
std::string imported_str = v_import->get_file_name();
size_t cur_slash_pos = file->rel_filename.rfind('/');
std::string rel_filename = cur_slash_pos == std::string::npos || imported_str[0] == '@'
? std::move(imported_str)
: file->rel_filename.substr(0, cur_slash_pos + 1) + imported_str;
SrcFile* imported = G.all_src_files.locate_and_register_source_file(rel_filename, v_import->loc);
file->imports.push_back(SrcFile::ImportStatement{imported});
const SrcFile* imported = G.all_src_files.locate_and_register_source_file(rel_filename, v_import->loc);
file->imports.push_back(SrcFile::ImportDirective{imported});
v_import->mutate()->assign_src_file(imported);
}
}
}
// todo #ifdef TOLK_PROFILING
// lexer_measure_performance(G.all_src_files.get_all_files());
return G.all_src_files.get_all_files();
lexer_measure_performance(G.all_src_files);
}
} // namespace tolk