Skip to content

Commit

Permalink
Merge pull request #107 from czgdp1807/vec_02
Browse files Browse the repository at this point in the history
Ported ``integration_tests/test_list_06.py`` from LPython and improve LC to compile it
  • Loading branch information
czgdp1807 authored Mar 11, 2024
2 parents 33a4fde + 60aff8a commit 06f6814
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,6 @@ RUN(NAME union_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME union_02.cpp LABELS gcc llvm NOFAST)

RUN(NAME vector_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME vector_02.cpp LABELS gcc llvm NOFAST)

RUN(NAME loop_01.cpp LABELS gcc llvm NOFAST)
96 changes: 96 additions & 0 deletions integration_tests/vector_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <vector>
#include <iostream>

#define assert(cond) if( !(cond) ) { \
exit(2); \
} \

void check_mat_and_vec(std::vector<std::vector<double>>& mat, std::vector<double>& vec) {
int32_t rows = mat.size();
int32_t cols = vec.size();
int32_t i;
int32_t j;

for( i = 0; i < rows; i++ ) {
for( j = 0; j < cols; j++ ) {
assert( mat[i][j] == double(i + j) );
}
}

for( i = 0; i < cols; i++ ) {
assert( vec[i] == 2.0 * float(i) );
}
}

void test_list_of_lists() {
std::vector<std::vector<std::vector<std::vector<double>>>> arrays = {};
std::vector<std::vector<std::vector<double>>> array = {};
std::vector<std::vector<double>> mat = {};
std::vector<double> vec = {};
int32_t rows = 10;
int32_t cols = 5;
int32_t i;
int32_t j;
int32_t k;
int32_t l;

for( i = 0; i < rows; i++ ) {
for( j = 0; j < cols; j++ ) {
vec.push_back(double(i + j));
}
mat.push_back(vec);
vec.clear();
}

for( i = 0; i < cols; i++ ) {
vec.push_back(2.0 * double(i));
}

check_mat_and_vec(mat, vec);

for( k = 0; k < rows; k++ ) {
array.push_back(mat);
for( i = 0; i < rows; i++ ) {
for( j = 0; j < cols; j++ ) {
mat[i][j] += double(1);
}
}
}

for( k = 0; k < rows; k++ ) {
for( i = 0; i < rows; i++ ) {
for( j = 0; j < cols; j++ ) {
assert( mat[i][j] - array[k][i][j] == double(rows - k) );
}
}
}

for( l = 0; l < 2 * rows; l++ ) {
arrays.push_back(array);
for( i = 0; i < rows; i++ ) {
for( j = 0; j < rows; j++ ) {
for( k = 0; k < cols; k++ ) {
array[i][j][k] += 1.0;
}
}
}
}

for( l = 0; l < 2 * rows; l++ ) {
for( i = 0; i < rows; i++ ) {
for( j = 0; j < rows; j++ ) {
for( k = 0; k < cols; k++ ) {
std::cout << (array[i][j][k] - arrays[l][i][j][k]);
std::cout << 2 * rows - l;
assert( array[i][j][k] - arrays[l][i][j][k] == double(2 * rows - l) );
}
}
}
}
}

int main() {

test_list_of_lists();

}
24 changes: 20 additions & 4 deletions src/lc/clang_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum SpecialFunc {
Iota,
Sqrt,
PushBack,
Clear,
};

std::map<std::string, SpecialFunc> special_function_map = {
Expand Down Expand Up @@ -66,6 +67,7 @@ std::map<std::string, SpecialFunc> special_function_map = {
{"operator\"\"i", SpecialFunc::Iota},
{"sqrt", SpecialFunc::Sqrt},
{"push_back", SpecialFunc::PushBack},
{"clear", SpecialFunc::Clear},
};

class OneTimeUseString {
Expand Down Expand Up @@ -1166,12 +1168,19 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
is_stmt_created = true;
} else if (sf == SpecialFunc::Size) {
if( args.size() != 0 ) {
throw std::runtime_error("xt::xtensor::size should be called with only one argument.");
throw std::runtime_error("xt::xtensor::size/std::vector::size should be called with only one argument.");
}

tmp = ASR::make_ArraySize_t(al, Lloc(x), callee, nullptr,
ASRUtils::TYPE(ASR::make_Integer_t(al, Lloc(x), 4)),
nullptr);
if( ASRUtils::is_array(ASRUtils::expr_type(callee)) ) {
tmp = ASR::make_ArraySize_t(al, Lloc(x), callee, nullptr,
ASRUtils::TYPE(ASR::make_Integer_t(al, Lloc(x), 4)),
nullptr);
} else if( ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(callee)) ) {
tmp = ASR::make_ListLen_t(al, Lloc(x), callee,
ASRUtils::TYPE(ASR::make_Integer_t(al, Lloc(x), 4)), nullptr);
} else {
throw std::runtime_error("Only xt::xtensor::size and std::vector::size are supported.");
}
} else if (sf == SpecialFunc::Fill) {
if( args.size() != 1 ) {
throw std::runtime_error("xt::xtensor::fill should be called with only one argument.");
Expand Down Expand Up @@ -1333,6 +1342,13 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit

tmp = ASR::make_ListAppend_t(al, Lloc(x), callee, args[0]);
is_stmt_created = true;
} else if (sf == SpecialFunc::Clear) {
if( args.size() > 1 ) {
throw std::runtime_error("std::vector::clear should be called with only one argument.");
}

tmp = ASR::make_ListClear_t(al, Lloc(x), callee);
is_stmt_created = true;
} else {
throw std::runtime_error("Only printf and exit special functions supported");
}
Expand Down

0 comments on commit 06f6814

Please sign in to comment.