Skip to content

Commit d921cf6

Browse files
ronnurielczgdp1807
authored andcommitted
clean up code and added test
Signed-off-by: Ron Nuriel <[email protected]>
1 parent 62e252f commit d921cf6

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ RUN(NAME generics_list_01 LABELS cpython llvm)
275275
RUN(NAME test_statistics LABELS cpython llvm)
276276
RUN(NAME test_str_attributes LABELS cpython llvm)
277277
RUN(NAME kwargs_01 LABELS cpython llvm)
278+
RUN(NAME test_01_goto LABELS c)
278279

279280
RUN(NAME func_inline_01 LABELS llvm wasm)
280281
RUN(NAME func_static_01 LABELS cpython llvm c wasm)

src/libasr/asr_utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,6 @@ class LabelGenerator {
17791779
if (labelname2id.find(label) == labelname2id.end() ){
17801780
int id = label_generator->get_unique_id();
17811781
labelname2id[label] = id ;
1782-
std::cout << " GOT ID = " + label_generator->get_unique_id() << std::endl;
17831782
}
17841783
return labelname2id[label];
17851784
}

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -846,26 +846,11 @@ R"(
846846
}
847847

848848
void visit_GoTo(const ASR::GoTo_t &x) {
849-
//seems like there is no data /?
850-
// this->visit_stmt(x.base);
851-
852-
// std::ostringstream sid;
853-
// sid << x.m_target_id;
854-
855-
// std::cout << "In Goto = " << x.m_target_id <<std::endl ;
856-
// std::string s << x.m_target_id;
857-
src = "label " + std::to_string(x.m_target_id) ;
849+
src = x.m_label + ":\n";
858850
}
859851

860852
void visit_GoToTarget(const ASR::GoToTarget_t &x) {
861-
//seems like there is no data /?
862-
// this->visit_stmt(x.base);
863-
// std::cout << "In Goto = " + src;
864-
std::cout << "In Goto Target = " << x.m_id <<std::endl ;
865-
866-
src = "goto " + std::to_string(x.m_id) ;
867-
868-
// src = " goto " << x.m_target_id << std::endl;
853+
src = "goto " + x.m_label + ";\n";
869854
}
870855
};
871856

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,7 +2843,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
28432843
bool current_procedure_interface = false;
28442844
bool overload = false;
28452845
bool vectorize = false, is_inline = false, is_static = false;
2846-
2846+
bool is_goto = false;
28472847
Vec<ASR::ttype_t*> tps;
28482848
tps.reserve(al, x.m_args.n_args);
28492849
bool is_restriction = false;
@@ -2866,6 +2866,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
28662866
vectorize = true;
28672867
} else if (name == "restriction") {
28682868
is_restriction = true;
2869+
} else if (name == "with_goto") {
2870+
is_goto = true;
28692871
} else if (name == "inline") {
28702872
is_inline = true;
28712873
} else if (name == "static") {
@@ -3985,40 +3987,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
39853987
if (AST::is_a<AST::Name_t>(*x.m_value)) {
39863988
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
39873989
if( value == "label" ) {
3988-
// Label name has to be converted into an ID
3989-
// Now each label must be mapped to a unique ID
3990-
// because ASR doesn't care about the label names
3991-
// it just cares about IDs. Now if we don't map
3992-
// label names to unique IDs then we won't be able
3993-
// to create backend code correctly.
3994-
// How to generate IDs? We have a generator for that.
39953990
std::string labelname = std::string(x.m_attr);
3996-
// std::cout << "ATTRIB NAME = " + labelname <<std::endl;
39973991
ASRUtils::LabelGenerator* label_generator = ASRUtils::LabelGenerator::get_instance();
39983992
int id = label_generator->get_id_by_label(labelname);
3999-
std::cout << " ID IS =" << id << std::endl;
4000-
// tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id); ??
4001-
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id); // #looks like i want to create goto label .
3993+
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id, labelname);
40023994
return ;
40033995
}
40043996

40053997
if (value == "goto"){
40063998
std::string labelname = std::string(x.m_attr);
4007-
// std::cout << "ATTRIB NAME = " + std::string(x.m_attr) <<std::endl;
3999+
40084000
ASRUtils::LabelGenerator* label_generator =ASRUtils::LabelGenerator::get_instance();
40094001
int id = label_generator->get_id_by_label(labelname);
4010-
// int id = label_generator->get_unique_id();
4011-
// std::string labelname = std::string(x.m_attr);
4012-
// std::cout << "INSiDE NAME = " + labelname <<std::endl;
4013-
// label_generator->add_node_with_unique_label(labelname, id);
4014-
4015-
//why empty block ? Ondrej advise GoTo_target
4016-
4017-
4018-
// set_empty_block(current_scope, x.base.base.loc);
4019-
// tmp = ASR::make_BlockCall_t(al, x.base.base.loc, id, empty_block);
4020-
4021-
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id);
4002+
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id, labelname);
40224003
return;
40234004
}
40244005

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)