Skip to content

Commit

Permalink
Merge pull request lcompilers#499 from namannimmo10/set_pop
Browse files Browse the repository at this point in the history
Add Set pop in ASR
  • Loading branch information
namannimmo10 authored May 17, 2022
2 parents fbffc79 + 829d307 commit bb41e2d
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ expr
| ListSection(expr a, array_index section, ttype type, expr? value)
| ListPop(symbol a, expr? index, ttype type, expr? value)
| DictPop(symbol a, expr key, ttype type, expr? value)
| SetPop(symbol a, ttype type, expr? value)

-- `len` in Character:
-- >=0 ... the length of the string, known at compile time
Expand Down
2 changes: 2 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static inline ASR::ttype_t* expr_type(const ASR::expr_t *f)
case ASR::exprType::ListSection: { return ((ASR::ListSection_t*)f)->m_type; }
case ASR::exprType::ListPop: { return ((ASR::ListPop_t*)f)->m_type; }
case ASR::exprType::DictPop: { return ((ASR::DictPop_t*)f)->m_type; }
case ASR::exprType::SetPop: { return ((ASR::SetPop_t*)f)->m_type; }
default : throw LFortranException("Not implemented");
}
}
Expand Down Expand Up @@ -313,6 +314,7 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
case ASR::exprType::ListSection: { return ASR::down_cast<ASR::ListSection_t>(f)->m_value; }
case ASR::exprType::ListPop: { return ASR::down_cast<ASR::ListPop_t>(f)->m_value; }
case ASR::exprType::DictPop: { return ASR::down_cast<ASR::DictPop_t>(f)->m_value; }
case ASR::exprType::SetPop: { return ASR::down_cast<ASR::SetPop_t>(f)->m_value; }
case ASR::exprType::DictItem: // Drop through
case ASR::exprType::ArrayConstant: // Drop through
case ASR::exprType::IntegerConstant: // Drop through
Expand Down
15 changes: 15 additions & 0 deletions src/lpython/semantics/python_attribute_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct AttributeHandler {
{"list@remove", &eval_list_remove},
{"list@insert", &eval_list_insert},
{"list@pop", &eval_list_pop},
{"set@pop", &eval_set_pop},
{"set@add", &eval_set_add},
{"set@remove", &eval_set_remove},
{"dict@get", &eval_dict_get},
Expand Down Expand Up @@ -177,6 +178,20 @@ struct AttributeHandler {
return make_ListPop_t(al, loc, s, idx, list_type, nullptr);
}

static ASR::asr_t* eval_set_pop(ASR::symbol_t *s, Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
if (args.size() != 0) {
throw SemanticError("pop() takes no arguments (" + std::to_string(args.size()) + " given)", loc);
}

ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);

ASR::ttype_t *type = v->m_type;
ASR::ttype_t *set_type = ASR::down_cast<ASR::Set_t>(type)->m_type;

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

static ASR::asr_t* eval_set_add(ASR::symbol_t *s, Allocator &al, const Location &loc,
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
if (args.size() != 1) {
Expand Down
5 changes: 5 additions & 0 deletions tests/errors/test_set3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def f():
s: set[i32]
s = {1, 2, 3, 4}
i: i32
i = s.pop(3)
4 changes: 2 additions & 2 deletions tests/reference/asr-set1-b7b913a.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-set1-b7b913a",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/set1.py",
"infile_hash": "1fecd0d73e1cca64b609d716c1db4d8c647ac8e37850f5aaaf36df74",
"infile_hash": "63b5dc6a1f9c17099f1b10c8c45fcac1e50319ca8efbf7c283060abb",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-set1-b7b913a.stdout",
"stdout_hash": "3992bc8f92b7f965fa5414352c535e4140234fe248135e76cb3143f2",
"stdout_hash": "07fd8c206e47eff16ad2c2cdd3079b9eede212c10958457c80406356",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-set1-b7b913a.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_Set: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Set (Integer 4 [])) Source Public Required .false.), b: (Variable 2 b Local () () Default (Set (Character 1 -2 () [])) Source Public Required .false.)}) test_Set [] [(= (Var 2 a) (SetConstant [(IntegerConstant 1 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])) (IntegerConstant 3 (Integer 4 []))] (Set (Integer 4 []))) ()) (= (Var 2 a) (SetConstant [(IntegerConstant 2 (Integer 4 [])) (IntegerConstant 3 (Integer 4 [])) (IntegerConstant 4 (Integer 4 [])) (IntegerConstant 5 (Integer 4 [])) (IntegerConstant 5 (Integer 4 []))] (Set (Integer 4 []))) ()) (SetInsert 2 a (IntegerConstant 9 (Integer 4 []))) (SetRemove 2 a (IntegerConstant 4 (Integer 4 []))) (= (Var 2 b) (SetConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () [])) (StringConstant "c" (Character 1 1 () []))] (Set (Character 1 1 () []))) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_Set: (Subroutine (SymbolTable 2 {a: (Variable 2 a Local () () Default (Set (Integer 4 [])) Source Public Required .false.), b: (Variable 2 b Local () () Default (Set (Character 1 -2 () [])) Source Public Required .false.), s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_Set [] [(= (Var 2 a) (SetConstant [(IntegerConstant 1 (Integer 4 [])) (IntegerConstant 2 (Integer 4 [])) (IntegerConstant 3 (Integer 4 []))] (Set (Integer 4 []))) ()) (= (Var 2 a) (SetConstant [(IntegerConstant 2 (Integer 4 [])) (IntegerConstant 3 (Integer 4 [])) (IntegerConstant 4 (Integer 4 [])) (IntegerConstant 5 (Integer 4 [])) (IntegerConstant 5 (Integer 4 []))] (Set (Integer 4 []))) ()) (SetInsert 2 a (IntegerConstant 9 (Integer 4 []))) (SetRemove 2 a (IntegerConstant 4 (Integer 4 []))) (= (Var 2 b) (SetConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () [])) (StringConstant "c" (Character 1 1 () []))] (Set (Character 1 1 () []))) ()) (= (Var 2 s) (SetPop 2 b (Character 1 -2 () []) ()) ())] Source Public Implementation () .false. .false.)}) [])
13 changes: 13 additions & 0 deletions tests/reference/asr-test_set3-f9bbffb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_set3-f9bbffb",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_set3.py",
"infile_hash": "c4e83ae667bdaa458fd271a51e15d6c5f9a666f006e31de27a897571",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set3-f9bbffb.stderr",
"stderr_hash": "1ec6cfef0827e97a905c5927de43b74452777394b1f5110e94e5cf97",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_set3-f9bbffb.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: pop() takes no arguments (1 given)
--> tests/errors/test_set3.py:5:9
|
5 | i = s.pop(3)
| ^^^^^^^^
4 changes: 2 additions & 2 deletions tests/reference/ast-set1-ebd6ee0.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast-set1-ebd6ee0",
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
"infile": "tests/set1.py",
"infile_hash": "1fecd0d73e1cca64b609d716c1db4d8c647ac8e37850f5aaaf36df74",
"infile_hash": "63b5dc6a1f9c17099f1b10c8c45fcac1e50319ca8efbf7c283060abb",
"outfile": null,
"outfile_hash": null,
"stdout": "ast-set1-ebd6ee0.stdout",
"stdout_hash": "ca6e9c6754c51a463169f68fb874a92a1df0f28b6ea854e737ea3692",
"stdout_hash": "bc412431f1754c75ece480170de1a8433d23f985ce6875622f85a27e",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast-set1-ebd6ee0.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(FunctionDef test_Set ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Subscript (Name set Load) (Name i32 Load) Load) () 1) (Assign [(Name a Store)] (Set [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())]) ()) (Assign [(Name a Store)] (Set [(ConstantInt 2 ()) (ConstantInt 3 ()) (ConstantInt 4 ()) (ConstantInt 5 ()) (ConstantInt 5 ())]) ()) (Expr (Call (Attribute (Name a Load) add Load) [(ConstantInt 9 ())] [])) (Expr (Call (Attribute (Name a Load) remove Load) [(ConstantInt 4 ())] [])) (AnnAssign (Name b Store) (Subscript (Name set Load) (Name str Load) Load) () 1) (Assign [(Name b Store)] (Set [(ConstantStr "a" ()) (ConstantStr "b" ()) (ConstantStr "c" ())]) ())] [] () ())] [])
(Module [(FunctionDef test_Set ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Subscript (Name set Load) (Name i32 Load) Load) () 1) (Assign [(Name a Store)] (Set [(ConstantInt 1 ()) (ConstantInt 2 ()) (ConstantInt 3 ())]) ()) (Assign [(Name a Store)] (Set [(ConstantInt 2 ()) (ConstantInt 3 ()) (ConstantInt 4 ()) (ConstantInt 5 ()) (ConstantInt 5 ())]) ()) (Expr (Call (Attribute (Name a Load) add Load) [(ConstantInt 9 ())] [])) (Expr (Call (Attribute (Name a Load) remove Load) [(ConstantInt 4 ())] [])) (AnnAssign (Name b Store) (Subscript (Name set Load) (Name str Load) Load) () 1) (Assign [(Name b Store)] (Set [(ConstantStr "a" ()) (ConstantStr "b" ()) (ConstantStr "c" ())]) ()) (AnnAssign (Name s Store) (Name str Load) () 1) (Assign [(Name s Store)] (Call (Attribute (Name b Load) pop Load) [] []) ())] [] () ())] [])
3 changes: 3 additions & 0 deletions tests/set1.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ def test_Set():
b: set[str]
b = {"a", "b", "c"}
# a = {-1, -2, "c"} -> semantic error for now

s: str
s = b.pop()
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ asr = true
filename = "errors/test_set2.py"
asr = true

[[test]]
filename = "errors/test_set3.py"
asr = true

[[test]]
filename = "errors/test_pow.py"
asr = true
Expand Down

0 comments on commit bb41e2d

Please sign in to comment.