2024-10-31 07:03:33 +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
|
|
|
|
|
|
|
|
#ifdef TOLK_DEBUG
|
|
|
|
|
|
|
|
#include "ast.h"
|
|
|
|
#include "ast-visitor.h"
|
[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
|
|
|
#include "type-system.h"
|
2024-10-31 07:03:33 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ASTStringifier is used to print out the whole vertex tree in a human-readable format.
|
|
|
|
* To stringify any vertex, call v->debug_print(), which uses this class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace tolk {
|
|
|
|
|
|
|
|
class ASTStringifier final : public ASTVisitor {
|
|
|
|
constexpr static std::pair<ASTNodeType, const char*> name_pairs[] = {
|
[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
|
|
|
{ast_identifier, "ast_identifier"},
|
|
|
|
// expressions
|
[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
|
|
|
{ast_empty_expression, "ast_empty_expression"},
|
|
|
|
{ast_parenthesized_expression, "ast_parenthesized_expression"},
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_tensor, "ast_tensor"},
|
[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
|
|
|
{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"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_int_const, "ast_int_const"},
|
|
|
|
{ast_string_const, "ast_string_const"},
|
|
|
|
{ast_bool_const, "ast_bool_const"},
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_null_keyword, "ast_null_keyword"},
|
2024-10-31 07:18:54 +00:00
|
|
|
{ast_argument, "ast_argument"},
|
|
|
|
{ast_argument_list, "ast_argument_list"},
|
[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
|
|
|
{ast_dot_access, "ast_dot_access"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_function_call, "ast_function_call"},
|
|
|
|
{ast_underscore, "ast_underscore"},
|
[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
|
|
|
{ast_assign, "ast_assign"},
|
|
|
|
{ast_set_assign, "ast_set_assign"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_unary_operator, "ast_unary_operator"},
|
|
|
|
{ast_binary_operator, "ast_binary_operator"},
|
|
|
|
{ast_ternary_operator, "ast_ternary_operator"},
|
[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
|
|
|
{ast_cast_as_operator, "ast_cast_as_operator"},
|
|
|
|
// statements
|
|
|
|
{ast_empty_statement, "ast_empty_statement"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_sequence, "ast_sequence"},
|
[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
|
|
|
{ast_return_statement, "ast_return_statement"},
|
|
|
|
{ast_if_statement, "ast_if_statement"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_repeat_statement, "ast_repeat_statement"},
|
|
|
|
{ast_while_statement, "ast_while_statement"},
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_do_while_statement, "ast_do_while_statement"},
|
|
|
|
{ast_throw_statement, "ast_throw_statement"},
|
|
|
|
{ast_assert_statement, "ast_assert_statement"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_try_catch_statement, "ast_try_catch_statement"},
|
[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
|
|
|
{ast_asm_body, "ast_asm_body"},
|
|
|
|
// other
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_genericsT_item, "ast_genericsT_item"},
|
|
|
|
{ast_genericsT_list, "ast_genericsT_list"},
|
[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
|
|
|
{ast_instantiationT_item, "ast_instantiationT_item"},
|
|
|
|
{ast_instantiationT_list, "ast_instantiationT_list"},
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_parameter, "ast_parameter"},
|
|
|
|
{ast_parameter_list, "ast_parameter_list"},
|
|
|
|
{ast_annotation, "ast_annotation"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_function_declaration, "ast_function_declaration"},
|
[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
|
|
|
{ast_global_var_declaration, "ast_global_var_declaration"},
|
|
|
|
{ast_constant_declaration, "ast_constant_declaration"},
|
2024-10-31 07:11:41 +00:00
|
|
|
{ast_tolk_required_version, "ast_tolk_required_version"},
|
[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
|
|
|
{ast_import_directive, "ast_import_directive"},
|
2024-10-31 07:03:33 +00:00
|
|
|
{ast_tolk_file, "ast_tolk_file"},
|
|
|
|
};
|
|
|
|
|
2024-10-31 07:11:41 +00:00
|
|
|
static_assert(std::size(name_pairs) == ast_tolk_file + 1, "name_pairs needs to be updated");
|
|
|
|
|
|
|
|
constexpr static std::pair<AnnotationKind, const char*> annotation_kinds[] = {
|
|
|
|
{AnnotationKind::inline_simple, "@inline"},
|
|
|
|
{AnnotationKind::inline_ref, "@inline_ref"},
|
|
|
|
{AnnotationKind::method_id, "@method_id"},
|
|
|
|
{AnnotationKind::pure, "@pure"},
|
|
|
|
{AnnotationKind::deprecated, "@deprecated"},
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(std::size(annotation_kinds) == static_cast<size_t>(AnnotationKind::unknown), "annotation_kinds needs to be updated");
|
|
|
|
|
2024-10-31 07:03:33 +00:00
|
|
|
template<ASTNodeType node_type>
|
|
|
|
constexpr static const char* ast_node_type_to_string() {
|
|
|
|
return name_pairs[node_type].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
int depth = 0;
|
|
|
|
std::string out;
|
|
|
|
bool colored = false;
|
|
|
|
|
|
|
|
template<ASTNodeType node_type>
|
|
|
|
void handle_vertex(V<node_type> v) {
|
|
|
|
out += std::string(depth * 2, ' ');
|
|
|
|
out += ast_node_type_to_string<node_type>();
|
|
|
|
if (std::string postfix = specific_str(v); !postfix.empty()) {
|
|
|
|
out += colored ? " \x1b[34m" : " // ";
|
|
|
|
out += postfix;
|
|
|
|
out += colored ? "\x1b[0m" : "";
|
|
|
|
}
|
|
|
|
out += '\n';
|
|
|
|
depth++;
|
|
|
|
visit_children(v);
|
|
|
|
depth--;
|
|
|
|
}
|
|
|
|
|
2024-10-31 07:04:58 +00:00
|
|
|
static std::string specific_str(AnyV v) {
|
|
|
|
switch (v->type) {
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_identifier:
|
2024-10-31 07:04:58 +00:00
|
|
|
return static_cast<std::string>(v->as<ast_identifier>()->name);
|
[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
|
|
|
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;
|
|
|
|
}
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_int_const:
|
[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
|
|
|
return static_cast<std::string>(v->as<ast_int_const>()->orig_str);
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_string_const:
|
2024-10-31 07:04:58 +00:00
|
|
|
if (char modifier = v->as<ast_string_const>()->modifier) {
|
|
|
|
return "\"" + static_cast<std::string>(v->as<ast_string_const>()->str_val) + "\"" + std::string(1, modifier);
|
2024-10-31 07:03:33 +00:00
|
|
|
} else {
|
2024-10-31 07:04:58 +00:00
|
|
|
return "\"" + static_cast<std::string>(v->as<ast_string_const>()->str_val) + "\"";
|
2024-10-31 07:03:33 +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)
2024-12-30 15:31:27 +00:00
|
|
|
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 result;
|
|
|
|
}
|
2024-10-31 07:04:58 +00:00
|
|
|
case ast_function_call: {
|
[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
|
|
|
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) + ")";
|
2024-10-31 07:04:58 +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)
2024-12-30 15:31:27 +00:00
|
|
|
return inner + "()";
|
2024-10-31 07:04:58 +00:00
|
|
|
}
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_global_var_declaration:
|
2024-10-31 07:04:58 +00:00
|
|
|
return static_cast<std::string>(v->as<ast_global_var_declaration>()->get_identifier()->name);
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_constant_declaration:
|
2024-10-31 07:04:58 +00:00
|
|
|
return static_cast<std::string>(v->as<ast_constant_declaration>()->get_identifier()->name);
|
[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
|
|
|
case ast_assign:
|
|
|
|
return "=";
|
|
|
|
case ast_set_assign:
|
|
|
|
return static_cast<std::string>(v->as<ast_set_assign>()->operator_name) + "=";
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_unary_operator:
|
2024-10-31 07:04:58 +00:00
|
|
|
return static_cast<std::string>(v->as<ast_unary_operator>()->operator_name);
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_binary_operator:
|
2024-10-31 07:04:58 +00:00
|
|
|
return static_cast<std::string>(v->as<ast_binary_operator>()->operator_name);
|
[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
|
|
|
case ast_cast_as_operator:
|
|
|
|
return v->as<ast_cast_as_operator>()->cast_to_type->as_human_readable();
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_sequence:
|
2024-10-31 07:04:58 +00:00
|
|
|
return "↓" + std::to_string(v->as<ast_sequence>()->get_items().size());
|
[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
|
|
|
case ast_instantiationT_item:
|
|
|
|
return v->as<ast_instantiationT_item>()->substituted_type->as_human_readable();
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_if_statement:
|
2024-10-31 07:04:58 +00:00
|
|
|
return v->as<ast_if_statement>()->is_ifnot ? "ifnot" : "";
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_annotation:
|
|
|
|
return annotation_kinds[static_cast<int>(v->as<ast_annotation>()->kind)].second;
|
|
|
|
case ast_parameter: {
|
2024-10-31 07:03:33 +00:00
|
|
|
std::ostringstream os;
|
[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
|
|
|
os << v->as<ast_parameter>()->declared_type;
|
|
|
|
return static_cast<std::string>(v->as<ast_parameter>()->param_name) + ": " + os.str();
|
2024-10-31 07:03:33 +00:00
|
|
|
}
|
|
|
|
case ast_function_declaration: {
|
2024-10-31 07:11:41 +00:00
|
|
|
std::string param_names;
|
|
|
|
for (int i = 0; i < v->as<ast_function_declaration>()->get_num_params(); i++) {
|
|
|
|
if (!param_names.empty())
|
|
|
|
param_names += ",";
|
[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
|
|
|
param_names += v->as<ast_function_declaration>()->get_param(i)->param_name;
|
2024-10-31 07:11:41 +00:00
|
|
|
}
|
|
|
|
return "fun " + static_cast<std::string>(v->as<ast_function_declaration>()->get_identifier()->name) + "(" + param_names + ")";
|
|
|
|
}
|
[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
|
|
|
case ast_local_var_lhs: {
|
2024-10-31 07:11:41 +00:00
|
|
|
std::ostringstream os;
|
[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
|
|
|
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 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();
|
2024-10-31 07:03:33 +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)
2024-12-30 15:31:27 +00:00
|
|
|
return result + ">";
|
2024-10-31 07:03:33 +00:00
|
|
|
}
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_tolk_required_version:
|
|
|
|
return static_cast<std::string>(v->as<ast_tolk_required_version>()->semver);
|
[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
|
|
|
case ast_import_directive:
|
|
|
|
return static_cast<std::string>(v->as<ast_import_directive>()->get_file_leaf()->str_val);
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_tolk_file:
|
2024-10-31 07:04:58 +00:00
|
|
|
return v->as<ast_tolk_file>()->file->rel_filename;
|
2024-10-31 07:03:33 +00:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ASTStringifier(bool colored) : colored(colored) {
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_string_with_children(AnyV v) {
|
|
|
|
out.clear();
|
|
|
|
visit(v);
|
|
|
|
return std::move(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string to_string_without_children(AnyV v) {
|
|
|
|
std::string result = ast_node_type_to_string(v->type);
|
|
|
|
if (std::string postfix = specific_str(v); !postfix.empty()) {
|
|
|
|
result += ' ';
|
|
|
|
result += specific_str(v);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* ast_node_type_to_string(ASTNodeType node_type) {
|
|
|
|
return name_pairs[node_type].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(AnyV v) override {
|
|
|
|
switch (v->type) {
|
[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
|
|
|
case ast_identifier: return handle_vertex(v->as<ast_identifier>());
|
|
|
|
// expressions
|
[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
|
|
|
case ast_empty_expression: return handle_vertex(v->as<ast_empty_expression>());
|
|
|
|
case ast_parenthesized_expression: return handle_vertex(v->as<ast_parenthesized_expression>());
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_tensor: return handle_vertex(v->as<ast_tensor>());
|
[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
|
|
|
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>());
|
2024-10-31 07:03:33 +00:00
|
|
|
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>());
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_null_keyword: return handle_vertex(v->as<ast_null_keyword>());
|
2024-10-31 07:18:54 +00:00
|
|
|
case ast_argument: return handle_vertex(v->as<ast_argument>());
|
|
|
|
case ast_argument_list: return handle_vertex(v->as<ast_argument_list>());
|
[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
|
|
|
case ast_dot_access: return handle_vertex(v->as<ast_dot_access>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_function_call: return handle_vertex(v->as<ast_function_call>());
|
|
|
|
case ast_underscore: return handle_vertex(v->as<ast_underscore>());
|
[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
|
|
|
case ast_assign: return handle_vertex(v->as<ast_assign>());
|
|
|
|
case ast_set_assign: return handle_vertex(v->as<ast_set_assign>());
|
2024-10-31 07:03:33 +00:00
|
|
|
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>());
|
[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
|
|
|
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>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_sequence: return handle_vertex(v->as<ast_sequence>());
|
[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
|
|
|
case ast_return_statement: return handle_vertex(v->as<ast_return_statement>());
|
|
|
|
case ast_if_statement: return handle_vertex(v->as<ast_if_statement>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_repeat_statement: return handle_vertex(v->as<ast_repeat_statement>());
|
|
|
|
case ast_while_statement: return handle_vertex(v->as<ast_while_statement>());
|
2024-10-31 07:11:41 +00:00
|
|
|
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>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_try_catch_statement: return handle_vertex(v->as<ast_try_catch_statement>());
|
[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
|
|
|
case ast_asm_body: return handle_vertex(v->as<ast_asm_body>());
|
|
|
|
// other
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_genericsT_item: return handle_vertex(v->as<ast_genericsT_item>());
|
|
|
|
case ast_genericsT_list: return handle_vertex(v->as<ast_genericsT_list>());
|
[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
|
|
|
case ast_instantiationT_item: return handle_vertex(v->as<ast_instantiationT_item>());
|
|
|
|
case ast_instantiationT_list: return handle_vertex(v->as<ast_instantiationT_list>());
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_parameter: return handle_vertex(v->as<ast_parameter>());
|
|
|
|
case ast_parameter_list: return handle_vertex(v->as<ast_parameter_list>());
|
|
|
|
case ast_annotation: return handle_vertex(v->as<ast_annotation>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_function_declaration: return handle_vertex(v->as<ast_function_declaration>());
|
[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
|
|
|
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>());
|
2024-10-31 07:11:41 +00:00
|
|
|
case ast_tolk_required_version: return handle_vertex(v->as<ast_tolk_required_version>());
|
[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
|
|
|
case ast_import_directive: return handle_vertex(v->as<ast_import_directive>());
|
2024-10-31 07:03:33 +00:00
|
|
|
case ast_tolk_file: return handle_vertex(v->as<ast_tolk_file>());
|
|
|
|
default:
|
|
|
|
throw UnexpectedASTNodeType(v, "ASTStringifier::visit");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tolk
|
|
|
|
|
|
|
|
#endif // TOLK_DEBUG
|