From 4620453f8c71b000d891b51f8b450ac8b23b69da Mon Sep 17 00:00:00 2001 From: Thirumalai-Shaktivel Date: Mon, 20 Mar 2023 11:28:13 +0530 Subject: [PATCH] Initial implementation of Symbolic type --- examples/expr2.py | 10 +++------- src/libasr/ASR.asdl | 3 +++ src/libasr/asr_utils.h | 11 +++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 8 ++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/examples/expr2.py b/examples/expr2.py index 2e66f1e584..6a104b48f5 100644 --- a/examples/expr2.py +++ b/examples/expr2.py @@ -1,10 +1,6 @@ -def main0(): - x: i32 - x = (2+3)*5 +def main(): + x: symbolic = Symbolic('x') print(x) -main0() +main() -# Not implemented yet in LPython: -#if __name__ == "__main__": -# main() diff --git a/src/libasr/ASR.asdl b/src/libasr/ASR.asdl index 5139f99481..281d2f9f9d 100644 --- a/src/libasr/ASR.asdl +++ b/src/libasr/ASR.asdl @@ -321,6 +321,8 @@ expr | IntrinsicFunctionSqrt(expr arg, ttype type, expr? value) + | SymbolicCall(expr arg, ttype type, expr? value) + -- `len` in Character: -- >=0 ... the length of the string, known at compile time @@ -359,6 +361,7 @@ ttype | Const(ttype type) | CPtr() | TypeParameter(identifier param, dimension* dims) + | Symbolic() | FunctionType(ttype* arg_types, ttype? return_var_type, abi abi, deftype deftype, string? bindc_name, bool elemental, bool pure, bool module, bool inline, bool static, ttype* type_params, diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 9b381c60a8..cff07739fa 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -1124,6 +1124,9 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t, ASR::TypeParameter_t *p = ASR::down_cast(t); return p->m_param; } + case ASR::ttypeType::Symbolic: { + return "Symbolic"; + } default : throw LCompilersException("Not implemented " + std::to_string(t->type)); } } @@ -1503,6 +1506,11 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x, m_dims = nullptr; break; } + case ASR::ttypeType::Symbolic: { + n_dims = 0; + m_dims = nullptr; + break; + } case ASR::ttypeType::TypeParameter: { ASR::TypeParameter_t* tp = ASR::down_cast(x); n_dims = tp->n_dims; @@ -2041,6 +2049,9 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b, case ASR::ttypeType::CPtr: { return true; } + case ASR::ttypeType::Symbolic: { + return true; + } case (ASR::ttypeType::Real) : { ASR::Real_t *a2 = ASR::down_cast(a); ASR::Real_t *b2 = ASR::down_cast(b); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index f71b9341b0..66c7b9b08c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -916,6 +916,8 @@ class CommonVisitor : public AST::BaseVisitor { 4, dims.p, dims.size())); } else if (var_annotation == "CPtr") { type = ASRUtils::TYPE(ASR::make_CPtr_t(al, loc)); + } else if (var_annotation == "symbolic") { + type = ASRUtils::TYPE(ASR::make_Symbolic_t(al, loc)); } else if (var_annotation == "pointer") { LCOMPILERS_ASSERT(n_args == 1); AST::expr_t* underlying_type = m_args[0]; @@ -6175,6 +6177,12 @@ class BodyVisitor : public CommonVisitor { tmp = intrinsic_node_handler.get_intrinsic_node(call_name, al, x.base.base.loc, args); return; + } else if (call_name == "Symbolic") { + LCOMPILERS_ASSERT(args.size() == 1) + tmp = ASR::make_SymbolicCall_t(al, x.base.base.loc, + args[0].m_value,ASRUtils::TYPE(ASR::make_Symbolic_t( + al, x.base.base.loc)), nullptr); + return ; } else { // The function was not found and it is not intrinsic throw SemanticError("Function '" + call_name + "' is not declared and not intrinsic",