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:
parent
3540424aa1
commit
799e2d1265
101 changed files with 5402 additions and 2713 deletions
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include "ast.h"
|
||||
#include "ast-visitor.h"
|
||||
#include "type-system.h"
|
||||
#include <sstream>
|
||||
|
||||
/*
|
||||
|
@ -31,47 +32,55 @@ namespace tolk {
|
|||
|
||||
class ASTStringifier final : public ASTVisitor {
|
||||
constexpr static std::pair<ASTNodeType, const char*> name_pairs[] = {
|
||||
{ast_empty_statement, "ast_empty_statement"},
|
||||
{ast_identifier, "ast_identifier"},
|
||||
// expressions
|
||||
{ast_empty_expression, "ast_empty_expression"},
|
||||
{ast_parenthesized_expression, "ast_parenthesized_expression"},
|
||||
{ast_tensor, "ast_tensor"},
|
||||
{ast_tensor_square, "ast_tensor_square"},
|
||||
{ast_identifier, "ast_identifier"},
|
||||
{ast_typed_tuple, "ast_typed_tuple"},
|
||||
{ast_reference, "ast_reference"},
|
||||
{ast_local_var_lhs, "ast_local_var_lhs"},
|
||||
{ast_local_vars_declaration, "ast_local_vars_declaration"},
|
||||
{ast_int_const, "ast_int_const"},
|
||||
{ast_string_const, "ast_string_const"},
|
||||
{ast_bool_const, "ast_bool_const"},
|
||||
{ast_null_keyword, "ast_null_keyword"},
|
||||
{ast_self_keyword, "ast_self_keyword"},
|
||||
{ast_argument, "ast_argument"},
|
||||
{ast_argument_list, "ast_argument_list"},
|
||||
{ast_dot_access, "ast_dot_access"},
|
||||
{ast_function_call, "ast_function_call"},
|
||||
{ast_dot_method_call, "ast_dot_method_call"},
|
||||
{ast_global_var_declaration, "ast_global_var_declaration"},
|
||||
{ast_constant_declaration, "ast_constant_declaration"},
|
||||
{ast_underscore, "ast_underscore"},
|
||||
{ast_assign, "ast_assign"},
|
||||
{ast_set_assign, "ast_set_assign"},
|
||||
{ast_unary_operator, "ast_unary_operator"},
|
||||
{ast_binary_operator, "ast_binary_operator"},
|
||||
{ast_ternary_operator, "ast_ternary_operator"},
|
||||
{ast_return_statement, "ast_return_statement"},
|
||||
{ast_cast_as_operator, "ast_cast_as_operator"},
|
||||
// statements
|
||||
{ast_empty_statement, "ast_empty_statement"},
|
||||
{ast_sequence, "ast_sequence"},
|
||||
{ast_return_statement, "ast_return_statement"},
|
||||
{ast_if_statement, "ast_if_statement"},
|
||||
{ast_repeat_statement, "ast_repeat_statement"},
|
||||
{ast_while_statement, "ast_while_statement"},
|
||||
{ast_do_while_statement, "ast_do_while_statement"},
|
||||
{ast_throw_statement, "ast_throw_statement"},
|
||||
{ast_assert_statement, "ast_assert_statement"},
|
||||
{ast_try_catch_statement, "ast_try_catch_statement"},
|
||||
{ast_if_statement, "ast_if_statement"},
|
||||
{ast_asm_body, "ast_asm_body"},
|
||||
// other
|
||||
{ast_genericsT_item, "ast_genericsT_item"},
|
||||
{ast_genericsT_list, "ast_genericsT_list"},
|
||||
{ast_instantiationT_item, "ast_instantiationT_item"},
|
||||
{ast_instantiationT_list, "ast_instantiationT_list"},
|
||||
{ast_parameter, "ast_parameter"},
|
||||
{ast_parameter_list, "ast_parameter_list"},
|
||||
{ast_asm_body, "ast_asm_body"},
|
||||
{ast_annotation, "ast_annotation"},
|
||||
{ast_function_declaration, "ast_function_declaration"},
|
||||
{ast_local_var, "ast_local_var"},
|
||||
{ast_local_vars_declaration, "ast_local_vars_declaration"},
|
||||
{ast_global_var_declaration, "ast_global_var_declaration"},
|
||||
{ast_constant_declaration, "ast_constant_declaration"},
|
||||
{ast_tolk_required_version, "ast_tolk_required_version"},
|
||||
{ast_import_statement, "ast_import_statement"},
|
||||
{ast_import_directive, "ast_import_directive"},
|
||||
{ast_tolk_file, "ast_tolk_file"},
|
||||
};
|
||||
|
||||
|
@ -115,6 +124,13 @@ class ASTStringifier final : public ASTVisitor {
|
|||
switch (v->type) {
|
||||
case ast_identifier:
|
||||
return static_cast<std::string>(v->as<ast_identifier>()->name);
|
||||
case ast_reference: {
|
||||
std::string result(v->as<ast_reference>()->get_name());
|
||||
if (v->as<ast_reference>()->has_instantiationTs()) {
|
||||
result += specific_str(v->as<ast_reference>()->get_instantiationTs());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
case ast_int_const:
|
||||
return static_cast<std::string>(v->as<ast_int_const>()->orig_str);
|
||||
case ast_string_const:
|
||||
|
@ -123,24 +139,40 @@ class ASTStringifier final : public ASTVisitor {
|
|||
} else {
|
||||
return "\"" + static_cast<std::string>(v->as<ast_string_const>()->str_val) + "\"";
|
||||
}
|
||||
case ast_function_call: {
|
||||
if (auto v_lhs = v->as<ast_function_call>()->get_called_f()->try_as<ast_identifier>()) {
|
||||
return static_cast<std::string>(v_lhs->name) + "()";
|
||||
case ast_bool_const:
|
||||
return v->as<ast_bool_const>()->bool_val ? "true" : "false";
|
||||
case ast_dot_access: {
|
||||
std::string result = "." + static_cast<std::string>(v->as<ast_dot_access>()->get_field_name());
|
||||
if (v->as<ast_dot_access>()->has_instantiationTs()) {
|
||||
result += specific_str(v->as<ast_dot_access>()->get_instantiationTs());
|
||||
}
|
||||
return {};
|
||||
return result;
|
||||
}
|
||||
case ast_function_call: {
|
||||
std::string inner = specific_str(v->as<ast_function_call>()->get_callee());
|
||||
if (int n_args = v->as<ast_function_call>()->get_num_args()) {
|
||||
return inner + "(..." + std::to_string(n_args) + ")";
|
||||
}
|
||||
return inner + "()";
|
||||
}
|
||||
case ast_dot_method_call:
|
||||
return static_cast<std::string>(v->as<ast_dot_method_call>()->method_name);
|
||||
case ast_global_var_declaration:
|
||||
return static_cast<std::string>(v->as<ast_global_var_declaration>()->get_identifier()->name);
|
||||
case ast_constant_declaration:
|
||||
return static_cast<std::string>(v->as<ast_constant_declaration>()->get_identifier()->name);
|
||||
case ast_assign:
|
||||
return "=";
|
||||
case ast_set_assign:
|
||||
return static_cast<std::string>(v->as<ast_set_assign>()->operator_name) + "=";
|
||||
case ast_unary_operator:
|
||||
return static_cast<std::string>(v->as<ast_unary_operator>()->operator_name);
|
||||
case ast_binary_operator:
|
||||
return static_cast<std::string>(v->as<ast_binary_operator>()->operator_name);
|
||||
case ast_cast_as_operator:
|
||||
return v->as<ast_cast_as_operator>()->cast_to_type->as_human_readable();
|
||||
case ast_sequence:
|
||||
return "↓" + std::to_string(v->as<ast_sequence>()->get_items().size());
|
||||
case ast_instantiationT_item:
|
||||
return v->as<ast_instantiationT_item>()->substituted_type->as_human_readable();
|
||||
case ast_if_statement:
|
||||
return v->as<ast_if_statement>()->is_ifnot ? "ifnot" : "";
|
||||
case ast_annotation:
|
||||
|
@ -159,18 +191,27 @@ class ASTStringifier final : public ASTVisitor {
|
|||
}
|
||||
return "fun " + static_cast<std::string>(v->as<ast_function_declaration>()->get_identifier()->name) + "(" + param_names + ")";
|
||||
}
|
||||
case ast_local_var: {
|
||||
case ast_local_var_lhs: {
|
||||
std::ostringstream os;
|
||||
os << (v->as<ast_local_var>()->inferred_type ? v->as<ast_local_var>()->inferred_type : v->as<ast_local_var>()->declared_type);
|
||||
if (auto v_ident = v->as<ast_local_var>()->get_identifier()->try_as<ast_identifier>()) {
|
||||
return static_cast<std::string>(v_ident->name) + ":" + os.str();
|
||||
os << (v->as<ast_local_var_lhs>()->inferred_type ? v->as<ast_local_var_lhs>()->inferred_type : v->as<ast_local_var_lhs>()->declared_type);
|
||||
if (v->as<ast_local_var_lhs>()->get_name().empty()) {
|
||||
return "_: " + os.str();
|
||||
}
|
||||
return "_: " + os.str();
|
||||
return static_cast<std::string>(v->as<ast_local_var_lhs>()->get_name()) + ":" + os.str();
|
||||
}
|
||||
case ast_instantiationT_list: {
|
||||
std::string result = "<";
|
||||
for (AnyV item : v->as<ast_instantiationT_list>()->get_items()) {
|
||||
if (result.size() > 1)
|
||||
result += ",";
|
||||
result += item->as<ast_instantiationT_item>()->substituted_type->as_human_readable();
|
||||
}
|
||||
return result + ">";
|
||||
}
|
||||
case ast_tolk_required_version:
|
||||
return static_cast<std::string>(v->as<ast_tolk_required_version>()->semver);
|
||||
case ast_import_statement:
|
||||
return static_cast<std::string>(v->as<ast_import_statement>()->get_file_leaf()->str_val);
|
||||
case ast_import_directive:
|
||||
return static_cast<std::string>(v->as<ast_import_directive>()->get_file_leaf()->str_val);
|
||||
case ast_tolk_file:
|
||||
return v->as<ast_tolk_file>()->file->rel_filename;
|
||||
default:
|
||||
|
@ -203,47 +244,55 @@ public:
|
|||
|
||||
void visit(AnyV v) override {
|
||||
switch (v->type) {
|
||||
case ast_empty_statement: return handle_vertex(v->as<ast_empty_statement>());
|
||||
case ast_identifier: return handle_vertex(v->as<ast_identifier>());
|
||||
// expressions
|
||||
case ast_empty_expression: return handle_vertex(v->as<ast_empty_expression>());
|
||||
case ast_parenthesized_expression: return handle_vertex(v->as<ast_parenthesized_expression>());
|
||||
case ast_tensor: return handle_vertex(v->as<ast_tensor>());
|
||||
case ast_tensor_square: return handle_vertex(v->as<ast_tensor_square>());
|
||||
case ast_identifier: return handle_vertex(v->as<ast_identifier>());
|
||||
case ast_typed_tuple: return handle_vertex(v->as<ast_typed_tuple>());
|
||||
case ast_reference: return handle_vertex(v->as<ast_reference>());
|
||||
case ast_local_var_lhs: return handle_vertex(v->as<ast_local_var_lhs>());
|
||||
case ast_local_vars_declaration: return handle_vertex(v->as<ast_local_vars_declaration>());
|
||||
case ast_int_const: return handle_vertex(v->as<ast_int_const>());
|
||||
case ast_string_const: return handle_vertex(v->as<ast_string_const>());
|
||||
case ast_bool_const: return handle_vertex(v->as<ast_bool_const>());
|
||||
case ast_null_keyword: return handle_vertex(v->as<ast_null_keyword>());
|
||||
case ast_self_keyword: return handle_vertex(v->as<ast_self_keyword>());
|
||||
case ast_argument: return handle_vertex(v->as<ast_argument>());
|
||||
case ast_argument_list: return handle_vertex(v->as<ast_argument_list>());
|
||||
case ast_dot_access: return handle_vertex(v->as<ast_dot_access>());
|
||||
case ast_function_call: return handle_vertex(v->as<ast_function_call>());
|
||||
case ast_dot_method_call: return handle_vertex(v->as<ast_dot_method_call>());
|
||||
case ast_global_var_declaration: return handle_vertex(v->as<ast_global_var_declaration>());
|
||||
case ast_constant_declaration: return handle_vertex(v->as<ast_constant_declaration>());
|
||||
case ast_underscore: return handle_vertex(v->as<ast_underscore>());
|
||||
case ast_assign: return handle_vertex(v->as<ast_assign>());
|
||||
case ast_set_assign: return handle_vertex(v->as<ast_set_assign>());
|
||||
case ast_unary_operator: return handle_vertex(v->as<ast_unary_operator>());
|
||||
case ast_binary_operator: return handle_vertex(v->as<ast_binary_operator>());
|
||||
case ast_ternary_operator: return handle_vertex(v->as<ast_ternary_operator>());
|
||||
case ast_return_statement: return handle_vertex(v->as<ast_return_statement>());
|
||||
case ast_cast_as_operator: return handle_vertex(v->as<ast_cast_as_operator>());
|
||||
// statements
|
||||
case ast_empty_statement: return handle_vertex(v->as<ast_empty_statement>());
|
||||
case ast_sequence: return handle_vertex(v->as<ast_sequence>());
|
||||
case ast_return_statement: return handle_vertex(v->as<ast_return_statement>());
|
||||
case ast_if_statement: return handle_vertex(v->as<ast_if_statement>());
|
||||
case ast_repeat_statement: return handle_vertex(v->as<ast_repeat_statement>());
|
||||
case ast_while_statement: return handle_vertex(v->as<ast_while_statement>());
|
||||
case ast_do_while_statement: return handle_vertex(v->as<ast_do_while_statement>());
|
||||
case ast_throw_statement: return handle_vertex(v->as<ast_throw_statement>());
|
||||
case ast_assert_statement: return handle_vertex(v->as<ast_assert_statement>());
|
||||
case ast_try_catch_statement: return handle_vertex(v->as<ast_try_catch_statement>());
|
||||
case ast_if_statement: return handle_vertex(v->as<ast_if_statement>());
|
||||
case ast_asm_body: return handle_vertex(v->as<ast_asm_body>());
|
||||
// other
|
||||
case ast_genericsT_item: return handle_vertex(v->as<ast_genericsT_item>());
|
||||
case ast_genericsT_list: return handle_vertex(v->as<ast_genericsT_list>());
|
||||
case ast_instantiationT_item: return handle_vertex(v->as<ast_instantiationT_item>());
|
||||
case ast_instantiationT_list: return handle_vertex(v->as<ast_instantiationT_list>());
|
||||
case ast_parameter: return handle_vertex(v->as<ast_parameter>());
|
||||
case ast_parameter_list: return handle_vertex(v->as<ast_parameter_list>());
|
||||
case ast_asm_body: return handle_vertex(v->as<ast_asm_body>());
|
||||
case ast_annotation: return handle_vertex(v->as<ast_annotation>());
|
||||
case ast_function_declaration: return handle_vertex(v->as<ast_function_declaration>());
|
||||
case ast_local_var: return handle_vertex(v->as<ast_local_var>());
|
||||
case ast_local_vars_declaration: return handle_vertex(v->as<ast_local_vars_declaration>());
|
||||
case ast_global_var_declaration: return handle_vertex(v->as<ast_global_var_declaration>());
|
||||
case ast_constant_declaration: return handle_vertex(v->as<ast_constant_declaration>());
|
||||
case ast_tolk_required_version: return handle_vertex(v->as<ast_tolk_required_version>());
|
||||
case ast_import_statement: return handle_vertex(v->as<ast_import_statement>());
|
||||
case ast_import_directive: return handle_vertex(v->as<ast_import_directive>());
|
||||
case ast_tolk_file: return handle_vertex(v->as<ast_tolk_file>());
|
||||
default:
|
||||
throw UnexpectedASTNodeType(v, "ASTStringifier::visit");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue