diff --git a/integration_tests/test_builtin_abs.py b/integration_tests/test_builtin_abs.py index 43379ff1c6..3c1e7b3a3f 100644 --- a/integration_tests/test_builtin_abs.py +++ b/integration_tests/test_builtin_abs.py @@ -6,6 +6,8 @@ def test_abs(): assert abs(x) == 5.5 x = -5.5 assert abs(x) == 5.5 + assert abs(5.5) == 5.5 + assert abs(-5.5) == 5.5 test_abs() diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 95293fe47e..6076ea77e8 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -368,6 +368,18 @@ static inline bool is_intrinsic_function(const ASR::Function_t *fn) { return false; } +// Returns true if the Function is intrinsic, otherwise false +// This version uses the `intrinsic` member of `Module`, so it +// should be used instead of is_intrinsic_function +static inline bool is_intrinsic_function2(const ASR::Function_t *fn) { + ASR::symbol_t *sym = (ASR::symbol_t*)fn; + ASR::Module_t *m = get_sym_module0(sym); + if (m != nullptr) { + if (m->m_intrinsic) return true; + } + return false; +} + // Returns true if all arguments have a `value` static inline bool all_args_have_value(const Vec &args) { for (auto &a : args) { diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 55f2c3bd32..49ccd201a5 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace LFortran::Python { @@ -147,11 +148,14 @@ class CommonVisitor : public AST::BaseVisitor { ASR::asr_t *tmp; Allocator &al; SymbolTable *current_scope; + // The current_module contains the current module that is being visited; + // this is used to append to the module dependencies if needed ASR::Module_t *current_module = nullptr; Vec current_module_dependencies; // True for the main module, false for every other one // The main module is stored directly in TranslationUnit, other modules are Modules bool main_module; + PythonIntrinsicProcedures intrinsic_procedures; CommonVisitor(Allocator &al, SymbolTable *symbol_table, diag::Diagnostics &diagnostics, bool main_module) @@ -178,6 +182,72 @@ class CommonVisitor : public AST::BaseVisitor { return ASR::make_Var_t(al, loc, v); } + ASR::symbol_t* resolve_intrinsic_function(const Location &loc, const std::string &remote_sym) { + LFORTRAN_ASSERT(intrinsic_procedures.is_intrinsic(remote_sym)) + std::string module_name = intrinsic_procedures.get_module(remote_sym, loc); + + SymbolTable *tu_symtab = ASRUtils::get_tu_symtab(current_scope); + std::string rl_path = get_runtime_library_dir(); + bool ltypes; + ASR::Module_t *m = load_module(al, tu_symtab, module_name, + loc, true, rl_path, + ltypes, + [&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); } + ); + LFORTRAN_ASSERT(!ltypes) + + ASR::symbol_t *t = m->m_symtab->resolve_symbol(remote_sym); + if (!t) { + throw SemanticError("The symbol '" + remote_sym + + "' not found in the module '" + module_name + "'", + loc); + } else if (! (ASR::is_a(*t) + || ASR::is_a(*t) + || ASR::is_a(*t))) { + throw SemanticError("The symbol '" + remote_sym + + "' found in the module '" + module_name + "', " + + "but it is not a function, subroutine or a generic procedure.", + loc); + } + char *fn_name = ASRUtils::symbol_name(t); + ASR::asr_t *fn = ASR::make_ExternalSymbol_t( + al, t->base.loc, + /* a_symtab */ current_scope, + /* a_name */ fn_name, + t, + m->m_name, nullptr, 0, fn_name, + ASR::accessType::Private + ); + std::string sym = fn_name; + + current_scope->scope[sym] = ASR::down_cast(fn); + ASR::symbol_t *v = ASR::down_cast(fn); + + // Now we need to add the module `m` with the intrinsic function + // into the current module dependencies + if (current_module) { + // We are in body visitor, the module is already constructed + // and available as current_module. + // Add the module `m` to current module dependencies + Vec vec; + vec.from_pointer_n_copy(al, current_module->m_dependencies, + current_module->n_dependencies); + if (!present(vec, m->m_name)) { + vec.push_back(al, m->m_name); + current_module->m_dependencies = vec.p; + current_module->n_dependencies = vec.size(); + } + } else { + // We are in the symtab visitor or body visitor and we are + // constructing a module, so current_module is not available yet + // (the current_module_dependencies is not used in body visitor) + if (!present(current_module_dependencies, m->m_name)) { + current_module_dependencies.push_back(al, m->m_name); + } + } + return v; + } + // Convert Python AST type annotation to an ASR type // Examples: // i32, i64, f32, f64 @@ -1981,6 +2051,18 @@ class BodyVisitor : public CommonVisitor { if (!s) { + if (intrinsic_procedures.is_intrinsic(call_name)) { + s = resolve_intrinsic_function(x.base.base.loc, call_name); + } else { + // TODO: We need to port all functions below to the intrinsic functions file + // Then we can uncomment this error message: + /* + throw SemanticError("The function '" + call_name + "' is not declared and not intrinsic", + x.base.base.loc); + } + if (false) { + */ + // This will all be removed once we port it to intrinsic functions // Intrinsic functions if (call_name == "size") { // TODO: size should be part of ASR. That way @@ -2481,12 +2563,9 @@ class BodyVisitor : public CommonVisitor { throw SemanticError("Function '" + call_name + "' is not declared and not intrinsic", x.base.base.loc); } + } // end of "comment" } - if (!s) { - throw SemanticError("Function '" + call_name + "' is not declared", - x.base.base.loc); - } // handling ExternalSymbol ASR::symbol_t *stemp = s; s = ASRUtils::symbol_get_past_external(s); @@ -2494,8 +2573,12 @@ class BodyVisitor : public CommonVisitor { if(ASR::is_a(*s)) { ASR::Function_t *func = ASR::down_cast(s); ASR::ttype_t *a_type = ASRUtils::expr_type(func->m_return_var); + ASR::expr_t *value = nullptr; + if (ASRUtils::is_intrinsic_function2(func)) { + value = intrinsic_procedures.comptime_eval(call_name, al, x.base.base.loc, args); + } tmp = ASR::make_FunctionCall_t(al, x.base.base.loc, stemp, - nullptr, args.p, args.size(), nullptr, 0, a_type, nullptr, nullptr); + nullptr, args.p, args.size(), nullptr, 0, a_type, value, nullptr); } else if(ASR::is_a(*s)) { tmp = ASR::make_SubroutineCall_t(al, x.base.base.loc, stemp, nullptr, args.p, args.size(), nullptr); diff --git a/src/lpython/semantics/python_comptime_eval.h b/src/lpython/semantics/python_comptime_eval.h new file mode 100644 index 0000000000..7a10f24981 --- /dev/null +++ b/src/lpython/semantics/python_comptime_eval.h @@ -0,0 +1,107 @@ +#ifndef LPYTHON_SEMANTICS_COMPTIME_EVAL_H +#define LPYTHON_SEMANTICS_COMPTIME_EVAL_H + +#include + +#include +#include +#include +#include +#include +#include + +namespace LFortran { + +struct PythonIntrinsicProcedures { + + const std::string m_builtin = "lpython_builtin"; + + typedef ASR::expr_t* (*comptime_eval_callback)(Allocator &, const Location &, Vec &); + // Table of intrinsics + // The callback is only called if all arguments have compile time `value` + // which is always one of the `Constant*` expression ASR nodes, so inside + // the callback one can assume that. + std::map> comptime_eval_map; + + PythonIntrinsicProcedures() { + comptime_eval_map = { + {"abs", {m_builtin, &eval_abs}}, + }; + } + + // Return `true` if `name` is in the table of intrinsics + bool is_intrinsic(std::string name) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + return true; + } else { + return false; + } + } + + // Looks up `name` in the table of intrinsics and returns the corresponding + // module name; Otherwise rises an exception + std::string get_module(std::string name, const Location &loc) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + std::string module_name = std::get<0>(search->second); + return module_name; + } else { + throw SemanticError("Function '" + name + + "' not found among intrinsic procedures", + loc); + } + } + + // Evaluates the intrinsic function `name` at compile time + ASR::expr_t *comptime_eval(std::string name, Allocator &al, const Location &loc, Vec &args) const { + auto search = comptime_eval_map.find(name); + if (search != comptime_eval_map.end()) { + comptime_eval_callback cb = std::get<1>(search->second); + Vec arg_values = ASRUtils::get_arg_values(al, args); + if (arg_values.size() != args.size()) { + // Not all arguments have compile time values; we do not call the callback + return nullptr; + } + return cb(al, loc, arg_values); + } else { + throw SemanticError("Intrinsic function '" + name + + "' compile time evaluation is not implemented yet", + loc); + } + } + + + static ASR::expr_t *eval_abs(Allocator &al, const Location &loc, + Vec &args + ) { + LFORTRAN_ASSERT(ASRUtils::all_args_evaluated(args)); + if (args.size() != 1) { + throw SemanticError("Intrinsic abs function accepts exactly 1 argument", loc); + } + ASR::expr_t* trig_arg = args[0]; + ASR::ttype_t* t = ASRUtils::expr_type(args[0]); + if (ASR::is_a(*t)) { + double rv = ASR::down_cast(trig_arg)->m_r; + double val = std::abs(rv); + return ASR::down_cast(ASR::make_ConstantReal_t(al, loc, val, t)); + } else if (ASR::is_a(*t)) { + int64_t rv = ASR::down_cast(trig_arg)->m_n; + int64_t val = std::abs(rv); + return ASR::down_cast(ASR::make_ConstantInteger_t(al, loc, val, t)); + } else if (ASR::is_a(*t)) { + double re = ASR::down_cast(trig_arg)->m_re; + double im = ASR::down_cast(trig_arg)->m_im; + std::complex x(re, im); + double result = std::abs(x); + return ASR::down_cast(ASR::make_ConstantReal_t(al, loc, result, t)); + } else { + throw SemanticError("Argument of the abs function must be Integer, Real or Complex", loc); + } + } + +}; // ComptimeEval + +} // namespace LFortran + +#endif /* LPYTHON_SEMANTICS_COMPTIME_EVAL_H */ diff --git a/tests/reference/asr-constants1-5828e8a.json b/tests/reference/asr-constants1-5828e8a.json index 1a4c4ea745..8eb9f87993 100644 --- a/tests/reference/asr-constants1-5828e8a.json +++ b/tests/reference/asr-constants1-5828e8a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-constants1-5828e8a.stdout", - "stdout_hash": "82599c6a0114feef56f5e9198dd5cfbd06d87638efc14126af92a89e", + "stdout_hash": "0a0d5acbd9b14c908f856d9d95d36e49e4cb2f5db735109a008af200", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-constants1-5828e8a.stdout b/tests/reference/asr-constants1-5828e8a.stdout index d53f6e2e4a..f9450621d6 100644 --- a/tests/reference/asr-constants1-5828e8a.stdout +++ b/tests/reference/asr-constants1-5828e8a.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 17 {}) main_program [] []), test_abs: (Subroutine (SymbolTable 4 {abs: (ExternalSymbol 4 abs 13 abs lpython_builtin [] abs Public), b: (Variable 4 b Local () () Default (Real 4 []) Source Public Required .false.)}) test_abs [] [(= (Var 4 b) (FunctionCall 4 abs () [(ConstantReal 3.450000 (Real 8 []))] [] (Real 8 []) () ()) ()) (= (Var 4 b) (FunctionCall 4 abs () [(UnaryOp USub (ConstantReal 5346.340000 (Real 8 [])) (Real 8 []) (ConstantReal -5346.340000 (Real 8 [])))] [] (Real 8 []) () ()) ())] Source Public Implementation () .false. .false.), test_bool: (Subroutine (SymbolTable 6 {a: (Variable 6 a Local () () Default (Logical 1 []) Source Public Required .false.)}) test_bool [] [(= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_boz: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_boz [] [(= (Var 2 b) (ConstantString "0b101" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0b1000000" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0b1000010110" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o10" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o70" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0o1026" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0x2a" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0xc0ffee" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0x216" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_callable: (Subroutine (SymbolTable 8 {a: (Variable 8 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 8 b Local () () Default (Integer 4 []) Source Public Required .false.)}) test_callable [] [(= (Var 8 b) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 8 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_divmod: (Subroutine (SymbolTable 11 {a: (Variable 11 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_divmod [] [(= (Var 11 a) (ConstantTuple [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger -3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ())] Source Public Implementation () .false. .false.), test_float: (Subroutine (SymbolTable 10 {a: (Variable 10 a Local () () Default (Real 8 []) Source Public Required .false.)}) test_float [] [(= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 4.560000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal -1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5346.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 423.533997 (Real 8 [])) ())] Source Public Implementation () .false. .false.), test_int: (Subroutine (SymbolTable 9 {a: (Variable 9 a Local () () Default (Integer 8 []) Source Public Required .false.)}) test_int [] [(= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 4 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger -5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 1 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5346 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ())] Source Public Implementation () .false. .false.), test_len: (Subroutine (SymbolTable 5 {a: (Variable 5 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_len [] [(= (Var 5 a) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 4 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 14 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_ord_chr: (Subroutine (SymbolTable 3 {a: (Variable 3 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 3 ord 13 ord lpython_builtin [] ord Public), s: (Variable 3 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_ord_chr [] [(= (Var 3 a) (FunctionCall 3 ord () [(ConstantString "5" (Character 1 1 () []))] [] (Integer 4 []) () ()) ()) (= (Var 3 s) (ConstantString "+" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_str: (Subroutine (SymbolTable 7 {s: (Variable 7 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_str [] [(= (Var 7 s) (ConstantString "" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "-4" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5.600000" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "True" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "False" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5346" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 17 {}) main_program [] []), test_abs: (Subroutine (SymbolTable 4 {abs: (ExternalSymbol 4 abs 13 abs lpython_builtin [] abs Private), b: (Variable 4 b Local () () Default (Real 4 []) Source Public Required .false.)}) test_abs [] [(= (Var 4 b) (FunctionCall 4 abs () [(ConstantReal 3.450000 (Real 8 []))] [] (Real 8 []) (ConstantReal 3.450000 (Real 8 [])) ()) ()) (= (Var 4 b) (FunctionCall 4 abs () [(UnaryOp USub (ConstantReal 5346.340000 (Real 8 [])) (Real 8 []) (ConstantReal -5346.340000 (Real 8 [])))] [] (Real 8 []) (ConstantReal 5346.340000 (Real 8 [])) ()) ())] Source Public Implementation () .false. .false.), test_bool: (Subroutine (SymbolTable 6 {a: (Variable 6 a Local () () Default (Logical 1 []) Source Public Required .false.)}) test_bool [] [(= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (= (Var 6 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 6 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_boz: (Subroutine (SymbolTable 2 {b: (Variable 2 b Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_boz [] [(= (Var 2 b) (ConstantString "0b101" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0b1000000" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0b1000010110" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o10" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0o70" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0o1026" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0x2a" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "0xc0ffee" (Character 1 1 () [])) ()) (= (Var 2 b) (ConstantString "-0x216" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_callable: (Subroutine (SymbolTable 8 {a: (Variable 8 a Local () () Default (Logical 1 []) Source Public Required .false.), b: (Variable 8 b Local () () Default (Integer 4 []) Source Public Required .false.)}) test_callable [] [(= (Var 8 b) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 8 a) (ConstantLogical .true. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .true. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ()) (= (Var 8 a) (ConstantLogical .false. (Logical 1 [])) ()) (Assert (Compare (Var 8 a) Eq (ConstantLogical .false. (Logical 1 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.), test_divmod: (Subroutine (SymbolTable 11 {a: (Variable 11 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_divmod [] [(= (Var 11 a) (ConstantTuple [(ConstantInteger 3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger -3 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 1 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 4 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ()) (= (Var 11 a) (ConstantTuple [(ConstantInteger 0 (Integer 4 [])) (ConstantInteger 0 (Integer 4 []))] (Tuple [(Integer 4 []) (Integer 4 [])])) ())] Source Public Implementation () .false. .false.), test_float: (Subroutine (SymbolTable 10 {a: (Variable 10 a Local () () Default (Real 8 []) Source Public Required .false.)}) test_float [] [(= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 4.560000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal -1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 1.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 0.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 5346.000000 (Real 8 [])) ()) (= (Var 10 a) (ConstantReal 423.533997 (Real 8 [])) ())] Source Public Implementation () .false. .false.), test_int: (Subroutine (SymbolTable 9 {a: (Variable 9 a Local () () Default (Integer 8 []) Source Public Required .false.)}) test_int [] [(= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 4 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger -5 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 1 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 0 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ()) (= (Var 9 a) (ImplicitCast (ConstantInteger 5346 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) ())] Source Public Implementation () .false. .false.), test_len: (Subroutine (SymbolTable 5 {a: (Variable 5 a Local () () Default (Integer 4 []) Source Public Required .false.)}) test_len [] [(= (Var 5 a) (ConstantInteger 0 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 4 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 14 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 2 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ()) (= (Var 5 a) (ConstantInteger 3 (Integer 4 [])) ())] Source Public Implementation () .false. .false.), test_ord_chr: (Subroutine (SymbolTable 3 {a: (Variable 3 a Local () () Default (Integer 4 []) Source Public Required .false.), ord: (ExternalSymbol 3 ord 13 ord lpython_builtin [] ord Public), s: (Variable 3 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_ord_chr [] [(= (Var 3 a) (FunctionCall 3 ord () [(ConstantString "5" (Character 1 1 () []))] [] (Integer 4 []) () ()) ()) (= (Var 3 s) (ConstantString "+" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.), test_str: (Subroutine (SymbolTable 7 {s: (Variable 7 s Local () () Default (Character 1 -2 () []) Source Public Required .false.)}) test_str [] [(= (Var 7 s) (ConstantString "" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "-4" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5.600000" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "True" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "False" (Character 1 1 () [])) ()) (= (Var 7 s) (ConstantString "5346" (Character 1 1 () [])) ())] Source Public Implementation () .false. .false.)}) []) diff --git a/tests/reference/asr-test_builtin_abs-c74d2c9.json b/tests/reference/asr-test_builtin_abs-c74d2c9.json index f8d50d9419..ce80265555 100644 --- a/tests/reference/asr-test_builtin_abs-c74d2c9.json +++ b/tests/reference/asr-test_builtin_abs-c74d2c9.json @@ -2,11 +2,11 @@ "basename": "asr-test_builtin_abs-c74d2c9", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/../integration_tests/test_builtin_abs.py", - "infile_hash": "6469a8f5b27cd216944f1dcde21c25cce6aa28435399b0b862cead11", + "infile_hash": "196e1cd7d641336a4bb317f947ec4e3369a0e62a4da29af1a9c20946", "outfile": null, "outfile_hash": null, "stdout": "asr-test_builtin_abs-c74d2c9.stdout", - "stdout_hash": "7bd2db29560606cab8ff10b4a418637743bc3e8083dead814e8f9c0a", + "stdout_hash": "db838618bb7ad2900d537a296ac699b1fcd846d088528fb22fba502a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-test_builtin_abs-c74d2c9.stdout b/tests/reference/asr-test_builtin_abs-c74d2c9.stdout index d6c082242c..57091561ff 100644 --- a/tests/reference/asr-test_builtin_abs-c74d2c9.stdout +++ b/tests/reference/asr-test_builtin_abs-c74d2c9.stdout @@ -1 +1 @@ -(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 9 {}) _lfortran_main_program [] [(SubroutineCall 1 test_abs () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 8 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_abs: (Subroutine (SymbolTable 2 {abs: (ExternalSymbol 2 abs 4 abs lpython_builtin [] abs Public), x: (Variable 2 x Local () () Default (Real 8 []) Source Public Required .false.)}) test_abs [] [(= (Var 2 x) (ConstantReal 5.500000 (Real 8 [])) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (= (Var 2 x) (UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 []))) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ())] Source Public Implementation () .false. .false.)}) []) +(TranslationUnit (SymbolTable 1 {_lfortran_main_program: (Subroutine (SymbolTable 9 {}) _lfortran_main_program [] [(SubroutineCall 1 test_abs () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 8 {}) main_program [] [(SubroutineCall 1 _lfortran_main_program () [] ())]), test_abs: (Subroutine (SymbolTable 2 {abs: (ExternalSymbol 2 abs 4 abs lpython_builtin [] abs Private), x: (Variable 2 x Local () () Default (Real 8 []) Source Public Required .false.)}) test_abs [] [(= (Var 2 x) (ConstantReal 5.500000 (Real 8 [])) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (= (Var 2 x) (UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 []))) ()) (Assert (Compare (FunctionCall 2 abs () [(Var 2 x)] [] (Real 8 []) () ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) () ()) ()) (Assert (Compare (FunctionCall 2 abs () [(ConstantReal 5.500000 (Real 8 []))] [] (Real 8 []) (ConstantReal 5.500000 (Real 8 [])) ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ()) (Assert (Compare (FunctionCall 2 abs () [(UnaryOp USub (ConstantReal 5.500000 (Real 8 [])) (Real 8 []) (ConstantReal -5.500000 (Real 8 [])))] [] (Real 8 []) (ConstantReal 5.500000 (Real 8 [])) ()) Eq (ConstantReal 5.500000 (Real 8 [])) (Logical 4 []) (ConstantLogical .true. (Logical 4 [])) ()) ())] Source Public Implementation () .false. .false.)}) [])