Skip to content

Commit 6b84158

Browse files
committed
WIP
1 parent 39e1a26 commit 6b84158

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
598598
// Stores the name of imported functions and the modules they are imported from
599599
std::map<std::string, std::string> imported_functions;
600600

601+
std::map<std::string, std::string> numpy2lpythontypes;
602+
601603
CommonVisitor(Allocator &al, LocationManager &lm, SymbolTable *symbol_table,
602604
diag::Diagnostics &diagnostics, bool main_module, std::string module_name,
603605
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir,
@@ -7413,14 +7415,40 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
74137415
tmp = ASR::make_GetPointer_t(al, x.base.base.loc, args[0].m_value, type, nullptr);
74147416
return ;
74157417
} else if( call_name == "array" ) {
7416-
parse_args(x, args);
7418+
ASR::ttype_t* type = nullptr;
7419+
if( x.n_keywords == 0 ) {
7420+
parse_args(x, args);
7421+
} else {
7422+
args.reserve(al, 1);
7423+
visit_expr_list(x.m_args, x.n_args, args);
7424+
if( x.n_keywords > 1 ) {
7425+
throw SemanticError("More than one keyword "
7426+
"arguments aren't recognised by array",
7427+
x.base.base.loc);
7428+
}
7429+
if( std::string(x.m_keywords[0].m_arg) != "dtype" ) {
7430+
throw SemanticError("Unrecognised keyword argument, " +
7431+
std::string(x.m_keywords[0].m_arg), x.base.base.loc);
7432+
}
7433+
visit_expr(*x.m_keywords[0].m_value);
7434+
ASR::expr_t* dtype_expr = ASRUtils::EXPR(tmp);
7435+
LCOMPILERS_ASSERT(ASR::is_a<ASR::StringConstant_t>(*dtype_expr));
7436+
std::string dtype_np = ASR::down_cast<ASR::StringConstant_t>(dtype_expr)->m_s;
7437+
LCOMPILERS_ASSERT(numpy2lpythontypes.find(dtype_np) != numpy2lpythontypes.end());
7438+
Vec<ASR::dimension_t> dims;
7439+
dims.n = 0;
7440+
type = get_type_from_var_annotation(
7441+
numpy2lpythontypes[dtype_np], x.base.base.loc, dims);
7442+
}
74177443
if( args.size() != 1 ) {
74187444
throw SemanticError("array accepts only 1 argument for now, got " +
74197445
std::to_string(args.size()) + " arguments instead.",
74207446
x.base.base.loc);
74217447
}
74227448
ASR::expr_t *arg = args[0].m_value;
7423-
ASR::ttype_t *type = ASRUtils::expr_type(arg);
7449+
if( type == nullptr ) {
7450+
type = ASRUtils::expr_type(arg);
7451+
}
74247452
if(ASR::is_a<ASR::ListConstant_t>(*arg)) {
74257453
type = ASR::down_cast<ASR::List_t>(type)->m_type;
74267454
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(arg);

0 commit comments

Comments
 (0)