Skip to content

Commit

Permalink
simpler arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
vajexal committed May 14, 2024
1 parent 0b71702 commit c70ccf8
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 42 deletions.
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions src/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExprList>(value)) {
delete expr;
}
Expand All @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions src/pipes/type_inferrer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExprList>(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();
Expand Down
4 changes: 2 additions & 2 deletions tests/arrays/append_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ 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])
)code",
"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])
Expand Down
6 changes: 3 additions & 3 deletions tests/arrays/array_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,7 @@ class Foo {
}
fn main() void {
[]Foo a = []Foo{new Foo(123)}
[]Foo a = [new Foo(123)]
println(a[0].val)
}
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/arrays/is_empty_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/arrays/length_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/class_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Greeter {
}
fn foo() Greeter {
return new Greeter([]string{"Ron", "Billy"})
return new Greeter(["Ron", "Billy"])
}
fn main() void {
Expand Down
14 changes: 7 additions & 7 deletions tests/for_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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)
}
)code",
""),
std::make_pair(
R"code(
auto a = []int{1, 2, 3}
auto a = [1, 2, 3]
for val in a {
println(val)
}
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/operators_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/statement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}

Expand Down
8 changes: 4 additions & 4 deletions tests/tour_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/type_inferrer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c70ccf8

Please sign in to comment.