Skip to content

Commit

Permalink
Merge pull request #99 from czgdp1807/enum_04
Browse files Browse the repository at this point in the history
Ported ``integration_tests/enum_06.py`` from LPython and improve LC to compile it
  • Loading branch information
czgdp1807 authored Feb 29, 2024
2 parents 029d3eb + 6fa64e5 commit f1443a3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,4 @@ RUN(NAME nbody_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_01.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_02.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_03.cpp LABELS gcc llvm NOFAST)
RUN(NAME enum_04.cpp LABELS gcc llvm NOFAST)
56 changes: 56 additions & 0 deletions integration_tests/enum_04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <xtensor/xfixed.hpp>

enum Color {
RED,
GREEN,
BLUE,
PINK,
WHITE,
YELLOW
};

struct Truck_t {
float horsepower;
int32_t seats;
double price;
enum Color color;
};

void init_Truck(Truck_t& truck, float horsepower_, int32_t seats_, double price_, enum Color color_) {
truck.horsepower = horsepower_;
truck.seats = seats_;
truck.price = price_;
truck.color = color_;
}

void print_Truck(const Truck_t& car) {
std::cout << car.horsepower << " " << car.seats << " " << car.price << " " << car.color << std::endl;
}

void assert_(bool condition) {
if( !condition ) {
exit(2);
}
}

void test_enum_as_struct_member() {
xt::xtensor_fixed<Truck_t, xt::xshape<3>> cars;
init_Truck(cars(0), 700.0, 4, 100000.0, RED);
init_Truck(cars(1), 800.0, 5, 200000.0, BLUE);
init_Truck(cars(2), 400.0, 4, 50000.0, WHITE);
print_Truck(cars(0));
print_Truck(cars(1));
print_Truck(cars(2));
assert_( cars(2).color == WHITE );
assert_( cars(1).color == BLUE );
assert_( cars(0).color == RED );
}

int main() {

test_enum_as_struct_member();

return 0;

}
3 changes: 3 additions & 0 deletions src/libasr/pass/pass_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,9 @@ namespace LCompilers {
if( ASR::is_a<ASR::Struct_t>(*type) ) {
ASR::Struct_t* struct_t = ASR::down_cast<ASR::Struct_t>(type);
vec.push_back(al, ASRUtils::symbol_name(struct_t->m_derived_type));
} else if( ASR::is_a<ASR::Enum_t>(*type) ) {
ASR::Enum_t* enum_t = ASR::down_cast<ASR::Enum_t>(type);
vec.push_back(al, ASRUtils::symbol_name(enum_t->m_enum_type));
}
}
xx.m_dependencies = vec.p;
Expand Down

0 comments on commit f1443a3

Please sign in to comment.