@@ -31,7 +31,7 @@ void pass_wrap_global_stmts(Allocator &al,
3131 char *fn_name = s.c_str (al);
3232 SymbolTable *fn_scope = al.make_new <SymbolTable>(unit.m_symtab );
3333
34- ASR::ttype_t *type;
34+ ASR::ttype_t *type = nullptr ;
3535 Location loc = unit.base .base .loc ;
3636 ASR::asr_t *return_var=nullptr ;
3737 ASR::expr_t *return_var_ref=nullptr ;
@@ -45,58 +45,14 @@ void pass_wrap_global_stmts(Allocator &al,
4545 ASR::expr_t *target;
4646 ASR::expr_t *value = EXPR (unit.m_items [i]);
4747 // Create a new variable with the right type
48- if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Integer) {
49- s.from_str (al, fn_name_s + std::to_string (idx));
50- var_name = s.c_str (al);
51-
52- int a_kind = down_cast<ASR::Integer_t>(ASRUtils::expr_type (value))->m_kind ;
53-
54- type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc, a_kind));
55- return_var = ASR::make_Variable_t (al, loc,
56- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
57- ASR::storage_typeType::Default, type,
58- nullptr , ASR::abiType::BindC,
59- ASR::Public, ASR::presenceType::Required, false );
60- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
61- down_cast<ASR::symbol_t >(return_var)));
62- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
63- target = return_var_ref;
64- idx++;
65- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::UnsignedInteger) {
66- s.from_str (al, fn_name_s + std::to_string (idx));
67- var_name = s.c_str (al);
68-
69- int a_kind = down_cast<ASR::UnsignedInteger_t>(ASRUtils::expr_type (value))->m_kind ;
70-
71- type = ASRUtils::TYPE (ASR::make_UnsignedInteger_t (al, loc, a_kind));
72- return_var = ASR::make_Variable_t (al, loc,
73- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
74- ASR::storage_typeType::Default, type,
75- nullptr , ASR::abiType::BindC,
76- ASR::Public, ASR::presenceType::Required, false );
77- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
78- down_cast<ASR::symbol_t >(return_var)));
79- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
80- target = return_var_ref;
81- idx++;
82- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Logical) {
83- s.from_str (al, fn_name_s + std::to_string (idx));
84- var_name = s.c_str (al);
85-
86- int a_kind = down_cast<ASR::Logical_t>(ASRUtils::expr_type (value))->m_kind ;
87-
88- type = ASRUtils::TYPE (ASR::make_Logical_t (al, loc, a_kind));
89- return_var = ASR::make_Variable_t (al, loc,
90- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
91- ASR::storage_typeType::Default, type,
92- nullptr , ASR::abiType::BindC,
93- ASR::Public, ASR::presenceType::Required, false );
94- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
95- down_cast<ASR::symbol_t >(return_var)));
96- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
97- target = return_var_ref;
98- idx++;
99- } else if (ASRUtils::expr_type (value)->type == ASR::ttypeType::Real) {
48+ if ((ASRUtils::expr_type (value)->type == ASR::ttypeType::Integer) ||
49+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::UnsignedInteger) ||
50+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Logical) ||
51+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Real) ||
52+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) ||
53+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Character) ||
54+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::List) ||
55+ (ASRUtils::expr_type (value)->type == ASR::ttypeType::Tuple)) {
10056 s.from_str (al, fn_name_s + std::to_string (idx));
10157 var_name = s.c_str (al);
10258 type = ASRUtils::expr_type (value);
@@ -110,22 +66,7 @@ void pass_wrap_global_stmts(Allocator &al,
11066 fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
11167 target = return_var_ref;
11268 idx++;
113- } else if ((ASRUtils::expr_type (value)->type == ASR::ttypeType::Complex) ||
114- (ASRUtils::expr_type (value)->type == ASR::ttypeType::Character)) {
115- s.from_str (al, fn_name_s + std::to_string (idx));
116- var_name = s.c_str (al);
117- type = ASRUtils::expr_type (value);
118- return_var = ASR::make_Variable_t (al, loc,
119- fn_scope, var_name, nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
120- ASR::storage_typeType::Default, type,
121- nullptr , ASR::abiType::BindC,
122- ASR::Public, ASR::presenceType::Required, false );
123- return_var_ref = EXPR (ASR::make_Var_t (al, loc,
124- down_cast<ASR::symbol_t >(return_var)));
125- fn_scope->add_symbol (std::string (var_name), down_cast<ASR::symbol_t >(return_var));
126- target = return_var_ref;
127- idx++;
128- } else {
69+ } else {
12970 throw LCompilersException (" Return type not supported in interactive mode" );
13071 }
13172 ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, target, value, nullptr ));
@@ -141,7 +82,26 @@ void pass_wrap_global_stmts(Allocator &al,
14182
14283 if (return_var) {
14384 // The last defined `return_var` is the actual return value
85+ LCOMPILERS_ASSERT (type)
86+ LCOMPILERS_ASSERT (return_var_ref)
87+
14488 ASR::down_cast2<ASR::Variable_t>(return_var)->m_intent = ASRUtils::intent_return_var;
89+ std::string global_underscore_name = " _" + fn_name_s;
90+ s.from_str (al, global_underscore_name);
91+
92+ ASR::asr_t *global_underscore = ASR::make_Variable_t (al, loc,
93+ unit.m_symtab , s.c_str (al), nullptr , 0 , ASRUtils::intent_local, nullptr , nullptr ,
94+ ASR::storage_typeType::Default, type,
95+ nullptr , ASR::abiType::Source,
96+ ASR::Public, ASR::presenceType::Required, false );
97+ ASR::expr_t * global_underscore_ref = EXPR (ASR::make_Var_t (al, loc, down_cast<ASR::symbol_t >(global_underscore)));
98+
99+ if (fn_scope->parent ->get_symbol (global_underscore_name) != nullptr ) {
100+ throw LCompilersException (" Global variable already defined" );
101+ }
102+ unit.m_symtab ->add_symbol (global_underscore_name, down_cast<ASR::symbol_t >(global_underscore));
103+ ASR::stmt_t * asr_stmt = ASRUtils::STMT (ASR::make_Assignment_t (al, loc, global_underscore_ref, return_var_ref, nullptr ));
104+ body.push_back (al, asr_stmt);
145105 }
146106
147107 ASR::asr_t *fn = ASRUtils::make_Function_t_util (
0 commit comments