Skip to content

Commit fd17b7d

Browse files
authored
Merge pull request #835 from czgdp1807/list01
Implementing lists in LLVM itself and supporting `ListConstant` in LLVM backend
2 parents feab0bb + cf91d9d commit fd17b7d

File tree

8 files changed

+419
-187
lines changed

8 files changed

+419
-187
lines changed

integration_tests/test_list_01.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
from ltypes import i32
2-
3-
def test_list_i32():
4-
a: list[i32] = [1]
5-
a.append(2)
6-
a.append(3)
7-
a.append(4)
8-
a.append(5)
9-
print(a[1])
10-
assert a[1] == 2 or a[1] == 3
11-
12-
test_list_i32()
1+
from ltypes import f64, i32
2+
3+
def test_list():
4+
a: list[i32] = [0, 1, 2, 3, 4]
5+
f: list[f64] = [1.0, 2.0, 3.0, 4.0, 5.0]
6+
i: i32
7+
8+
for i in range(10):
9+
a.append(i + 5)
10+
f.append(float(i + 6))
11+
12+
13+
for i in range(15):
14+
assert (f[i] - a[i]) == 1.0
15+
16+
test_list()

src/libasr/asr_utils.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,90 @@ static inline std::string type_python_1dim_helper(const std::string & res,
617617
return res;
618618
}
619619

620+
static inline void encode_dimensions(size_t n_dims, std::string& res) {
621+
if( n_dims > 0 ) {
622+
res += "[";
623+
}
624+
for( size_t i = 0; i < n_dims; i++ ) {
625+
res += ":";
626+
if( i == n_dims - 1 ) {
627+
res += "]";
628+
} else {
629+
res += ", ";
630+
}
631+
}
632+
}
633+
634+
static inline std::string get_type_code(const ASR::ttype_t *t)
635+
{
636+
switch (t->type) {
637+
case ASR::ttypeType::Integer: {
638+
ASR::Integer_t *integer = ASR::down_cast<ASR::Integer_t>(t);
639+
std::string res = "i" + std::to_string(integer->m_kind * 8);
640+
encode_dimensions(integer->n_dims, res);
641+
return res;
642+
}
643+
case ASR::ttypeType::Real: {
644+
ASR::Real_t *real = ASR::down_cast<ASR::Real_t>(t);
645+
std::string res = "r" + std::to_string(real->m_kind * 8);
646+
encode_dimensions(real->n_dims, res);
647+
return res;
648+
}
649+
case ASR::ttypeType::Complex: {
650+
ASR::Complex_t *complx = ASR::down_cast<ASR::Complex_t>(t);
651+
std::string res = "r" + std::to_string(complx->m_kind * 8);
652+
encode_dimensions(complx->n_dims, res);
653+
return res;
654+
}
655+
case ASR::ttypeType::Logical: {
656+
return "bool";
657+
}
658+
case ASR::ttypeType::Character: {
659+
return "str";
660+
}
661+
case ASR::ttypeType::Tuple: {
662+
ASR::Tuple_t *tup = ASR::down_cast<ASR::Tuple_t>(t);
663+
std::string result = "tuple[";
664+
for (size_t i = 0; i < tup->n_type; i++) {
665+
result += get_type_code(tup->m_type[i]);
666+
if (i + 1 != tup->n_type) {
667+
result += ", ";
668+
}
669+
}
670+
result += "]";
671+
return result;
672+
}
673+
case ASR::ttypeType::Set: {
674+
ASR::Set_t *s = ASR::down_cast<ASR::Set_t>(t);
675+
return "set[" + get_type_code(s->m_type) + "]";
676+
}
677+
case ASR::ttypeType::Dict: {
678+
ASR::Dict_t *d = ASR::down_cast<ASR::Dict_t>(t);
679+
return "dict[" + get_type_code(d->m_key_type) +
680+
", " + get_type_code(d->m_value_type) + "]";
681+
}
682+
case ASR::ttypeType::List: {
683+
ASR::List_t *l = ASR::down_cast<ASR::List_t>(t);
684+
return "list[" + get_type_code(l->m_type) + "]";
685+
}
686+
case ASR::ttypeType::CPtr: {
687+
return "CPtr";
688+
}
689+
case ASR::ttypeType::Derived: {
690+
ASR::Derived_t* d = ASR::down_cast<ASR::Derived_t>(t);
691+
return symbol_name(d->m_derived_type);
692+
}
693+
case ASR::ttypeType::Pointer: {
694+
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
695+
return "Pointer[" + get_type_code(p->m_type) + "]";
696+
}
697+
default: {
698+
throw LCompilersException("Type encoding not implemented for "
699+
+ std::to_string(t->type));
700+
}
701+
}
702+
}
703+
620704
static inline std::string type_to_str_python(const ASR::ttype_t *t,
621705
bool for_error_message=true)
622706
{

0 commit comments

Comments
 (0)