Skip to content

Commit

Permalink
Apply suggestions from code review by @certik
Browse files Browse the repository at this point in the history
  • Loading branch information
namannimmo10 committed Feb 1, 2022
1 parent e60076b commit 93c7dab
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion grammar/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ ttype
| Character(int kind, int len, expr? len_expr, dimension* dims)
| Logical(int kind, dimension* dims)
| Set(ttype type)
| Tuple(ttype type)
| Tuple(ttype* type)
| Derived(symbol derived_type, dimension* dims)
| Class(symbol class_type, dimension* dims)
| Dict(ttype key_type, ttype value_type)
Expand Down
24 changes: 10 additions & 14 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,20 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}

void visit_Tuple(const AST::Tuple_t &x) {
Vec<ASR::expr_t*> tuple;
tuple.reserve(al, x.n_elts);
ASR::ttype_t *type = nullptr;
Vec<ASR::expr_t*> elements;
elements.reserve(al, x.n_elts);
Vec<ASR::ttype_t*> tuple_type_vec;
tuple_type_vec.reserve(al, x.n_elts);
for (size_t i=0; i<x.n_elts; i++) {
this->visit_expr(*x.m_elts[i]);
ASR::expr_t *expr = ASRUtils::EXPR(tmp);
if (type == nullptr) {
type = ASRUtils::expr_type(expr);
} else {
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) {
throw SemanticError("All Tuple elements must be of the same type for now",
x.base.base.loc);
}
}
tuple.push_back(al, expr);
elements.push_back(al, expr);
tuple_type_vec.push_back(al, ASRUtils::expr_type(expr));
}
ASR::ttype_t *tuple_type = ASRUtils::TYPE(ASR::make_Tuple_t(al, x.base.base.loc, type));
tmp = ASR::make_ConstantTuple_t(al, x.base.base.loc, tuple.p, tuple.size(), tuple_type);
ASR::ttype_t *tuple_type = ASRUtils::TYPE(ASR::make_Tuple_t(al, x.base.base.loc,
tuple_type_vec.p, tuple_type_vec.n));
tmp = ASR::make_ConstantTuple_t(al, x.base.base.loc,
elements.p, elements.size(), tuple_type);
}

void visit_For(const AST::For_t &x) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/lpython_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def visit_Constant(self, node):
if isinstance(node.value, str):
return python_ast.ConstantStr(node.value, node.kind)
elif isinstance(node.value, list):
return python_ast.List(node.elts)
return python_ast.List(node.elts, node.ctx)
elif isinstance(node.value, bool):
return python_ast.ConstantBool(node.value, node.kind)
elif isinstance(node.value, int):
Expand All @@ -36,7 +36,7 @@ def visit_Constant(self, node):
elif isinstance(node.value, dict):
return python_ast.Dict(node.keys, node.values)
elif isinstance(node.value, tuple):
return python_ast.Tuple(node.elts)
return python_ast.Tuple(node.elts, node.ctx)
else:
print(type(node.value))
raise Exception("Unsupported Constant type")
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-tuple1-09972ab.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-tuple1-09972ab",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/tuple1.py",
"infile_hash": "c216176d4d1417b152919937341f86d003e29ee69ced98dff06293fa",
"infile_hash": "dd36acaa0b39916fa5d707bf66a250a1d628cdf0e5824cb412931fe5",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-tuple1-09972ab.stdout",
"stdout_hash": "c75404eba3e5698ef6787f2d09e8421071515d847e16af38ff054c21",
"stdout_hash": "79bdcbd6ccf8c8a8e4f0143d3b1b5ab980cfa92f39131570c0d51b8b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-tuple1-09972ab.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {test_Tuple: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Integer 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Integer 4 []) Source Public Required .false.)}) test_Tuple [] [(= (Var 2 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 [])) (ConstantInteger 3 (Integer 4 []))] (Tuple (Integer 4 []))) ()) (= (Var 2 a) (ConstantTuple [(UnaryOp USub (ConstantInteger 3 (Integer 4 [])) (Integer 4 []) (ConstantInteger -3 (Integer 4 []))) (UnaryOp USub (ConstantInteger 2 (Integer 4 [])) (Integer 4 []) (ConstantInteger -2 (Integer 4 []))) (UnaryOp USub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger -1 (Integer 4 [])))] (Tuple (Integer 4 []))) ()) (= (Var 2 a) (ConstantTuple [(ConstantString "a" (Character 1 1 () [])) (ConstantString "b" (Character 1 1 () [])) (ConstantString "c" (Character 1 1 () []))] (Tuple (Character 1 1 () []))) ()) (= (Var 2 b) (ArrayRef 2 a [(() (ConstantInteger 0 (Integer 4 [])) ())] (Integer 4 []) ()) ()) (= (ConstantTuple [(Var 2 b) (Var 2 b1)] (Tuple (Integer 4 []))) (ConstantTuple [(ArrayRef 2 a [(() (ConstantInteger 2 (Integer 4 [])) ())] (Integer 4 []) ()) (ArrayRef 2 a [(() (ConstantInteger 1 (Integer 4 [])) ())] (Integer 4 []) ())] (Tuple (Integer 4 []))) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {test_Tuple: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Integer 4 []) Source Public Required .false.), b: (Variable 2 b Local () () Default (Integer 4 []) Source Public Required .false.), b1: (Variable 2 b1 Local () () Default (Integer 4 []) Source Public Required .false.)}) test_Tuple [] [(= (Var 2 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 [])) (ConstantInteger 3 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 []) (Integer 4 [])])) ()) (= (Var 2 a) (ConstantTuple [(UnaryOp USub (ConstantInteger 3 (Integer 4 [])) (Integer 4 []) (ConstantInteger -3 (Integer 4 []))) (UnaryOp USub (ConstantInteger 2 (Integer 4 [])) (Integer 4 []) (ConstantInteger -2 (Integer 4 []))) (UnaryOp USub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger -1 (Integer 4 [])))] (Tuple [(Integer 4 []) (Integer 4 []) (Integer 4 [])])) ()) (= (Var 2 a) (ConstantTuple [(ConstantString "a" (Character 1 1 () [])) (ConstantString "b" (Character 1 1 () [])) (ConstantString "c" (Character 1 1 () []))] (Tuple [(Character 1 1 () []) (Character 1 1 () []) (Character 1 1 () [])])) ()) (= (Var 2 a) (ConstantTuple [(UnaryOp USub (ConstantInteger 2 (Integer 4 [])) (Integer 4 []) (ConstantInteger -2 (Integer 4 []))) (UnaryOp USub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger -1 (Integer 4 []))) (ConstantReal 0.450000 (Real 8 [])) (ConstantString "d" (Character 1 1 () []))] (Tuple [(Integer 4 []) (Integer 4 []) (Real 8 []) (Character 1 1 () [])])) ()) (= (Var 2 b) (ArrayRef 2 a [(() (ConstantInteger 0 (Integer 4 [])) ())] (Integer 4 []) ()) ()) (= (ConstantTuple [(Var 2 b) (Var 2 b1)] (Tuple [(Integer 4 []) (Integer 4 [])])) (ConstantTuple [(ArrayRef 2 a [(() (ConstantInteger 2 (Integer 4 [])) ())] (Integer 4 []) ()) (ArrayRef 2 a [(() (ConstantInteger 1 (Integer 4 [])) ())] (Integer 4 []) ())] (Tuple [(Integer 4 []) (Integer 4 [])])) ())] Source Public Implementation () .false. .false.)}) [])
4 changes: 2 additions & 2 deletions tests/reference/ast-tuple1-2fb5396.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast-tuple1-2fb5396",
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
"infile": "tests/tuple1.py",
"infile_hash": "c216176d4d1417b152919937341f86d003e29ee69ced98dff06293fa",
"infile_hash": "dd36acaa0b39916fa5d707bf66a250a1d628cdf0e5824cb412931fe5",
"outfile": null,
"outfile_hash": null,
"stdout": "ast-tuple1-2fb5396.stdout",
"stdout_hash": "e88991d4093266a2aad97d5d60c6cc9e1313417c791e3bcea8f7e0ea",
"stdout_hash": "7a36761e69666aed813a66d3c1239111ca09aac2cdfa4853be3b4062",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast-tuple1-2fb5396.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(FunctionDef test_Tuple ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Name i32 Load) () 1) (AnnAssign (Name b Store) (Name i32 Load) () 1) (AnnAssign (Name b1 Store) (Name i32 Load) () 1) (Assign [(Name a Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) ()) (Assign [(Name a Store)] (Tuple [(UnaryOp USub (ConstantInt 3 ())) (UnaryOp USub (ConstantInt 2 ())) (UnaryOp USub (ConstantInt 1 ()))] Load) ()) (Assign [(Name a Store)] (Tuple [(ConstantStr "a" ()) (ConstantStr "b" ()) (ConstantStr "c" ())] Load) ()) (Assign [(Name b Store)] (Subscript (Name a Load) (ConstantInt 0 ()) Load) ()) (Assign [(Tuple [(Name b Store) (Name b1 Store)] Store)] (Tuple [(Subscript (Name a Load) (ConstantInt 2 ()) Load) (Subscript (Name a Load) (ConstantInt 1 ()) Load)] Load) ())] [] () ())] [])
(Module [(FunctionDef test_Tuple ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Name i32 Load) () 1) (AnnAssign (Name b Store) (Name i32 Load) () 1) (AnnAssign (Name b1 Store) (Name i32 Load) () 1) (Assign [(Name a Store)] (Tuple [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())] Load) ()) (Assign [(Name a Store)] (Tuple [(UnaryOp USub (ConstantInt 3 ())) (UnaryOp USub (ConstantInt 2 ())) (UnaryOp USub (ConstantInt 1 ()))] Load) ()) (Assign [(Name a Store)] (Tuple [(ConstantStr "a" ()) (ConstantStr "b" ()) (ConstantStr "c" ())] Load) ()) (Assign [(Name a Store)] (Tuple [(UnaryOp USub (ConstantInt 2 ())) (UnaryOp USub (ConstantInt 1 ())) (ConstantFloat 0.450000 ()) (ConstantStr "d" ())] Load) ()) (Assign [(Name b Store)] (Subscript (Name a Load) (ConstantInt 0 ()) Load) ()) (Assign [(Tuple [(Name b Store) (Name b1 Store)] Store)] (Tuple [(Subscript (Name a Load) (ConstantInt 2 ()) Load) (Subscript (Name a Load) (ConstantInt 1 ()) Load)] Load) ())] [] () ())] [])
1 change: 1 addition & 0 deletions tests/tuple1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def test_Tuple():
a = (1, 2, 3)
a = (-3, -2, -1)
a = ("a", "b", "c")
a = (-2, -1, 0.45, "d")

b = a[0]
b, b1 = a[2], a[1]

0 comments on commit 93c7dab

Please sign in to comment.