Skip to content

Commit 12bb000

Browse files
committed
clean up code and added test
Signed-off-by: Ron Nuriel <[email protected]>
1 parent 678c0ba commit 12bb000

File tree

5 files changed

+24
-44
lines changed

5 files changed

+24
-44
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,6 @@ RUN(NAME generics_list_01 LABELS cpython llvm)
268268
RUN(NAME test_statistics LABELS cpython llvm)
269269
RUN(NAME test_str_attributes LABELS cpython llvm)
270270
RUN(NAME kwargs_01 LABELS cpython llvm)
271+
RUN(NAME test_01_goto LABELS c)
271272

272273
RUN(NAME func_inline_01 LABELS llvm wasm)

src/libasr/asr_utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,6 @@ class LabelGenerator {
17101710
if (labelname2id.find(label) == labelname2id.end() ){
17111711
int id = label_generator->get_unique_id();
17121712
labelname2id[label] = id ;
1713-
std::cout << " GOT ID = " + label_generator->get_unique_id() << std::endl;
17141713
}
17151714
return labelname2id[label];
17161715
}

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -770,26 +770,11 @@ R"(
770770
}
771771

772772
void visit_GoTo(const ASR::GoTo_t &x) {
773-
//seems like there is no data /?
774-
// this->visit_stmt(x.base);
775-
776-
// std::ostringstream sid;
777-
// sid << x.m_target_id;
778-
779-
// std::cout << "In Goto = " << x.m_target_id <<std::endl ;
780-
// std::string s << x.m_target_id;
781-
src = "label " + std::to_string(x.m_target_id) ;
773+
src = x.m_label + ":\n";
782774
}
783775

784776
void visit_GoToTarget(const ASR::GoToTarget_t &x) {
785-
//seems like there is no data /?
786-
// this->visit_stmt(x.base);
787-
// std::cout << "In Goto = " + src;
788-
std::cout << "In Goto Target = " << x.m_id <<std::endl ;
789-
790-
src = "goto " + std::to_string(x.m_id) ;
791-
792-
// src = " goto " << x.m_target_id << std::endl;
777+
src = "goto " + x.m_label + ";\n";
793778
}
794779
};
795780

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,7 +2662,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
26622662
bool current_procedure_interface = false;
26632663
bool overload = false;
26642664
bool vectorize = false, is_inline = false;
2665-
2665+
bool is_goto = false;
26662666
Vec<ASR::ttype_t*> tps;
26672667
tps.reserve(al, x.m_args.n_args);
26682668
bool is_restriction = false;
@@ -2684,7 +2684,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
26842684
} else if (name == "vectorize") {
26852685
vectorize = true;
26862686
} else if (name == "restriction") {
2687-
is_restriction = true;
2687+
is_restriction = true;
2688+
} else if (name == "with_goto") {
2689+
is_goto = true;
26882690
} else if (name == "inline") {
26892691
is_inline = true;
26902692
} else {
@@ -3726,40 +3728,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
37263728
if (AST::is_a<AST::Name_t>(*x.m_value)) {
37273729
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
37283730
if( value == "label" ) {
3729-
// Label name has to be converted into an ID
3730-
// Now each label must be mapped to a unique ID
3731-
// because ASR doesn't care about the label names
3732-
// it just cares about IDs. Now if we don't map
3733-
// label names to unique IDs then we won't be able
3734-
// to create backend code correctly.
3735-
// How to generate IDs? We have a generator for that.
37363731
std::string labelname = std::string(x.m_attr);
3737-
// std::cout << "ATTRIB NAME = " + labelname <<std::endl;
37383732
ASRUtils::LabelGenerator* label_generator = ASRUtils::LabelGenerator::get_instance();
37393733
int id = label_generator->get_id_by_label(labelname);
3740-
std::cout << " ID IS =" << id << std::endl;
3741-
// tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id); ??
3742-
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id); // #looks like i want to create goto label .
3734+
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id, labelname);
37433735
return ;
37443736
}
37453737

37463738
if (value == "goto"){
37473739
std::string labelname = std::string(x.m_attr);
3748-
// std::cout << "ATTRIB NAME = " + std::string(x.m_attr) <<std::endl;
3740+
37493741
ASRUtils::LabelGenerator* label_generator =ASRUtils::LabelGenerator::get_instance();
37503742
int id = label_generator->get_id_by_label(labelname);
3751-
// int id = label_generator->get_unique_id();
3752-
// std::string labelname = std::string(x.m_attr);
3753-
// std::cout << "INSiDE NAME = " + labelname <<std::endl;
3754-
// label_generator->add_node_with_unique_label(labelname, id);
3755-
3756-
//why empty block ? Ondrej advise GoTo_target
3757-
3758-
3759-
// set_empty_block(current_scope, x.base.base.loc);
3760-
// tmp = ASR::make_BlockCall_t(al, x.base.base.loc, id, empty_block);
3761-
3762-
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id);
3743+
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id, labelname);
37633744
return;
37643745
}
37653746

test_goto.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# from goto import with_goto #TODO: need to be supported for cpython integration_tests
2+
3+
@with_goto
4+
def func() -> i16:
5+
i:i16
6+
for i in range(10):
7+
goto .end
8+
i = 10
9+
10+
label .end
11+
assert i == 0
12+
return i
13+
14+

0 commit comments

Comments
 (0)