diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index 020ac4ee24..dccd0f8ecb 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -59,7 +59,7 @@ class ASRToCVisitor : public BaseCCPPVisitor public: ASRToCVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag, - false, false) {} + false, false, true) {} std::string convert_variable_decl(const ASR::Variable_t &v) { @@ -144,6 +144,7 @@ R"(#include #include #include #include +#include #include diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index 621b5492a5..0a5eb0c170 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -77,10 +77,12 @@ class BaseCCPPVisitor : public ASR::BaseVisitor bool gen_stdstring; // Use std::complex or float/double complex bool gen_stdcomplex; + bool is_c; BaseCCPPVisitor(diag::Diagnostics &diag, - bool gen_stdstring, bool gen_stdcomplex) : diag{diag}, - gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex} {} + bool gen_stdstring, bool gen_stdcomplex, bool is_c) : diag{diag}, + gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex}, + is_c{is_c} {} void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { // All loose statements must be converted to a function, so the items @@ -674,7 +676,8 @@ R"(#include case (ASR::binopType::Mul) : { last_expr_precedence = 5; break; } case (ASR::binopType::Div) : { last_expr_precedence = 5; break; } case (ASR::binopType::Pow) : { - src = "std::pow(" + left + ", " + right + ")"; + src = "pow(" + left + ", " + right + ")"; + if (!is_c) src = "std::" + src; return; } default: throw CodeGenError("BinOp: operator not implemented yet"); diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 1c4a26af2c..be4fcaee46 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -65,7 +65,7 @@ class ASRToCPPVisitor : public BaseCCPPVisitor { public: ASRToCPPVisitor(diag::Diagnostics &diag) : BaseCCPPVisitor(diag, - true, true) {} + true, true, false) {} std::string convert_variable_decl(const ASR::Variable_t &v) { diff --git a/tests/reference/c-c_interop1-e215531.json b/tests/reference/c-c_interop1-e215531.json index 7f91ab618d..92f4aea40b 100644 --- a/tests/reference/c-c_interop1-e215531.json +++ b/tests/reference/c-c_interop1-e215531.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "c-c_interop1-e215531.stdout", - "stdout_hash": "626931366920a203164bca08821af09d4e3401d5e62fc65cab4d9e76", + "stdout_hash": "ac7de87134ed10075c53ad60385310b20178165421d49aa124020905", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/c-c_interop1-e215531.stdout b/tests/reference/c-c_interop1-e215531.stdout index 7ff6cddd25..85caf71908 100644 --- a/tests/reference/c-c_interop1-e215531.stdout +++ b/tests/reference/c-c_interop1-e215531.stdout @@ -2,6 +2,7 @@ #include #include #include +#include #include diff --git a/tests/reference/c-expr7-bb2692a.json b/tests/reference/c-expr7-bb2692a.json new file mode 100644 index 0000000000..a949c3f0b2 --- /dev/null +++ b/tests/reference/c-expr7-bb2692a.json @@ -0,0 +1,13 @@ +{ + "basename": "c-expr7-bb2692a", + "cmd": "lpython --no-color --show-c {infile}", + "infile": "tests/expr7.py", + "infile_hash": "4a455e2279eb7dd269d81c1a257dea625b17b100d92e304e5ac34421", + "outfile": null, + "outfile_hash": null, + "stdout": "c-expr7-bb2692a.stdout", + "stdout_hash": "1300b234a8ba0b61ca9b134d106f05f2d23538aa3b7bcd4c308e6e47", + "stderr": "c-expr7-bb2692a.stderr", + "stderr_hash": "28509dd59a386eebd632340a550d14299cd2a921ef6dc3ac7dbe7fe9", + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/c-expr7-bb2692a.stderr b/tests/reference/c-expr7-bb2692a.stderr new file mode 100644 index 0000000000..7d800cf4e4 --- /dev/null +++ b/tests/reference/c-expr7-bb2692a.stderr @@ -0,0 +1,11 @@ +style suggestion: Could have used '**' instead of 'pow' + --> tests/expr7.py:3:9 + | +3 | a = pow(2, 2) + | ^^^^^^^^^ '**' could be used instead + +style suggestion: Could have used '**' instead of 'pow' + --> tests/expr7.py:7:11 + | +7 | res = pow(a, b) + | ^^^^^^^^^ '**' could be used instead diff --git a/tests/reference/c-expr7-bb2692a.stdout b/tests/reference/c-expr7-bb2692a.stdout new file mode 100644 index 0000000000..cc21a272bd --- /dev/null +++ b/tests/reference/c-expr7-bb2692a.stdout @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +#include + +void _lpython_main_program() +{ + main0(); +} + +void main0() +{ + int32_t c; + test_pow(); + c = test_pow_1(1, 2); +} + +void test_pow() +{ + int32_t a; + a = __lpython_overloaded_0__pow(2, 2); +} + +int test_pow_1(int32_t a, int32_t b) +{ + int32_t _lpython_return_variable; + int32_t res; + res = __lpython_overloaded_0__pow(a, b); + _lpython_return_variable = res; + return _lpython_return_variable; +} + +int __lpython_overloaded_0__pow(int32_t x, int32_t y) +{ + int32_t _lpython_return_variable; + _lpython_return_variable = pow(x, y); + return _lpython_return_variable; +} + +float _lfortran_caimag(float complex x); + +double _lfortran_zaimag(double complex x); + +int main(int argc, char* argv[]) +{ + _lpython_main_program(); + return 0; +} diff --git a/tests/reference/c-loop1-3e341c7.json b/tests/reference/c-loop1-3e341c7.json index 34f0579a73..608174777d 100644 --- a/tests/reference/c-loop1-3e341c7.json +++ b/tests/reference/c-loop1-3e341c7.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "c-loop1-3e341c7.stdout", - "stdout_hash": "5c8eae32cbe4df80d617ce6e6c3141dce742fede8793f22fcd48df3a", + "stdout_hash": "a0890fe0052d3a3fe6e74eb71fa408a8883b1bd20d03f8bca0eac59d", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/c-loop1-3e341c7.stdout b/tests/reference/c-loop1-3e341c7.stdout index c92a6b4a4e..fccf3ddfc6 100644 --- a/tests/reference/c-loop1-3e341c7.stdout +++ b/tests/reference/c-loop1-3e341c7.stdout @@ -2,6 +2,7 @@ #include #include #include +#include #include diff --git a/tests/tests.toml b/tests/tests.toml index 6b5a4f009e..2778e337fa 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -86,6 +86,7 @@ filename = "expr7.py" ast = true asr = true cpp = true +c = true [[test]] filename = "expr8.py"