Skip to content

Commit d1535f4

Browse files
committed
ASR: Support array.size attribute
1 parent 9b8fa1e commit d1535f4

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,7 +5137,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
51375137

51385138
void visit_AttributeUtil(ASR::ttype_t* type, char* attr_char,
51395139
ASR::symbol_t *t, const Location& loc) {
5140-
if (ASRUtils::is_complex(*type)) {
5140+
if (ASRUtils::is_array(type)) {
5141+
std::string attr = attr_char;
5142+
ASR::expr_t *se = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, loc, t));
5143+
Vec<ASR::expr_t*> args;
5144+
args.reserve(al, 0);
5145+
handle_attribute(se, attr, loc, args);
5146+
} else if (ASRUtils::is_complex(*type)) {
51415147
std::string attr = attr_char;
51425148
if (attr == "imag") {
51435149
ASR::expr_t *val = ASR::down_cast<ASR::expr_t>(ASR::make_Var_t(al, loc, t));
@@ -6705,7 +6711,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
67056711
ASRUtils::IntrinsicFunctionRegistry::get_create_function(call_name);
67066712
Vec<ASR::expr_t*> args_; args_.reserve(al, x.n_args);
67076713
visit_expr_list(x.m_args, x.n_args, args_);
6708-
if (ASRUtils::is_array(ASRUtils::expr_type(args_[0])) &&
6714+
if (ASRUtils::is_array(ASRUtils::expr_type(args_[0])) &&
67096715
imported_functions[call_name] == "math" ) {
67106716
throw SemanticError("Function '" + call_name + "' does not accept vector values",
67116717
x.base.base.loc);

src/lpython/semantics/python_attribute_eval.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct AttributeHandler {
2020
AttributeHandler() {
2121
attribute_map = {
2222
{"int@bit_length", &eval_int_bit_length},
23+
{"array@size", &eval_array_size},
2324
{"list@append", &eval_list_append},
2425
{"list@remove", &eval_list_remove},
2526
{"list@count", &eval_list_count},
@@ -42,6 +43,8 @@ struct AttributeHandler {
4243
return "set";
4344
} else if (ASR::is_a<ASR::Dict_t>(*t)) {
4445
return "dict";
46+
} else if (ASRUtils::is_array(t)) {
47+
return "array";
4548
} else if (ASR::is_a<ASR::Integer_t>(*t)) {
4649
return "int";
4750
}
@@ -55,7 +58,7 @@ struct AttributeHandler {
5558
if (class_name == "") {
5659
throw SemanticError("Type name is not implemented yet.", loc);
5760
}
58-
std::string key = get_type_name(type) + "@" + attr_name;
61+
std::string key = class_name + "@" + attr_name;
5962
auto search = attribute_map.find(key);
6063
if (search != attribute_map.end()) {
6164
attribute_eval_callback cb = search->second;
@@ -77,6 +80,15 @@ struct AttributeHandler {
7780
return ASR::make_IntegerBitLen_t(al, loc, s, int_type, nullptr);
7881
}
7982

83+
static ASR::asr_t* eval_array_size(ASR::expr_t *s, Allocator &al, const Location &loc,
84+
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
85+
if (args.size() != 0) {
86+
throw SemanticError("array.size() takes no arguments", loc);
87+
}
88+
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4, nullptr, 0));
89+
return ASR::make_ArraySize_t(al, loc, s, nullptr, int_type, nullptr);
90+
}
91+
8092
static ASR::asr_t* eval_list_append(ASR::expr_t *s, Allocator &al, const Location &loc,
8193
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
8294
if (args.size() != 1) {

0 commit comments

Comments
 (0)