Skip to content

Commit b83c06c

Browse files
authored
Merge pull request #126 from namannimmo10/slice
Implement slice
2 parents f4cf11e + 807ed41 commit b83c06c

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,17 +823,33 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
823823
void visit_Subscript(const AST::Subscript_t &x) {
824824
this->visit_expr(*x.m_value);
825825
ASR::expr_t *value = ASRUtils::EXPR(tmp);
826-
this->visit_expr(*x.m_slice);
827-
ASR::expr_t *index = ASRUtils::EXPR(tmp);
828826
Vec<ASR::array_index_t> args;
829827
args.reserve(al, 1);
830828
ASR::array_index_t ai;
831829
ai.loc = x.base.base.loc;
832830
ai.m_left = nullptr;
833-
ai.m_right = index;
831+
ai.m_right = nullptr;
834832
ai.m_step = nullptr;
833+
if (AST::is_a<AST::Slice_t>(*x.m_slice)) {
834+
AST::Slice_t *s = AST::down_cast<AST::Slice_t>(x.m_slice);
835+
if (s->m_lower != nullptr) {
836+
this->visit_expr(*s->m_lower);
837+
ai.m_left = ASRUtils::EXPR(tmp);
838+
}
839+
if (s->m_upper != nullptr) {
840+
this->visit_expr(*s->m_upper);
841+
ai.m_right = ASRUtils::EXPR(tmp);
842+
}
843+
if (s->m_step != nullptr) {
844+
this->visit_expr(*s->m_step);
845+
ai.m_step = ASRUtils::EXPR(tmp);
846+
}
847+
} else {
848+
this->visit_expr(*x.m_slice);
849+
ASR::expr_t *index = ASRUtils::EXPR(tmp);
850+
ai.m_right = index;
851+
}
835852
args.push_back(al, ai);
836-
837853
ASR::symbol_t *s = ASR::down_cast<ASR::Var_t>(value)->m_v;
838854
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
839855
ASR::ttype_t *type = v->m_type;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-subscript1-1acfc19",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/subscript1.py",
5+
"infile_hash": "24efc3980ad13e8772a093253b380c91ce3b38071d69200e5af33d36",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "asr-subscript1-1acfc19.stdout",
9+
"stdout_hash": "f7bf6b29c18149b3c536af48eb59a1133156ac03fba83603a083d0fb",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_subscript: (Subroutine (SymbolTable 2 {A: (Variable 2 A Local () () Default (Integer 4 [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 5 (Integer 4 [])))]) Source Public Required .false.), B: (Variable 2 B Local () () Default (Integer 4 [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 [])))]) Source Public Required .false.), i: (Variable 2 i Local () () Default (Integer 4 []) Source Public Required .false.), s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_subscript [] [(= (Var 2 s) (ConstantString "abc" (Character 1 3 () [])) ()) (= (Var 2 s) (ArrayRef 2 s [(() (ConstantInteger 0 (Integer 4 [])) ())] (Character 1 -2 () []) ()) ()) (= (Var 2 s) (ArrayRef 2 s [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 [])) ())] (Character 1 -2 () []) ()) ()) (= (Var 2 i) (ArrayRef 2 A [(() (ConstantInteger 0 (Integer 4 [])) ())] (Integer 4 [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 5 (Integer 4 [])))]) ()) ()) (= (Var 2 B) (ArrayRef 2 A [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 3 (Integer 4 [])) ())] (Integer 4 [((ConstantInteger 1 (Integer 4 [])) (ConstantInteger 5 (Integer 4 [])))]) ()) ())] Source Public Implementation () .false. .false.)}) [])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "ast-subscript1-bd5584b",
3+
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
4+
"infile": "tests/subscript1.py",
5+
"infile_hash": "24efc3980ad13e8772a093253b380c91ce3b38071d69200e5af33d36",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "ast-subscript1-bd5584b.stdout",
9+
"stdout_hash": "a3078483928581b43987ad69903c0319507833a869b1e1fe7d52edb5",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(Module [(FunctionDef test_subscript ([] [] [] [] [] [] []) [(AnnAssign (Name s Store) (Name str Load) () 1) (Assign [(Name s Store)] (ConstantStr "abc" ()) ()) (Assign [(Name s Store)] (Subscript (Name s Load) (ConstantInt 0 ()) Load) ()) (Assign [(Name s Store)] (Subscript (Name s Load) (Slice (ConstantInt 1 ()) (ConstantInt 2 ()) ()) Load) ()) (AnnAssign (Name A Store) (Subscript (Name i32 Load) (ConstantInt 5 ()) Load) () 1) (AnnAssign (Name B Store) (Subscript (Name i32 Load) (ConstantInt 2 ()) Load) () 1) (AnnAssign (Name i Store) (Name i32 Load) () 1) (Assign [(Name i Store)] (Subscript (Name A Load) (ConstantInt 0 ()) Load) ()) (Assign [(Name B Store)] (Subscript (Name A Load) (Slice (ConstantInt 1 ()) (ConstantInt 3 ()) ()) Load) ())] [] () ())] [])

tests/subscript1.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def test_subscript():
2+
s: str
3+
s = 'abc'
4+
s = s[0]
5+
s = s[1:2]
6+
7+
A: i32[5]
8+
B: i32[2]
9+
i: i32
10+
i = A[0]
11+
B = A[1:3]

tests/tests.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ filename = "constants1.py"
2323
ast = true
2424
asr = true
2525

26+
[[test]]
27+
filename = "subscript1.py"
28+
ast = true
29+
asr = true
30+
2631
[[test]]
2732
filename = "list1.py"
2833
ast = true

0 commit comments

Comments
 (0)