Skip to content

Commit 4cce3e1

Browse files
committed
tmp
1 parent 8d679fa commit 4cce3e1

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

examples/expr2.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
def main0():
2-
x: i32
3-
x = (2+3)*5
4-
print(x)
5-
6-
main0()
7-
8-
# Not implemented yet in LPython:
9-
#if __name__ == "__main__":
10-
# main()
1+
def f(x: In[list[i32]]) -> None:
2+
x[5] = 5 # Assignment not allowed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,26 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
15811581
}
15821582
}
15831583

1584+
AST::expr_t* get_var_intent_and_annotation(AST::expr_t *annotation, ASR::intentType &intent) {
1585+
if (AST::is_a<AST::Subscript_t>(*annotation)) {
1586+
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(annotation);
1587+
if (AST::is_a<AST::Name_t>(*s->m_value)) {
1588+
std::string ann_name = AST::down_cast<AST::Name_t>(s->m_value)->m_id;
1589+
if (ann_name == "In") {
1590+
intent = ASRUtils::intent_in;
1591+
} else if (ann_name == "InOut") {
1592+
intent = ASRUtils::intent_inout;
1593+
} else if (ann_name == "Out") {
1594+
intent = ASRUtils::intent_out;
1595+
}
1596+
return s->m_slice;
1597+
} else {
1598+
throw SemanticError("Only Name in Subscript supported for now in annotation", annotation->base.loc);
1599+
}
1600+
}
1601+
return annotation;
1602+
}
1603+
15841604
// Convert Python AST type annotation to an ASR type
15851605
// Examples:
15861606
// i32, i64, f32, f64
@@ -3741,7 +3761,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
37413761
if (x.m_args.m_args[i].m_annotation == nullptr) {
37423762
throw SemanticError("Argument does not have a type", loc);
37433763
}
3744-
ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *x.m_args.m_args[i].m_annotation);
3764+
ASR::intentType s_intent = ASRUtils::intent_in;
3765+
AST::expr_t* arg_annotation_type = get_var_intent_and_annotation(x.m_args.m_args[i].m_annotation, s_intent);
3766+
ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *arg_annotation_type);
37453767
// Set the function as generic if an argument is typed with a type parameter
37463768
if (ASRUtils::is_generic(*arg_type)) {
37473769
ASR::ttype_t* arg_type_type = ASRUtils::get_type_parameter(arg_type);
@@ -3766,10 +3788,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
37663788
}
37673789

37683790
std::string arg_s = arg;
3769-
37703791
ASR::expr_t *value = nullptr;
37713792
ASR::expr_t *init_expr = nullptr;
3772-
ASR::intentType s_intent = ASRUtils::intent_in;
37733793
if (ASRUtils::is_array(arg_type)) {
37743794
s_intent = ASRUtils::intent_inout;
37753795
}

0 commit comments

Comments
 (0)