dynamic array type expression changed: 0 -> _

This commit is contained in:
ProgramSnail 2023-07-08 13:23:52 +03:00
parent 3815f8259b
commit d6e6d5cc2c
4 changed files with 42 additions and 20 deletions

@ -1 +1 @@
Subproject commit 8e69e96520290fab76a98e620d58a4478e9d0861 Subproject commit e2f0ef7b83e0bb5cc370fe365dd4fa871a44342c

View file

@ -1481,32 +1481,52 @@ void BuildVisitor::Visit(TypeExpression* node) {
auto suffix_node1 = current_node_.NextSibling(); auto suffix_node1 = current_node_.NextSibling();
// TODO: better structure // TODO: check // TODO: refactor ?? // TODO: check
if (!suffix_node1.IsNull()) { if (!suffix_node1.IsNull()) {
auto suffix_node2 = suffix_node1.NextSibling(); auto suffix_node2 = suffix_node1.NextSibling();
if (!suffix_node2.IsNull()) { if (!suffix_node2.IsNull()) {
auto suffix_node3 = suffix_node2.NextSibling(); auto suffix_node3 = suffix_node2.NextSibling();
if (!suffix_node3.IsNull()) { if (!suffix_node3.IsNull()) {
if (suffix_node2.GetValue() == "_") {
node->array_size = 0; // dynamic array
} else {
current_node_ = suffix_node2; current_node_ = suffix_node2;
NumberLiteral size_literal;
Visit(&size_literal);
node->array_size = size_literal.value;
NumberLiteral literal; if (size_literal.value == 0) {
Visit(&literal); error_handling::HandleParsingError("Can't have zero sized arrays with constant length",
node->array_size = literal.value; node->base.start_position,
++excluded_child_count; node->base.end_position);
}
++excluded_child_count; // literal is named child
}
node->is_optional = true; node->is_optional = true;
} else { } else {
if (suffix_node1.GetValue() == "`" && suffix_node2.GetValue() == "?") { if (suffix_node1.GetValue() == "`" && suffix_node2.GetValue() == "?") {
node->array_size = 0; error_handling::HandleInternalError("Array suffix should have size parameter or _",
"BuildVisitor.TypeExpression",
node->is_optional = true; std::nullopt);
} else if (suffix_node1.GetValue() == "`") { } else if (suffix_node1.GetValue() == "`") {
if (suffix_node2.GetValue() == "_") {
node->array_size = 0; // dynamic array
} else {
current_node_ = suffix_node2; current_node_ = suffix_node2;
NumberLiteral size_literal;
Visit(&size_literal);
node->array_size = size_literal.value;
NumberLiteral literal; if (size_literal.value == 0) {
Visit(&literal); error_handling::HandleParsingError("Can't have zero sized arrays with constant length",
node->array_size = literal.value; node->base.start_position,
++excluded_child_count; node->base.end_position);
}
++excluded_child_count; // literal is named child
}
} else { } else {
error_handling::HandleInternalError("Undefined suffix (2 elements)", error_handling::HandleInternalError("Undefined suffix (2 elements)",
"BuildVisitor.TypeExpression", "BuildVisitor.TypeExpression",
@ -1515,7 +1535,9 @@ void BuildVisitor::Visit(TypeExpression* node) {
} }
} else { } else {
if (suffix_node1.GetValue() == "`") { 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() == "?") { } else if (suffix_node1.GetValue() == "?") {
node->is_optional = true; node->is_optional = true;
} else { } else {

View file

@ -1,5 +1,5 @@
struct \struct-with-ref = struct \struct-with-ref =
& @\int`0 & @\int`_
decl test-memory : -> \unit decl test-memory : -> \unit
def test-memory = { def test-memory = {

View file

@ -188,7 +188,7 @@ typeclass \enum =
// //
decl ( -- ) : \int -> \int -> \int`0 decl ( -- ) : \int -> \int -> \int`_
def ( -- ) : begin end = { def ( -- ) : begin end = {
var current = begin var current = begin
return (while current < end do { return (while current < end do {
@ -252,10 +252,10 @@ def print-anything : x = \io..print: (x..show:)
// return a-copy // return a-copy
// } // }
struct \array 'a = & 'a`0 struct \array 'a = & 'a`_
namespace \array { namespace \array {
decl of : 'a`0 -> \array['a] decl of : 'a`_ -> \array['a]
def of : x = $array['a] & x def of : x = $array['a] & x
} }