Skip to content

ASR: Allow variable access from module using dot #1243

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 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions integration_tests/test_math.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc,
modf, fsum, prod)
import math
from ltypes import i32, i64, f32, f64

eps: f64
Expand Down Expand Up @@ -234,6 +235,17 @@ def test_modf():
assert abs(res[1] + 442.0) <= 1e-6


def test_issue_1242():
assert abs(math.pi - 3.14159265358979323846) < 1e-10
assert abs(math.e - 2.7182818284590452353) < 1e-10

# https://github.com/lcompilers/lpython/pull/1243#discussion_r1008810444
pi: f64 = 8.4603959020429502
assert abs(pi - 8.4603959020429502) < 1e-10
assert abs(math.pi - 3.14159265358979323846) < 1e-10
assert abs(math.pi - 3.14159265358979323846) < 1e-10


def check():
test_factorial_1()
test_comb()
Expand All @@ -257,6 +269,7 @@ def check():
test_fsum()
test_prod()
test_modf()
test_issue_1242()


check()
18 changes: 15 additions & 3 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,15 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,

ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
std::string mname, std::string cur_sym_name, std::string new_sym_name,
const Location &loc) {
const Location &loc, bool skip_current_scope_check=false) {
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
if (!t) {
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
loc);
}
if (current_scope->get_scope().find(cur_sym_name) != current_scope->get_scope().end()) {
throw SemanticError(cur_sym_name + " already defined", loc);
if (!skip_current_scope_check &&
current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
throw SemanticError(new_sym_name + " already defined", loc);
}
if (ASR::is_a<ASR::Function_t>(*t)) {
ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
Expand Down Expand Up @@ -4391,6 +4392,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_member_var,
enum_t, enum_member_variable->m_type,
ASRUtils::expr_value(enum_member_variable->m_symbolic_value));
} else if (ASR::is_a<ASR::Module_t>(*t)) {
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
std::string sym_name = value + "@" + x.m_attr;
ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name);
if (!sym) {
sym = import_from_module(al, m, current_scope, value,
x.m_attr, sym_name, x.base.base.loc, true);
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*sym));
current_scope->add_symbol(sym_name, sym);
}
tmp = ASR::make_Var_t(al, x.base.base.loc, sym);
} else {
throw SemanticError("Only Variable type is supported for now in Attribute",
x.base.base.loc);
Expand Down