Skip to content

Commit f8ced2b

Browse files
committed
AST->ASR: Add 1 to string indices
1 parent 628c862 commit f8ced2b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
967967
tmp = ASR::make_Assert_t(al, x.base.base.loc, test, msg);
968968
}
969969

970+
ASR::expr_t *index_add_one(const Location &loc, ASR::expr_t *idx) {
971+
// Add 1 to the index `idx`, assumes `idx` is of type Integer 4
972+
ASR::expr_t *overloaded = nullptr;
973+
ASR::expr_t *comptime_value = nullptr;
974+
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
975+
4, nullptr, 0));
976+
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_ConstantInteger_t(
977+
al, loc, 1, a_type));
978+
return ASRUtils::EXPR(ASR::make_BinOp_t(al, loc, idx,
979+
ASR::binopType::Add, constant_one, a_type,
980+
comptime_value, overloaded));
981+
}
982+
970983
void visit_Subscript(const AST::Subscript_t &x) {
971984
this->visit_expr(*x.m_value);
972985
ASR::expr_t *value = ASRUtils::EXPR(tmp);
@@ -981,19 +994,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
981994
AST::Slice_t *s = AST::down_cast<AST::Slice_t>(x.m_slice);
982995
if (s->m_lower != nullptr) {
983996
this->visit_expr(*s->m_lower);
984-
ai.m_left = ASRUtils::EXPR(tmp);
997+
ai.m_left = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
985998
}
986999
if (s->m_upper != nullptr) {
9871000
this->visit_expr(*s->m_upper);
988-
ai.m_right = ASRUtils::EXPR(tmp);
1001+
ai.m_right = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
9891002
}
9901003
if (s->m_step != nullptr) {
9911004
this->visit_expr(*s->m_step);
992-
ai.m_step = ASRUtils::EXPR(tmp);
1005+
ai.m_step = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
9931006
}
9941007
} else {
9951008
this->visit_expr(*x.m_slice);
996-
ASR::expr_t *index = ASRUtils::EXPR(tmp);
1009+
ASR::expr_t *index = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
9971010
ai.m_right = index;
9981011
}
9991012
ASR::symbol_t *s = ASR::down_cast<ASR::Var_t>(value)->m_v;

0 commit comments

Comments
 (0)