Skip to content

Commit c354fdb

Browse files
committed
Make changes to use expr
1 parent 209e372 commit c354fdb

2 files changed

Lines changed: 35 additions & 41 deletions

File tree

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
357357
return v;
358358
}
359359

360-
void handle_attribute(ASR::symbol_t *s, std::string attr_name,
360+
void handle_attribute(ASR::expr_t *s, std::string attr_name,
361361
const Location &loc, Vec<ASR::expr_t*> &args) {
362362
tmp = attr_handler.get_attribute(s, attr_name, al, loc, args, diag);
363363
return;
@@ -1603,19 +1603,19 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
16031603
ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'",
16041604
index->base.loc);
16051605
}
1606-
tmp = make_DictItem_t(al, x.base.base.loc, s, index, nullptr,
1606+
tmp = make_DictItem_t(al, x.base.base.loc, value, index, nullptr,
16071607
ASR::down_cast<ASR::Dict_t>(type)->m_value_type, nullptr);
16081608
return;
16091609

16101610
} else if (ASR::is_a<ASR::List_t>(*type)) {
16111611
index = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
1612-
tmp = make_ListItem_t(al, x.base.base.loc, s, index,
1612+
tmp = make_ListItem_t(al, x.base.base.loc, value, index,
16131613
ASR::down_cast<ASR::List_t>(type)->m_type, nullptr);
16141614
return;
16151615
} else if (ASR::is_a<ASR::Tuple_t>(*type)) {
16161616
index = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
16171617
int i = ASR::down_cast<ASR::IntegerConstant_t>(ASRUtils::EXPR(tmp))->m_n;
1618-
tmp = make_TupleItem_t(al, x.base.base.loc, s, index,
1618+
tmp = make_TupleItem_t(al, x.base.base.loc, value, index,
16191619
ASR::down_cast<ASR::Tuple_t>(type)->m_type[i], nullptr);
16201620
return;
16211621
} else {
@@ -2216,7 +2216,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
22162216
);
22172217
throw SemanticAbort();
22182218
}
2219-
tmp = make_DictInsert_t(al, x.base.base.loc, s, key, val);
2219+
ASR::expr_t* se = ASR::down_cast<ASR::expr_t>(
2220+
ASR::make_Var_t(al, x.base.base.loc, s));
2221+
tmp = make_DictInsert_t(al, x.base.base.loc, se, key, val);
22202222
return;
22212223
}
22222224
}
@@ -2849,7 +2851,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
28492851
visit_expr(*c->m_args[i]);
28502852
elements.push_back(al, ASRUtils::EXPR(tmp));
28512853
}
2852-
handle_attribute(t, at->m_attr, x.base.base.loc, elements);
2854+
ASR::expr_t *te = ASR::down_cast<ASR::expr_t>(
2855+
ASR::make_Var_t(al, x.base.base.loc, t));
2856+
handle_attribute(te, at->m_attr, x.base.base.loc, elements);
28532857
return;
28542858
}
28552859
} else {
@@ -3100,7 +3104,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
31003104
for (size_t i=0; i<x.n_args; i++) {
31013105
eles.push_back(al, args[i].m_value);
31023106
}
3103-
handle_attribute(st, at->m_attr, x.base.base.loc, eles);
3107+
ASR::expr_t *se = ASR::down_cast<ASR::expr_t>(
3108+
ASR::make_Var_t(al, x.base.base.loc, st));
3109+
handle_attribute(se, at->m_attr, x.base.base.loc, eles);
31043110
return;
31053111
}
31063112
throw SemanticError("module '" + mod_name + "' is not imported",

src/lpython/semantics/python_attribute_eval.h

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace LFortran {
1111

1212
struct AttributeHandler {
1313

14-
typedef ASR::asr_t* (*attribute_eval_callback)(ASR::symbol_t*, Allocator &,
14+
typedef ASR::asr_t* (*attribute_eval_callback)(ASR::expr_t*, Allocator &,
1515
const Location &, Vec<ASR::expr_t*> &, diag::Diagnostics &);
1616

1717
std::map<std::string, attribute_eval_callback> attribute_map;
@@ -41,10 +41,9 @@ struct AttributeHandler {
4141
return "";
4242
}
4343

44-
ASR::asr_t* get_attribute(ASR::symbol_t *s, std::string attr_name,
44+
ASR::asr_t* get_attribute(ASR::expr_t *e, std::string attr_name,
4545
Allocator &al, const Location &loc, Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
46-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
47-
ASR::ttype_t *type = v->m_type;
46+
ASR::ttype_t *type = ASRUtils::expr_type(e);
4847
std::string class_name = get_type_name(type);
4948
if (class_name == "") {
5049
throw SemanticError("Type name is not implemented yet.", loc);
@@ -53,21 +52,20 @@ struct AttributeHandler {
5352
auto search = attribute_map.find(key);
5453
if (search != attribute_map.end()) {
5554
attribute_eval_callback cb = search->second;
56-
return cb(s, al, loc, args, diag);
55+
return cb(e, al, loc, args, diag);
5756
} else {
5857
throw SemanticError(class_name + "." + attr_name + " is not implemented yet",
5958
loc);
6059
}
6160
}
6261

63-
static ASR::asr_t* eval_list_append(ASR::symbol_t *s, Allocator &al, const Location &loc,
62+
static ASR::asr_t* eval_list_append(ASR::expr_t *s, Allocator &al, const Location &loc,
6463
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
6564
if (args.size() != 1) {
6665
throw SemanticError("append() takes exactly one argument",
6766
loc);
6867
}
69-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
70-
ASR::ttype_t *type = v->m_type;
68+
ASR::ttype_t *type = ASRUtils::expr_type(s);
7169
ASR::ttype_t *list_type = ASR::down_cast<ASR::List_t>(type)->m_type;
7270
ASR::ttype_t *ele_type = ASRUtils::expr_type(args[0]);
7371
if (!ASRUtils::check_equal_type(ele_type, list_type)) {
@@ -85,14 +83,13 @@ struct AttributeHandler {
8583
return make_ListAppend_t(al, loc, s, args[0]);
8684
}
8785

88-
static ASR::asr_t* eval_list_remove(ASR::symbol_t *s, Allocator &al, const Location &loc,
86+
static ASR::asr_t* eval_list_remove(ASR::expr_t *s, Allocator &al, const Location &loc,
8987
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
9088
if (args.size() != 1) {
9189
throw SemanticError("remove() takes exactly one argument",
9290
loc);
9391
}
94-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
95-
ASR::ttype_t *type = v->m_type;
92+
ASR::ttype_t *type = ASRUtils::expr_type(s);
9693
ASR::ttype_t *list_type = ASR::down_cast<ASR::List_t>(type)->m_type;
9794
ASR::ttype_t *ele_type = ASRUtils::expr_type(args[0]);
9895
if (!ASRUtils::check_equal_type(ele_type, list_type)) {
@@ -110,7 +107,7 @@ struct AttributeHandler {
110107
return make_ListRemove_t(al, loc, s, args[0]);
111108
}
112109

113-
static ASR::asr_t* eval_list_insert(ASR::symbol_t *s, Allocator &al, const Location &loc,
110+
static ASR::asr_t* eval_list_insert(ASR::expr_t *s, Allocator &al, const Location &loc,
114111
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
115112
if (args.size() != 2) {
116113
throw SemanticError("insert() takes exactly two arguments",
@@ -125,8 +122,7 @@ struct AttributeHandler {
125122
}
126123

127124
ASR::ttype_t *ele_type = ASRUtils::expr_type(args[1]);
128-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
129-
ASR::ttype_t *type = v->m_type;
125+
ASR::ttype_t *type = ASRUtils::expr_type(s);
130126
ASR::ttype_t *list_type = ASR::down_cast<ASR::List_t>(type)->m_type;
131127
if (!ASRUtils::check_equal_type(ele_type, list_type)) {
132128
std::string fnd = ASRUtils::type_to_str_python(ele_type);
@@ -143,7 +139,7 @@ struct AttributeHandler {
143139
return make_ListInsert_t(al, loc, s, args[0], args[1]);
144140
}
145141

146-
static ASR::asr_t* eval_list_pop(ASR::symbol_t *s, Allocator &al, const Location &loc,
142+
static ASR::asr_t* eval_list_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
147143
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
148144
if (args.size() > 1) {
149145
throw SemanticError("pop() takes atmost one argument",
@@ -152,8 +148,7 @@ struct AttributeHandler {
152148
ASR::expr_t *idx = nullptr;
153149
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
154150
4, nullptr, 0));
155-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
156-
ASR::ttype_t *type = v->m_type;
151+
ASR::ttype_t *type = ASRUtils::expr_type(s);
157152
ASR::ttype_t *list_type = ASR::down_cast<ASR::List_t>(type)->m_type;
158153
if (args.size() == 1) {
159154
ASR::ttype_t *pos_type = ASRUtils::expr_type(args[0]);
@@ -178,28 +173,24 @@ struct AttributeHandler {
178173
return make_ListPop_t(al, loc, s, idx, list_type, nullptr);
179174
}
180175

181-
static ASR::asr_t* eval_set_pop(ASR::symbol_t *s, Allocator &al, const Location &loc,
176+
static ASR::asr_t* eval_set_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
182177
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
183178
if (args.size() != 0) {
184179
throw SemanticError("pop() takes no arguments (" + std::to_string(args.size()) + " given)", loc);
185180
}
186-
187-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
188-
189-
ASR::ttype_t *type = v->m_type;
181+
ASR::ttype_t *type = ASRUtils::expr_type(s);
190182
ASR::ttype_t *set_type = ASR::down_cast<ASR::Set_t>(type)->m_type;
191183

192184
return make_SetPop_t(al, loc, s, set_type, nullptr);
193185
}
194186

195-
static ASR::asr_t* eval_set_add(ASR::symbol_t *s, Allocator &al, const Location &loc,
187+
static ASR::asr_t* eval_set_add(ASR::expr_t *s, Allocator &al, const Location &loc,
196188
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
197189
if (args.size() != 1) {
198190
throw SemanticError("add() takes exactly one argument", loc);
199191
}
200192

201-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
202-
ASR::ttype_t *type = v->m_type;
193+
ASR::ttype_t *type = ASRUtils::expr_type(s);
203194
ASR::ttype_t *set_type = ASR::down_cast<ASR::Set_t>(type)->m_type;
204195
ASR::ttype_t *ele_type = ASRUtils::expr_type(args[0]);
205196
if (!ASRUtils::check_equal_type(ele_type, set_type)) {
@@ -218,14 +209,13 @@ struct AttributeHandler {
218209
return make_SetInsert_t(al, loc, s, args[0]);
219210
}
220211

221-
static ASR::asr_t* eval_set_remove(ASR::symbol_t *s, Allocator &al, const Location &loc,
212+
static ASR::asr_t* eval_set_remove(ASR::expr_t *s, Allocator &al, const Location &loc,
222213
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
223214
if (args.size() != 1) {
224215
throw SemanticError("remove() takes exactly one argument", loc);
225216
}
226217

227-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
228-
ASR::ttype_t *type = v->m_type;
218+
ASR::ttype_t *type = ASRUtils::expr_type(s);
229219
ASR::ttype_t *set_type = ASR::down_cast<ASR::Set_t>(type)->m_type;
230220
ASR::ttype_t *ele_type = ASRUtils::expr_type(args[0]);
231221
if (!ASRUtils::check_equal_type(ele_type, set_type)) {
@@ -244,15 +234,14 @@ struct AttributeHandler {
244234
return make_SetRemove_t(al, loc, s, args[0]);
245235
}
246236

247-
static ASR::asr_t* eval_dict_get(ASR::symbol_t *s, Allocator &al, const Location &loc,
237+
static ASR::asr_t* eval_dict_get(ASR::expr_t *s, Allocator &al, const Location &loc,
248238
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
249239
ASR::expr_t *def = nullptr;
250240
if (args.size() > 2 || args.size() < 1) {
251241
throw SemanticError("'get' takes atleast 1 and atmost 2 arguments",
252242
loc);
253243
}
254-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
255-
ASR::ttype_t *type = v->m_type;
244+
ASR::ttype_t *type = ASRUtils::expr_type(s);
256245
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;
257246
ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;
258247
if (args.size() == 2) {
@@ -285,13 +274,12 @@ struct AttributeHandler {
285274
return make_DictItem_t(al, loc, s, args[0], def, value_type, nullptr);
286275
}
287276

288-
static ASR::asr_t* eval_dict_pop(ASR::symbol_t *s, Allocator &al, const Location &loc,
277+
static ASR::asr_t* eval_dict_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
289278
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
290279
if (args.size() != 1) {
291280
throw SemanticError("'pop' takes only one argument for now", loc);
292281
}
293-
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
294-
ASR::ttype_t *type = v->m_type;
282+
ASR::ttype_t *type = ASRUtils::expr_type(s);
295283
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;
296284
ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;
297285
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(args[0]), key_type)) {

0 commit comments

Comments
 (0)