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

[Tolk] v0.6 syntax: fun, import, var, types on the right, etc.

Lots of changes, actually. Most noticeable are:
- traditional //comments
- #include -> import
- a rule "import what you use"
- ~ found -> !found (for -1/0)
- null() -> null
- is_null?(v) -> v == null
- throw is a keyword
- catch with swapped arguments
- throw_if, throw_unless -> assert
- do until -> do while
- elseif -> else if
- drop ifnot, elseifnot
- drop rarely used operators

A testing framework also appears here. All tests existed earlier,
but due to significant syntax changes, their history is useless.
This commit is contained in:
tolk-vm 2024-10-31 11:11:41 +04:00
parent 5a3e3595d6
commit e2edadba92
No known key found for this signature in database
GPG key ID: 7905DD7FE0324B12
133 changed files with 8196 additions and 2605 deletions

View file

@ -32,22 +32,18 @@ namespace tolk {
class ASTStringifier final : public ASTVisitor {
constexpr static std::pair<ASTNodeType, const char*> name_pairs[] = {
{ast_empty, "ast_empty"},
{ast_parenthesized_expr, "ast_parenthesized_expr"},
{ast_tensor, "ast_tensor"},
{ast_tensor_square, "ast_tensor_square"},
{ast_identifier, "ast_identifier"},
{ast_int_const, "ast_int_const"},
{ast_string_const, "ast_string_const"},
{ast_bool_const, "ast_bool_const"},
{ast_nil_tuple, "ast_nil_tuple"},
{ast_null_keyword, "ast_null_keyword"},
{ast_function_call, "ast_function_call"},
{ast_parenthesized_expr, "ast_parenthesized_expr"},
{ast_global_var_declaration, "ast_global_var_declaration"},
{ast_global_var_declaration_list, "ast_global_var_declaration_list"},
{ast_constant_declaration, "ast_constant_declaration"},
{ast_constant_declaration_list, "ast_constant_declaration_list"},
{ast_underscore, "ast_underscore"},
{ast_type_expression, "ast_type_expression"},
{ast_variable_declaration, "ast_variable_declaration"},
{ast_tensor, "ast_tensor"},
{ast_tensor_square, "ast_tensor_square"},
{ast_dot_tilde_call, "ast_dot_tilde_call"},
{ast_unary_operator, "ast_unary_operator"},
{ast_binary_operator, "ast_binary_operator"},
@ -56,24 +52,39 @@ class ASTStringifier final : public ASTVisitor {
{ast_sequence, "ast_sequence"},
{ast_repeat_statement, "ast_repeat_statement"},
{ast_while_statement, "ast_while_statement"},
{ast_do_until_statement, "ast_do_until_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_forall_item, "ast_forall_item"},
{ast_forall_list, "ast_forall_list"},
{ast_argument, "ast_argument"},
{ast_argument_list, "ast_argument_list"},
{ast_genericsT_item, "ast_genericsT_item"},
{ast_genericsT_list, "ast_genericsT_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_pragma_no_arg, "ast_pragma_no_arg"},
{ast_pragma_version, "ast_pragma_version"},
{ast_include_statement, "ast_include_statement"},
{ast_local_var, "ast_local_var"},
{ast_local_vars_declaration, "ast_local_vars_declaration"},
{ast_tolk_required_version, "ast_tolk_required_version"},
{ast_import_statement, "ast_import_statement"},
{ast_tolk_file, "ast_tolk_file"},
};
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");
template<ASTNodeType node_type>
constexpr static const char* ast_node_type_to_string() {
static_assert(std::size(name_pairs) == ast_tolk_file + 1, "name_pairs needs to be updated");
return name_pairs[node_type].second;
}
@ -118,16 +129,6 @@ class ASTStringifier final : public ASTVisitor {
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_type_expression: {
std::ostringstream os;
os << v->as<ast_type_expression>()->declared_type;
return os.str();
}
case ast_variable_declaration: {
std::ostringstream os;
os << v->as<ast_variable_declaration>()->declared_type;
return os.str();
}
case ast_dot_tilde_call:
return static_cast<std::string>(v->as<ast_dot_tilde_call>()->method_name);
case ast_unary_operator:
@ -138,26 +139,34 @@ class ASTStringifier final : public ASTVisitor {
return "" + std::to_string(v->as<ast_sequence>()->get_items().size());
case ast_if_statement:
return v->as<ast_if_statement>()->is_ifnot ? "ifnot" : "";
case ast_argument: {
case ast_annotation:
return annotation_kinds[static_cast<int>(v->as<ast_annotation>()->kind)].second;
case ast_parameter: {
std::ostringstream os;
os << v->as<ast_argument>()->arg_type;
return static_cast<std::string>(v->as<ast_argument>()->get_identifier()->name) + ": " + os.str();
os << v->as<ast_parameter>()->param_type;
return static_cast<std::string>(v->as<ast_parameter>()->get_identifier()->name) + ": " + os.str();
}
case ast_function_declaration: {
std::string arg_names;
for (int i = 0; i < v->as<ast_function_declaration>()->get_num_args(); i++) {
if (!arg_names.empty())
arg_names += ",";
arg_names += v->as<ast_function_declaration>()->get_arg(i)->get_identifier()->name;
std::string param_names;
for (int i = 0; i < v->as<ast_function_declaration>()->get_num_params(); i++) {
if (!param_names.empty())
param_names += ",";
param_names += v->as<ast_function_declaration>()->get_param(i)->get_identifier()->name;
}
return "fun " + static_cast<std::string>(v->as<ast_function_declaration>()->get_identifier()->name) + "(" + arg_names + ")";
return "fun " + static_cast<std::string>(v->as<ast_function_declaration>()->get_identifier()->name) + "(" + param_names + ")";
}
case ast_pragma_no_arg:
return static_cast<std::string>(v->as<ast_pragma_no_arg>()->pragma_name);
case ast_pragma_version:
return static_cast<std::string>(v->as<ast_pragma_version>()->semver);
case ast_include_statement:
return static_cast<std::string>(v->as<ast_include_statement>()->get_file_leaf()->str_val);
case ast_local_var: {
std::ostringstream os;
os << 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();
}
return "_: " + os.str();
}
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_tolk_file:
return v->as<ast_tolk_file>()->file->rel_filename;
default:
@ -191,22 +200,18 @@ public:
void visit(AnyV v) override {
switch (v->type) {
case ast_empty: return handle_vertex(v->as<ast_empty>());
case ast_parenthesized_expr: return handle_vertex(v->as<ast_parenthesized_expr>());
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_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_nil_tuple: return handle_vertex(v->as<ast_nil_tuple>());
case ast_null_keyword: return handle_vertex(v->as<ast_null_keyword>());
case ast_function_call: return handle_vertex(v->as<ast_function_call>());
case ast_parenthesized_expr: return handle_vertex(v->as<ast_parenthesized_expr>());
case ast_global_var_declaration: return handle_vertex(v->as<ast_global_var_declaration>());
case ast_global_var_declaration_list: return handle_vertex(v->as<ast_global_var_declaration_list>());
case ast_constant_declaration: return handle_vertex(v->as<ast_constant_declaration>());
case ast_constant_declaration_list: return handle_vertex(v->as<ast_constant_declaration_list>());
case ast_underscore: return handle_vertex(v->as<ast_underscore>());
case ast_type_expression: return handle_vertex(v->as<ast_type_expression>());
case ast_variable_declaration: return handle_vertex(v->as<ast_variable_declaration>());
case ast_tensor: return handle_vertex(v->as<ast_tensor>());
case ast_tensor_square: return handle_vertex(v->as<ast_tensor_square>());
case ast_dot_tilde_call: return handle_vertex(v->as<ast_dot_tilde_call>());
case ast_unary_operator: return handle_vertex(v->as<ast_unary_operator>());
case ast_binary_operator: return handle_vertex(v->as<ast_binary_operator>());
@ -215,18 +220,22 @@ public:
case ast_sequence: return handle_vertex(v->as<ast_sequence>());
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_until_statement: return handle_vertex(v->as<ast_do_until_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_forall_item: return handle_vertex(v->as<ast_forall_item>());
case ast_forall_list: return handle_vertex(v->as<ast_forall_list>());
case ast_argument: return handle_vertex(v->as<ast_argument>());
case ast_argument_list: return handle_vertex(v->as<ast_argument_list>());
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_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_pragma_no_arg: return handle_vertex(v->as<ast_pragma_no_arg>());
case ast_pragma_version: return handle_vertex(v->as<ast_pragma_version>());
case ast_include_statement: return handle_vertex(v->as<ast_include_statement>());
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_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_tolk_file: return handle_vertex(v->as<ast_tolk_file>());
default:
throw UnexpectedASTNodeType(v, "ASTStringifier::visit");