From 81b73727c01e838dce74d29bc54be18e9db19a36 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 10 Feb 2022 04:45:17 +0530 Subject: [PATCH 1/4] Modify Subscript visitor to support slice --- src/lpython/semantics/python_ast_to_asr.cpp | 24 +++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 030ef64608..215d55b178 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -823,17 +823,33 @@ class BodyVisitor : public CommonVisitor { void visit_Subscript(const AST::Subscript_t &x) { this->visit_expr(*x.m_value); ASR::expr_t *value = ASRUtils::EXPR(tmp); - this->visit_expr(*x.m_slice); - ASR::expr_t *index = ASRUtils::EXPR(tmp); Vec args; args.reserve(al, 1); ASR::array_index_t ai; ai.loc = x.base.base.loc; ai.m_left = nullptr; - ai.m_right = index; + ai.m_right = nullptr; ai.m_step = nullptr; + if (AST::is_a(*x.m_slice)) { + AST::Slice_t *s = AST::down_cast(x.m_slice); + if (s->m_lower != nullptr) { + this->visit_expr(*s->m_lower); + ai.m_left = ASRUtils::EXPR(tmp); + } + if (s->m_upper != nullptr) { + this->visit_expr(*s->m_upper); + ai.m_right = ASRUtils::EXPR(tmp); + } + if (s->m_step != nullptr) { + this->visit_expr(*s->m_step); + ai.m_step = ASRUtils::EXPR(tmp); + } + } else { + this->visit_expr(*x.m_slice); + ASR::expr_t *index = ASRUtils::EXPR(tmp); + ai.m_right = index; + } args.push_back(al, ai); - ASR::symbol_t *s = ASR::down_cast(value)->m_v; ASR::Variable_t *v = ASR::down_cast(s); ASR::ttype_t *type = v->m_type; From 96b5b5d5f6c89487c6ec32f0e76292ed231b8431 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 10 Feb 2022 04:46:27 +0530 Subject: [PATCH 2/4] Implement visitor: Slice --- src/lpython/semantics/python_ast_to_asr.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 215d55b178..3ba8cc184f 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -857,6 +857,22 @@ class BodyVisitor : public CommonVisitor { args.size(), type, nullptr); } + void visit_Slice(const AST::Slice_t &x) { + if (x.m_lower != nullptr) { + this->visit_expr(*x.m_lower); + ASRUtils::EXPR(tmp); + } + if (x.m_upper != nullptr) { + this->visit_expr(*x.m_upper); + ASRUtils::EXPR(tmp); + } + if (x.m_step != nullptr) { + this->visit_expr(*x.m_step); + ASRUtils::EXPR(tmp); + } + tmp = nullptr; + } + void visit_List(const AST::List_t &x) { Vec list; list.reserve(al, x.n_elts); From a0cdb71c553e95bf3d774fea2a47a4949e650d9a Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 10 Feb 2022 04:55:44 +0530 Subject: [PATCH 3/4] Register the test for slice --- tests/reference/asr-subscript1-1acfc19.json | 13 +++++++++++++ tests/reference/asr-subscript1-1acfc19.stdout | 1 + tests/reference/ast-subscript1-bd5584b.json | 13 +++++++++++++ tests/reference/ast-subscript1-bd5584b.stdout | 1 + tests/subscript1.py | 5 +++++ tests/tests.toml | 5 +++++ 6 files changed, 38 insertions(+) create mode 100644 tests/reference/asr-subscript1-1acfc19.json create mode 100644 tests/reference/asr-subscript1-1acfc19.stdout create mode 100644 tests/reference/ast-subscript1-bd5584b.json create mode 100644 tests/reference/ast-subscript1-bd5584b.stdout create mode 100644 tests/subscript1.py diff --git a/tests/reference/asr-subscript1-1acfc19.json b/tests/reference/asr-subscript1-1acfc19.json new file mode 100644 index 0000000000..b4933268bc --- /dev/null +++ b/tests/reference/asr-subscript1-1acfc19.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-subscript1-1acfc19", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/subscript1.py", + "infile_hash": "2046e24d81cbd28eb23052804f256dc73c52bf0b1677d787078607b1", + "outfile": null, + "outfile_hash": null, + "stdout": "asr-subscript1-1acfc19.stdout", + "stdout_hash": "330bce95c399e53617ba71cb6e097b05ec47b7991b3ac7e59ea9e469", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/asr-subscript1-1acfc19.stdout b/tests/reference/asr-subscript1-1acfc19.stdout new file mode 100644 index 0000000000..6942154252 --- /dev/null +++ b/tests/reference/asr-subscript1-1acfc19.stdout @@ -0,0 +1 @@ +(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_subscript: (Subroutine (SymbolTable 2 {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 () []) ()) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/ast-subscript1-bd5584b.json b/tests/reference/ast-subscript1-bd5584b.json new file mode 100644 index 0000000000..ba0639ea74 --- /dev/null +++ b/tests/reference/ast-subscript1-bd5584b.json @@ -0,0 +1,13 @@ +{ + "basename": "ast-subscript1-bd5584b", + "cmd": "lpython --show-ast --no-color {infile} -o {outfile}", + "infile": "tests/subscript1.py", + "infile_hash": "2046e24d81cbd28eb23052804f256dc73c52bf0b1677d787078607b1", + "outfile": null, + "outfile_hash": null, + "stdout": "ast-subscript1-bd5584b.stdout", + "stdout_hash": "a1bdbcd5217c00a08079072c0580e6fea0a95e94aa73905cd3932ba5", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/ast-subscript1-bd5584b.stdout b/tests/reference/ast-subscript1-bd5584b.stdout new file mode 100644 index 0000000000..45819f2322 --- /dev/null +++ b/tests/reference/ast-subscript1-bd5584b.stdout @@ -0,0 +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) ())] [] () ())] []) diff --git a/tests/subscript1.py b/tests/subscript1.py new file mode 100644 index 0000000000..d295001299 --- /dev/null +++ b/tests/subscript1.py @@ -0,0 +1,5 @@ +def test_subscript(): + s: str + s = 'abc' + s = s[0] + s = s[1:2] diff --git a/tests/tests.toml b/tests/tests.toml index 8510d5bcc3..a209cbcc19 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -23,6 +23,11 @@ filename = "constants1.py" ast = true asr = true +[[test]] +filename = "subscript1.py" +ast = true +asr = true + [[test]] filename = "list1.py" ast = true From 807ed41ffa2ffff56fe154504e98a591390385c6 Mon Sep 17 00:00:00 2001 From: Naman Gera Date: Thu, 10 Feb 2022 14:32:05 +0530 Subject: [PATCH 4/4] Add test for array subscript --- src/lpython/semantics/python_ast_to_asr.cpp | 16 ---------------- tests/reference/asr-subscript1-1acfc19.json | 4 ++-- tests/reference/asr-subscript1-1acfc19.stdout | 2 +- tests/reference/ast-subscript1-bd5584b.json | 4 ++-- tests/reference/ast-subscript1-bd5584b.stdout | 2 +- tests/subscript1.py | 6 ++++++ 6 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 3ba8cc184f..215d55b178 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -857,22 +857,6 @@ class BodyVisitor : public CommonVisitor { args.size(), type, nullptr); } - void visit_Slice(const AST::Slice_t &x) { - if (x.m_lower != nullptr) { - this->visit_expr(*x.m_lower); - ASRUtils::EXPR(tmp); - } - if (x.m_upper != nullptr) { - this->visit_expr(*x.m_upper); - ASRUtils::EXPR(tmp); - } - if (x.m_step != nullptr) { - this->visit_expr(*x.m_step); - ASRUtils::EXPR(tmp); - } - tmp = nullptr; - } - void visit_List(const AST::List_t &x) { Vec list; list.reserve(al, x.n_elts); diff --git a/tests/reference/asr-subscript1-1acfc19.json b/tests/reference/asr-subscript1-1acfc19.json index b4933268bc..f95b20cc69 100644 --- a/tests/reference/asr-subscript1-1acfc19.json +++ b/tests/reference/asr-subscript1-1acfc19.json @@ -2,11 +2,11 @@ "basename": "asr-subscript1-1acfc19", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/subscript1.py", - "infile_hash": "2046e24d81cbd28eb23052804f256dc73c52bf0b1677d787078607b1", + "infile_hash": "24efc3980ad13e8772a093253b380c91ce3b38071d69200e5af33d36", "outfile": null, "outfile_hash": null, "stdout": "asr-subscript1-1acfc19.stdout", - "stdout_hash": "330bce95c399e53617ba71cb6e097b05ec47b7991b3ac7e59ea9e469", + "stdout_hash": "f7bf6b29c18149b3c536af48eb59a1133156ac03fba83603a083d0fb", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-subscript1-1acfc19.stdout b/tests/reference/asr-subscript1-1acfc19.stdout index 6942154252..8be93abb61 100644 --- a/tests/reference/asr-subscript1-1acfc19.stdout +++ b/tests/reference/asr-subscript1-1acfc19.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_subscript: (Subroutine (SymbolTable 2 {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 () []) ()) ())] Source Public Implementation () .false. .false.)}) []) +(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.)}) []) diff --git a/tests/reference/ast-subscript1-bd5584b.json b/tests/reference/ast-subscript1-bd5584b.json index ba0639ea74..67b04dcb2c 100644 --- a/tests/reference/ast-subscript1-bd5584b.json +++ b/tests/reference/ast-subscript1-bd5584b.json @@ -2,11 +2,11 @@ "basename": "ast-subscript1-bd5584b", "cmd": "lpython --show-ast --no-color {infile} -o {outfile}", "infile": "tests/subscript1.py", - "infile_hash": "2046e24d81cbd28eb23052804f256dc73c52bf0b1677d787078607b1", + "infile_hash": "24efc3980ad13e8772a093253b380c91ce3b38071d69200e5af33d36", "outfile": null, "outfile_hash": null, "stdout": "ast-subscript1-bd5584b.stdout", - "stdout_hash": "a1bdbcd5217c00a08079072c0580e6fea0a95e94aa73905cd3932ba5", + "stdout_hash": "a3078483928581b43987ad69903c0319507833a869b1e1fe7d52edb5", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/ast-subscript1-bd5584b.stdout b/tests/reference/ast-subscript1-bd5584b.stdout index 45819f2322..e1a82401cb 100644 --- a/tests/reference/ast-subscript1-bd5584b.stdout +++ b/tests/reference/ast-subscript1-bd5584b.stdout @@ -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) ())] [] () ())] []) +(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) ())] [] () ())] []) diff --git a/tests/subscript1.py b/tests/subscript1.py index d295001299..7130c598b8 100644 --- a/tests/subscript1.py +++ b/tests/subscript1.py @@ -3,3 +3,9 @@ def test_subscript(): s = 'abc' s = s[0] s = s[1:2] + + A: i32[5] + B: i32[2] + i: i32 + i = A[0] + B = A[1:3]