diff --git a/lang-parser b/lang-parser index 8e69e96..e2f0ef7 160000 --- a/lang-parser +++ b/lang-parser @@ -1 +1 @@ -Subproject commit 8e69e96520290fab76a98e620d58a4478e9d0861 +Subproject commit e2f0ef7b83e0bb5cc370fe365dd4fa871a44342c diff --git a/src/build_visitor.cpp b/src/build_visitor.cpp index 92987c4..6cf145f 100644 --- a/src/build_visitor.cpp +++ b/src/build_visitor.cpp @@ -1481,32 +1481,52 @@ void BuildVisitor::Visit(TypeExpression* node) { auto suffix_node1 = current_node_.NextSibling(); - // TODO: better structure // TODO: check + // TODO: refactor ?? // TODO: check if (!suffix_node1.IsNull()) { auto suffix_node2 = suffix_node1.NextSibling(); if (!suffix_node2.IsNull()) { auto suffix_node3 = suffix_node2.NextSibling(); if (!suffix_node3.IsNull()) { - current_node_ = suffix_node2; + if (suffix_node2.GetValue() == "_") { + node->array_size = 0; // dynamic array + } else { + current_node_ = suffix_node2; + NumberLiteral size_literal; + Visit(&size_literal); + node->array_size = size_literal.value; - NumberLiteral literal; - Visit(&literal); - node->array_size = literal.value; - ++excluded_child_count; + if (size_literal.value == 0) { + error_handling::HandleParsingError("Can't have zero sized arrays with constant length", + node->base.start_position, + node->base.end_position); + } + + ++excluded_child_count; // literal is named child + } node->is_optional = true; } else { if (suffix_node1.GetValue() == "`" && suffix_node2.GetValue() == "?") { - node->array_size = 0; - - node->is_optional = true; + error_handling::HandleInternalError("Array suffix should have size parameter or _", + "BuildVisitor.TypeExpression", + std::nullopt); } else if (suffix_node1.GetValue() == "`") { - current_node_ = suffix_node2; + if (suffix_node2.GetValue() == "_") { + node->array_size = 0; // dynamic array + } else { + current_node_ = suffix_node2; + NumberLiteral size_literal; + Visit(&size_literal); + node->array_size = size_literal.value; - NumberLiteral literal; - Visit(&literal); - node->array_size = literal.value; - ++excluded_child_count; + if (size_literal.value == 0) { + error_handling::HandleParsingError("Can't have zero sized arrays with constant length", + node->base.start_position, + node->base.end_position); + } + + ++excluded_child_count; // literal is named child + } } else { error_handling::HandleInternalError("Undefined suffix (2 elements)", "BuildVisitor.TypeExpression", @@ -1515,7 +1535,9 @@ void BuildVisitor::Visit(TypeExpression* node) { } } else { if (suffix_node1.GetValue() == "`") { - node->array_size = 0; + error_handling::HandleInternalError("Array suffix should have size parameter or _", + "BuildVisitor.TypeExpression", + std::nullopt); } else if (suffix_node1.GetValue() == "?") { node->is_optional = true; } else { diff --git a/tests/memory.lang b/tests/memory.lang index c0762f4..dbe541d 100644 --- a/tests/memory.lang +++ b/tests/memory.lang @@ -1,5 +1,5 @@ struct \struct-with-ref = - & @\int`0 + & @\int`_ decl test-memory : -> \unit def test-memory = { diff --git a/tests/test_code.lang b/tests/test_code.lang index cbdc85a..6c64bff 100644 --- a/tests/test_code.lang +++ b/tests/test_code.lang @@ -188,7 +188,7 @@ typeclass \enum = // -decl ( -- ) : \int -> \int -> \int`0 +decl ( -- ) : \int -> \int -> \int`_ def ( -- ) : begin end = { var current = begin return (while current < end do { @@ -252,10 +252,10 @@ def print-anything : x = \io..print: (x..show:) // return a-copy // } -struct \array 'a = & 'a`0 +struct \array 'a = & 'a`_ namespace \array { - decl of : 'a`0 -> \array['a] + decl of : 'a`_ -> \array['a] def of : x = $array['a] & x }