diff --git a/readme.md b/readme.md index e792189..7ba7044 100644 --- a/readme.md +++ b/readme.md @@ -62,7 +62,7 @@ fn types() void { // arrays // we can declare array literal using curly braces - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] // retrieve array length println(a.length()) // 3 @@ -78,7 +78,7 @@ fn types() void { println(a.length()) // 4 // we can create array of arbitrary types - []Foo arr = []Foo{new Foo(), new Foo()} + []Foo arr = [new Foo(), new Foo()] } auto PI = 3.14 // globals @@ -128,7 +128,7 @@ fn flow() void { println("string is not empty") } - auto a = []int{1, 2, 3} + auto a = [1, 2, 3] if a { println("array is not empty") } @@ -140,7 +140,7 @@ fn flow() void { println(val) } - // it's possible to specify index name. Index modification won't affect iteration + // it's possible to specify index name. Index modification won't affect iteration. // Will print 0, 1, 2 for i, val in a { println(i) diff --git a/src/ast.h b/src/ast.h index 0171dd2..4c5e3de 100644 --- a/src/ast.h +++ b/src/ast.h @@ -117,19 +117,19 @@ namespace X { public: ExprNode(NodeKind kind) : Node(kind) {} - // maybe need to rename to something like getExprType (to avoid collision with other nodes with getType method) const Type &getType() const { return type; } void setType(const Type &typ) { type = typ; } }; class ScalarNode : public ExprNode { - Type type; ScalarValue value; public: - ScalarNode(Type type, ScalarValue value) : ExprNode(NodeKind::Scalar), type(std::move(type)), value(std::move(value)) {} + ScalarNode(Type type, ScalarValue value) : ExprNode(NodeKind::Scalar), value(std::move(value)) { + ExprNode::setType(type); + } ~ScalarNode() { - if (type.is(Type::TypeID::ARRAY)) { + if (getType().is(Type::TypeID::ARRAY)) { for (auto expr: std::get(value)) { delete expr; } @@ -139,8 +139,6 @@ namespace X { void print(Pipes::PrintAst &astPrinter, int level = 0) override; llvm::Value *gen(Codegen::Codegen &codegen) override; Type infer(Pipes::TypeInferrer &typeInferrer) override; - - const Type &getType() const { return type; } const ScalarValue &getValue() const { return value; } static bool classof(const Node *node) { diff --git a/src/pipes/type_inferrer.cpp b/src/pipes/type_inferrer.cpp index 4d0964b..f3d96fd 100644 --- a/src/pipes/type_inferrer.cpp +++ b/src/pipes/type_inferrer.cpp @@ -159,17 +159,17 @@ namespace X::Pipes { // check all elements have the same type if (node->getType().is(Type::TypeID::ARRAY)) { auto &exprList = std::get(node->getValue()); - if (!exprList.empty()) { - auto firstExprType = exprList[0]->infer(*this); + auto firstExprType = exprList[0]->infer(*this); - if (firstExprType.is(Type::TypeID::VOID)) { - throw TypeInferrerException("array must not have void elements"); - } + if (firstExprType.is(Type::TypeID::VOID)) { + throw TypeInferrerException("array must not have void elements"); + } - if (!std::ranges::all_of(exprList.begin() + 1, exprList.end(), [&](auto expr) { return expr->infer(*this) == firstExprType; })) { - throw TypeInferrerException("all array elements must be the same type"); - } + if (!std::ranges::all_of(exprList.begin() + 1, exprList.end(), [&](auto expr) { return expr->infer(*this) == firstExprType; })) { + throw TypeInferrerException("all array elements must be the same type"); } + + return Type::array(std::move(firstExprType)); } return node->getType(); diff --git a/tests/arrays/append_test.cpp b/tests/arrays/append_test.cpp index f8f0d49..90e18fb 100644 --- a/tests/arrays/append_test.cpp +++ b/tests/arrays/append_test.cpp @@ -14,7 +14,7 @@ TEST_P(ArrayAppendTest, append) { INSTANTIATE_TEST_SUITE_P(Code, ArrayAppendTest, testing::Values( std::make_pair( R"code( - []int a = []int{} + []int a a[] = 1 println(a.length()) println(a[0]) @@ -22,7 +22,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ArrayAppendTest, testing::Values( "1\n1"), std::make_pair( R"code( - []int a = []int{1, 2, 3, 4, 5, 6, 7, 8} + []int a = [1, 2, 3, 4, 5, 6, 7, 8] a[] = 9 println(a.length()) println(a[8]) diff --git a/tests/arrays/array_test.cpp b/tests/arrays/array_test.cpp index 3cd6843..5fce564 100644 --- a/tests/arrays/array_test.cpp +++ b/tests/arrays/array_test.cpp @@ -5,7 +5,7 @@ class ArrayTest : public CompilerTest { TEST_F(ArrayTest, general) { auto code = R"code( - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] a[1] = 10 @@ -25,7 +25,7 @@ class Foo { } fn main() void { - []Foo a = []Foo{new Foo(123)} + []Foo a = [new Foo(123)] println(a[0].val) } @@ -37,7 +37,7 @@ TEST_F(ArrayTest, allElementsHaveSameType) { try { compiler.compile(R"code( fn main() void { - auto a = []int{1, "foo", false} + auto a = [1, "foo", false] } )code"); } catch (const std::exception &e) { diff --git a/tests/arrays/is_empty_test.cpp b/tests/arrays/is_empty_test.cpp index a983054..d7a278f 100644 --- a/tests/arrays/is_empty_test.cpp +++ b/tests/arrays/is_empty_test.cpp @@ -11,13 +11,13 @@ TEST_P(ArrayIsEmptyTest, isEmpty) { INSTANTIATE_TEST_SUITE_P(Code, ArrayIsEmptyTest, testing::Values( std::make_pair( R"code( - []int a = []int{} + []int a println(a.isEmpty()) )code", "true"), std::make_pair( R"code( - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] println(a.isEmpty()) )code", "false") diff --git a/tests/arrays/length_test.cpp b/tests/arrays/length_test.cpp index 789c0a3..3dff051 100644 --- a/tests/arrays/length_test.cpp +++ b/tests/arrays/length_test.cpp @@ -11,13 +11,13 @@ TEST_P(ArrayLengthTest, length) { INSTANTIATE_TEST_SUITE_P(Code, ArrayLengthTest, testing::Values( std::make_pair( R"code( - []int a = []int{} + []int a println(a.length()) )code", "0"), std::make_pair( R"code( - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] println(a.length()) )code", "3") diff --git a/tests/class_test.cpp b/tests/class_test.cpp index 7a624b7..48f7e20 100644 --- a/tests/class_test.cpp +++ b/tests/class_test.cpp @@ -125,7 +125,7 @@ class Greeter { } fn foo() Greeter { - return new Greeter([]string{"Ron", "Billy"}) + return new Greeter(["Ron", "Billy"]) } fn main() void { diff --git a/tests/for_test.cpp b/tests/for_test.cpp index e775087..ef59f3a 100644 --- a/tests/for_test.cpp +++ b/tests/for_test.cpp @@ -13,7 +13,7 @@ TEST_P(ForTest, foreach) { INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( std::make_pair( R"code( - auto a = []int{} + []int a for val in a { println(val) } @@ -21,7 +21,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( ""), std::make_pair( R"code( - auto a = []int{1, 2, 3} + auto a = [1, 2, 3] for val in a { println(val) } @@ -30,7 +30,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( 3)output"), std::make_pair( R"code( - auto a = []int{1, 2, 3} + auto a = [1, 2, 3] for val in a { val++ println(val) @@ -40,7 +40,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( 4)output"), std::make_pair( R"code( - auto a = []int{1, 2, 3, 4, 5} + auto a = [1, 2, 3, 4, 5] for val in a { if val == 2 { continue @@ -57,7 +57,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( 4)output"), std::make_pair( R"code( - auto a = []float{1.23, 3.14, 6.28} + auto a = [1.23, 3.14, 6.28] for i, val in a { println(i) } @@ -66,7 +66,7 @@ INSTANTIATE_TEST_SUITE_P(Code, ForTest, testing::Values( 2)output"), std::make_pair( R"code( - auto a = []float{1.23, 3.14, 6.28} + auto a = [1.23, 3.14, 6.28] for i, val in a { i++ println(val) @@ -143,7 +143,7 @@ TEST_F(ForTest, useValVarOutsideOfFor) { try { compiler.compile(R"code( fn main() void { - for val in []int{} { + for val in [1] { } println(val) diff --git a/tests/operators_test.cpp b/tests/operators_test.cpp index 9da6828..3fbbc19 100644 --- a/tests/operators_test.cpp +++ b/tests/operators_test.cpp @@ -34,7 +34,7 @@ true)output"), "no"), std::make_pair( R"code( - if []int{1, 2, 3} { + if [1, 2, 3] { println("yes") } else { println("no") diff --git a/tests/statement_test.cpp b/tests/statement_test.cpp index d376524..79cd1f7 100644 --- a/tests/statement_test.cpp +++ b/tests/statement_test.cpp @@ -29,7 +29,7 @@ false TEST_F(StatementTest, printArray) { checkCode(R"code( - println([]int{1, 2, 3}) + println([1, 2, 3]) )code", "[1, 2, 3]"); } diff --git a/tests/tour_test.cpp b/tests/tour_test.cpp index 98e164e..120975c 100644 --- a/tests/tour_test.cpp +++ b/tests/tour_test.cpp @@ -62,7 +62,7 @@ fn types() void { // arrays // we can declare array literal using curly braces - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] // retrieve array length println(a.length()) // 3 @@ -78,7 +78,7 @@ fn types() void { println(a.length()) // 4 // we can create array of arbitrary types - []Foo arr = []Foo{new Foo(), new Foo()} + []Foo arr = [new Foo(), new Foo()] } auto PI = 3.14 // globals @@ -128,7 +128,7 @@ fn flow() void { println("string is not empty") } - auto a = []int{1, 2, 3} + auto a = [1, 2, 3] if a { println("array is not empty") } @@ -140,7 +140,7 @@ fn flow() void { println(val) } - // it's possible to specify index name. Index modification won't affect iteration + // it's possible to specify index name. Index modification won't affect iteration. // Will print 0, 1, 2 for i, val in a { println(i) diff --git a/tests/type_inferrer_test.cpp b/tests/type_inferrer_test.cpp index 79281ef..572db5a 100644 --- a/tests/type_inferrer_test.cpp +++ b/tests/type_inferrer_test.cpp @@ -251,7 +251,7 @@ fn main() void { std::make_pair( R"code( fn main() void { - []int a = []int{1, 2, 3} + []int a = [1, 2, 3] a[1] = "hello" } )code",