Skip to content

Commit

Permalink
Merge pull request lcompilers#91 from namannimmo10/set
Browse files Browse the repository at this point in the history
Implement Set
  • Loading branch information
namannimmo10 committed Feb 1, 2022
2 parents 670e186 + 3411459 commit d6d69bb
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions grammar/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ expr
| ConstantInteger(int n, ttype type)
| ConstantReal(float r, ttype type)
| ConstantComplex(float re, float im, ttype type)
| ConstantSet(expr* elements, ttype type)
| ConstantLogical(bool value, ttype type)
| ConstantString(string s, ttype type)
| ConstantDictionary(expr* keys, expr* values, ttype type)
Expand All @@ -231,6 +232,7 @@ ttype
| Complex(int kind, dimension* dims)
| Character(int kind, int len, expr? len_expr, dimension* dims)
| Logical(int kind, dimension* dims)
| Set(ttype type)
| Derived(symbol derived_type, dimension* dims)
| Class(symbol class_type, dimension* dims)
| Dict(ttype key_type, ttype value_type)
Expand Down
22 changes: 22 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,28 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = ASR::make_ErrorStop_t(al, x.base.base.loc, code);
}

void visit_Set(const AST::Set_t &x) {
LFORTRAN_ASSERT(x.n_elts > 0); // type({}) is 'dict'
Vec<ASR::expr_t*> elements;
elements.reserve(al, x.n_elts);
ASR::ttype_t* type = nullptr;
for (size_t i = 0; i < x.n_elts; ++i) {
visit_expr(*x.m_elts[i]);
ASR::expr_t *value = ASRUtils::EXPR(tmp);
if (type == nullptr) {
type = ASRUtils::expr_type(value);
} else {
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) {
throw SemanticError("All Set values must be of the same type for now",
x.base.base.loc);
}
}
elements.push_back(al, value);
}
ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type));
tmp = ASR::make_ConstantSet_t(al, x.base.base.loc, elements.p, elements.size(), set_type);
}

void visit_Expr(const AST::Expr_t &x) {
if (AST::is_a<AST::Call_t>(*x.m_value)) {
AST::Call_t *c = AST::down_cast<AST::Call_t>(x.m_value);
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/lpython_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def visit_Constant(self, node):
return python_ast.ConstantInt(node.value, node.kind)
elif isinstance(node.value, float):
return python_ast.ConstantFloat(node.value, node.kind)
elif isinstance(node.value, set):
return python_ast.Set(node.elts)
elif isinstance(node.value, complex):
return python_ast.ConstantComplex(node.value.real,
node.value.imag, node.kind)
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-set1-b7b913a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-set1-b7b913a",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/set1.py",
"infile_hash": "9daf37483fb6f1523007506236ce239db1f9c56a31c5e028bbecff0b",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-set1-b7b913a.stdout",
"stdout_hash": "f50412c1c431e3b37674040d42c8e48c101c73e4f72008b8955f7fe0",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/asr-set1-b7b913a.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(TranslationUnit (SymbolTable 1 {test_Set: (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.)}) test_Set [] [(= (Var 2 a) (ConstantSet [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 2 (Integer 4 [])) (ConstantInteger 3 (Integer 4 []))] (Set (Integer 4 []))) ()) (= (Var 2 a) (ConstantSet [(ConstantInteger 2 (Integer 4 [])) (ConstantInteger 3 (Integer 4 [])) (ConstantInteger 4 (Integer 4 [])) (ConstantInteger 5 (Integer 4 [])) (ConstantInteger 5 (Integer 4 []))] (Set (Integer 4 []))) ()) (= (Var 2 a) (ConstantSet [(ConstantString "a" (Character 1 1 () [])) (ConstantString "b" (Character 1 1 () [])) (ConstantString "c" (Character 1 1 () []))] (Set (Character 1 1 () []))) ()) (= (Var 2 b) (ArrayRef 2 a [(() (ConstantInteger 0 (Integer 4 [])) ())] (Integer 4 []) ()) ())] Source Public Implementation () .false. .false.)}) [])
13 changes: 13 additions & 0 deletions tests/reference/ast-set1-ebd6ee0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "ast-set1-ebd6ee0",
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
"infile": "tests/set1.py",
"infile_hash": "9daf37483fb6f1523007506236ce239db1f9c56a31c5e028bbecff0b",
"outfile": null,
"outfile_hash": null,
"stdout": "ast-set1-ebd6ee0.stdout",
"stdout_hash": "8c1cad9ac254b1847c30e26721e705d4db36fca6316e0f4e870f06a0",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
1 change: 1 addition & 0 deletions tests/reference/ast-set1-ebd6ee0.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Module [(FunctionDef test_Set ([] [] [] [] [] [] []) [(AnnAssign (Name a Store) (Name i32 Load) () 1) (AnnAssign (Name b Store) (Name i32 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 ())]) ()) (Assign [(Name a Store)] (Set [(ConstantStr "a" ()) (ConstantStr "b" ()) (ConstantStr "c" ())]) ()) (Assign [(Name b Store)] (Subscript (Name a Load) (ConstantInt 0 ()) Load) ())] [] () ())] [])
9 changes: 9 additions & 0 deletions tests/set1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def test_Set():
a: i32
b: i32

a = {1, 2, 3}
a = {2, 3, 4, 5, 5}
a = {"a", "b", "c"}
# a = {-1, -2, "c"} -> semantic error for now
b = a[0]
5 changes: 5 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ filename = "assign1.py"
ast = true
asr = true

[[test]]
filename = "set1.py"
ast = true
asr = true

[[test]]
filename = "global_scope1.py"
ast = true
Expand Down

0 comments on commit d6d69bb

Please sign in to comment.