Skip to content

Commit 8b0357d

Browse files
committed
C: Fix goto
1 parent 2367d77 commit 8b0357d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/libasr/codegen/asr_to_c.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,14 +1280,6 @@ R"(
12801280
src = "strlen(" + src + ")";
12811281
}
12821282

1283-
void visit_GoTo(const ASR::GoTo_t &x) {
1284-
std::string indent(indentation_level*indentation_spaces, ' ');
1285-
src = indent + "goto " + std::string(x.m_name) + ";\n";
1286-
}
1287-
1288-
void visit_GoToTarget(const ASR::GoToTarget_t &x) {
1289-
src = std::string(x.m_name) + ":\n";
1290-
}
12911283
};
12921284

12931285
Result<std::string> asr_to_c(Allocator & /*al*/, ASR::TranslationUnit_t &asr,

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
102102
const ASR::Function_t *current_function = nullptr;
103103
std::map<uint64_t, SymbolInfo> sym_info;
104104
std::map<uint64_t, std::string> const_var_names;
105+
std::map<int32_t, std::string> gotoid2name;
105106

106107
// Output configuration:
107108
// Use std::string or char*
@@ -331,6 +332,15 @@ R"(#include <stdio.h>
331332
std::string indent(indentation_level*indentation_spaces, ' ');
332333
std::string open_paranthesis = indent + "{\n";
333334
std::string close_paranthesis = indent + "}\n";
335+
if (x.m_label != -1) {
336+
std::string b_name;
337+
if (gotoid2name.find(x.m_label) != gotoid2name.end()) {
338+
b_name = gotoid2name[x.m_label];
339+
} else {
340+
b_name = "__" +std::to_string(x.m_label);
341+
}
342+
open_paranthesis = indent + b_name + ": {\n";
343+
}
334344
indent += std::string(indentation_spaces, ' ');
335345
indentation_level += 1;
336346
SymbolTable* current_scope_copy = current_scope;
@@ -1911,6 +1921,12 @@ R"(#include <stdio.h>
19111921
}
19121922
}
19131923

1924+
void visit_GoTo(const ASR::GoTo_t &x) {
1925+
std::string indent(indentation_level*indentation_spaces, ' ');
1926+
src = indent + "goto " + std::string(x.m_name) + ";\n";
1927+
gotoid2name[x.m_target_id] = std::string(x.m_name);
1928+
}
1929+
19141930
void visit_GoToTarget(const ASR::GoToTarget_t & /* x */) {
19151931
// Ignore for now
19161932
src = "";

0 commit comments

Comments
 (0)