Skip to content

Commit

Permalink
Merge pull request #98 from czgdp1807/enum_03
Browse files Browse the repository at this point in the history
Ported ``integration_tests/enum_04.py`` from LPython and improve LC to compile it
  • Loading branch information
czgdp1807 authored Feb 29, 2024
2 parents f142447 + 9602eab commit 029d3eb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,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)
87 changes: 87 additions & 0 deletions integration_tests/enum_03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <iostream>

enum Color {
RED,
GREEN,
BLUE = 7,
YELLOW,
WHITE,
PINK = 15,
GREY
};

enum Integers {
a = -5,
b,
c,
d = 0,
e,
f = 7,
g
};

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

void test_color_enum() {
std::cout << RED << std::endl;
std::cout << GREEN << std::endl;
std::cout << BLUE << std::endl;
std::cout << YELLOW << std::endl;
std::cout << WHITE << std::endl;
std::cout << PINK << std::endl;
std::cout << GREY << std::endl;

assert( RED == 0 );
assert( GREEN == 1 );
assert( BLUE == 7 );
assert( YELLOW == 8 );
assert( WHITE == 9 );
assert( PINK == 15 );
assert( GREY == 16 );
}

void test_selected_color(enum Color selected_color) {
enum Color color;
color = selected_color;
assert( color == YELLOW);
std::cout << color << std::endl;
}

void test_integer(enum Integers integer, int value) {
assert(integer == value);
}

void test_integers() {
std::cout << a << std::endl;
test_integer(a, -5);

std::cout << b << std::endl;
test_integer(b, -4);

std::cout << c << std::endl;
test_integer(c, -3);

std::cout << d << std::endl;
test_integer(d, 0);

std::cout << e << std::endl;
test_integer(e, 1);

std::cout << f << std::endl;
test_integer(f, 7);

std::cout << g << std::endl;
test_integer(g, 8);
}

int main() {

test_color_enum();
test_selected_color(YELLOW);
test_integers();

}
13 changes: 11 additions & 2 deletions src/lc/clang_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,22 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
SymbolTable* parent_scope = current_scope;
current_scope = al.make_new<SymbolTable>(parent_scope);
Vec<char*> field_names; field_names.reserve(al, 1);
int64_t last_enum_value = 0;
for( auto enum_const_itr = x->enumerator_begin();
enum_const_itr != x->enumerator_end(); enum_const_itr++ ) {
clang::EnumConstantDecl* enum_const = *enum_const_itr;
std::string enum_const_name = enum_const->getNameAsString();
field_names.push_back(al, s2c(al, enum_const_name));
TraverseStmt(enum_const->getInitExpr());
ASR::expr_t* init_expr = ASRUtils::EXPR(tmp.get());
ASR::expr_t* init_expr = nullptr;
if( enum_const->getInitExpr() ) {
TraverseStmt(enum_const->getInitExpr());
init_expr = ASRUtils::EXPR(tmp.get());
last_enum_value = enum_const->getInitVal().getSExtValue() + 1;
} else {
init_expr = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, Lloc(x),
last_enum_value, ASRUtils::TYPE(ASR::make_Integer_t(al, Lloc(x), 4))));
last_enum_value += 1;
}
ASR::symbol_t* v = ASR::down_cast<ASR::symbol_t>(ASR::make_Variable_t(al, Lloc(x),
current_scope, s2c(al, enum_const_name), nullptr, 0, ASR::intentType::Local,
init_expr, init_expr, ASR::storage_typeType::Default, ASRUtils::expr_type(init_expr),
Expand Down

0 comments on commit 029d3eb

Please sign in to comment.