Skip to content

Commit b2b74ca

Browse files
authored
Merge pull request #1243 from Smit-create/i-1242
ASR: Allow variable access from module using dot
2 parents ec3b0d2 + e4bd9b5 commit b2b74ca

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

integration_tests/test_math.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
22
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc,
33
modf, fsum, prod)
4+
import math
45
from ltypes import i32, i64, f32, f64
56

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

236237

238+
def test_issue_1242():
239+
assert abs(math.pi - 3.14159265358979323846) < 1e-10
240+
assert abs(math.e - 2.7182818284590452353) < 1e-10
241+
242+
# https://github.com/lcompilers/lpython/pull/1243#discussion_r1008810444
243+
pi: f64 = 8.4603959020429502
244+
assert abs(pi - 8.4603959020429502) < 1e-10
245+
assert abs(math.pi - 3.14159265358979323846) < 1e-10
246+
assert abs(math.pi - 3.14159265358979323846) < 1e-10
247+
248+
237249
def check():
238250
test_factorial_1()
239251
test_comb()
@@ -257,6 +269,7 @@ def check():
257269
test_fsum()
258270
test_prod()
259271
test_modf()
272+
test_issue_1242()
260273

261274

262275
check()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,15 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
395395

396396
ASR::symbol_t* import_from_module(Allocator &al, ASR::Module_t *m, SymbolTable *current_scope,
397397
std::string mname, std::string cur_sym_name, std::string new_sym_name,
398-
const Location &loc) {
398+
const Location &loc, bool skip_current_scope_check=false) {
399399
ASR::symbol_t *t = m->m_symtab->resolve_symbol(cur_sym_name);
400400
if (!t) {
401401
throw SemanticError("The symbol '" + cur_sym_name + "' not found in the module '" + mname + "'",
402402
loc);
403403
}
404-
if (current_scope->get_scope().find(cur_sym_name) != current_scope->get_scope().end()) {
405-
throw SemanticError(cur_sym_name + " already defined", loc);
404+
if (!skip_current_scope_check &&
405+
current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
406+
throw SemanticError(new_sym_name + " already defined", loc);
406407
}
407408
if (ASR::is_a<ASR::Function_t>(*t)) {
408409
ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
@@ -4391,6 +4392,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
43914392
tmp = ASR::make_EnumValue_t(al, x.base.base.loc, enum_member_var,
43924393
enum_t, enum_member_variable->m_type,
43934394
ASRUtils::expr_value(enum_member_variable->m_symbolic_value));
4395+
} else if (ASR::is_a<ASR::Module_t>(*t)) {
4396+
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
4397+
std::string sym_name = value + "@" + x.m_attr;
4398+
ASR::symbol_t *sym = current_scope->resolve_symbol(sym_name);
4399+
if (!sym) {
4400+
sym = import_from_module(al, m, current_scope, value,
4401+
x.m_attr, sym_name, x.base.base.loc, true);
4402+
LFORTRAN_ASSERT(ASR::is_a<ASR::ExternalSymbol_t>(*sym));
4403+
current_scope->add_symbol(sym_name, sym);
4404+
}
4405+
tmp = ASR::make_Var_t(al, x.base.base.loc, sym);
43944406
} else {
43954407
throw SemanticError("Only Variable type is supported for now in Attribute",
43964408
x.base.base.loc);

0 commit comments

Comments
 (0)