Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Remove goto #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
ASR: Remove goto support
  • Loading branch information
ubaidsk authored and czgdp1807 committed May 23, 2023
commit ce1ac9a2ba43aaaa3c33c6211aab1c1106bd39e9
43 changes: 1 addition & 42 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3793,8 +3793,6 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
vectorize = true;
} else if (name == "restriction") {
is_restriction = true;
} else if (name == "with_goto") {
// TODO: Use goto attribute in function?
} else if (name == "inline") {
is_inline = true;
} else if (name == "static") {
Expand Down Expand Up @@ -4337,8 +4335,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {

public:
ASR::asr_t *asr;
std::map<std::string, std::tuple<int64_t, bool, Location>> goto_name2id;
int64_t gotoids;
std::vector<ASR::symbol_t*> do_loop_variables;
// Stores the name of imported functions and the modules they are imported from
std::map<std::string, std::string> imported_functions;
Expand All @@ -4348,13 +4344,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
bool main_module, std::map<int, ASR::symbol_t*> &ast_overload,
bool allow_implicit_casting_)
: CommonVisitor(al, lm, nullptr, diagnostics, main_module, ast_overload, "", {}, allow_implicit_casting_),
asr{unit}, gotoids{0}
asr{unit}
{}

// Transforms statements to a list of ASR statements
// In addition, it also inserts the following nodes if needed:
// * ImplicitDeallocate
// * GoToTarget
// The `body` Vec must already be reserved
void transform_stmts(Vec<ASR::stmt_t*> &body, size_t n_body, AST::stmt_t **m_body) {
tmp = nullptr;
Expand Down Expand Up @@ -4505,8 +4500,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}

void visit_FunctionDef(const AST::FunctionDef_t &x) {
goto_name2id.clear();
gotoids = 0;
SymbolTable *old_scope = current_scope;
ASR::symbol_t *t = current_scope->get_symbol(x.m_name);
if (ASR::is_a<ASR::Function_t>(*t)) {
Expand All @@ -4527,13 +4520,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
current_scope = old_scope;
tmp = nullptr;

for( auto itr: goto_name2id ) {
if( !std::get<1>(itr.second) ) {
throw SemanticError("Label '" + itr.first + "' is not defined in '"
+ std::string(x.m_name) + "'", std::get<2>(itr.second));
}
}
}

void visit_Import(const AST::Import_t &x) {
Expand Down Expand Up @@ -5523,33 +5509,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
void visit_Attribute(const AST::Attribute_t &x) {
if (AST::is_a<AST::Name_t>(*x.m_value)) {
std::string value = AST::down_cast<AST::Name_t>(x.m_value)->m_id;
if( value == "label" ) {
std::string labelname = x.m_attr;
if( goto_name2id.find(labelname) == goto_name2id.end() ) {
goto_name2id[labelname] = std::make_tuple(gotoids, true, x.base.base.loc);
gotoids += 1;
} else if( !std::get<1>(goto_name2id[labelname]) ) {
goto_name2id[labelname] = std::make_tuple(
std::get<0>(goto_name2id[labelname]),
true,
std::get<2>(goto_name2id[labelname])
);
}
int id = std::get<0>(goto_name2id[labelname]);
tmp = ASR::make_GoToTarget_t(al, x.base.base.loc, id, x.m_attr);
return ;
}

if (value == "goto"){
std::string labelname = std::string(x.m_attr);
if( goto_name2id.find(labelname) == goto_name2id.end() ) {
goto_name2id[labelname] = std::make_tuple(gotoids, false, x.base.base.loc);
gotoids += 1;
}
int id = std::get<0>(goto_name2id[labelname]);
tmp = ASR::make_GoTo_t(al, x.base.base.loc, id, x.m_attr);
return ;
}

ASR::symbol_t *org_sym = current_scope->resolve_symbol(value);
if (!org_sym) {
Expand Down
241 changes: 0 additions & 241 deletions src/runtime/lpython/goto.py

This file was deleted.

3 changes: 1 addition & 2 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import ctypes
import platform
from dataclasses import dataclass as py_dataclass, is_dataclass as py_is_dataclass
from goto import with_goto

# TODO: this does not seem to restrict other imports
__slots__ = ["i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", "c32", "c64", "CPtr",
"overload", "ccall", "TypeVar", "pointer", "c_p_pointer", "Pointer",
"p_c_pointer", "vectorize", "inline", "Union", "static", "with_goto",
"p_c_pointer", "vectorize", "inline", "Union", "static",
"packed", "Const", "sizeof", "ccallable", "ccallback", "Callable",
"Allocatable"]

Expand Down