Skip to content

Commit 6ebd379

Browse files
committed
Evaluate intrinsic functions at compile time
1 parent 7480f14 commit 6ebd379

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2569,8 +2569,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
25692569
if(ASR::is_a<ASR::Function_t>(*s)) {
25702570
ASR::Function_t *func = ASR::down_cast<ASR::Function_t>(s);
25712571
ASR::ttype_t *a_type = ASRUtils::expr_type(func->m_return_var);
2572+
ASR::expr_t *value = nullptr;
2573+
if (ASRUtils::is_intrinsic_function2(func)) {
2574+
value = intrinsic_procedures.comptime_eval(call_name, al, x.base.base.loc, args);
2575+
}
25722576
tmp = ASR::make_FunctionCall_t(al, x.base.base.loc, stemp,
2573-
nullptr, args.p, args.size(), nullptr, 0, a_type, nullptr, nullptr);
2577+
nullptr, args.p, args.size(), nullptr, 0, a_type, value, nullptr);
25742578
} else if(ASR::is_a<ASR::Subroutine_t>(*s)) {
25752579
tmp = ASR::make_SubroutineCall_t(al, x.base.base.loc, stemp,
25762580
nullptr, args.p, args.size(), nullptr);

src/lpython/semantics/python_comptime_eval.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ struct PythonIntrinsicProcedures {
6060
comptime_eval_callback cb = std::get<1>(search->second);
6161
Vec<ASR::expr_t*> arg_values = ASRUtils::get_arg_values(al, args);
6262
if (arg_values.size() != args.size()) {
63-
throw SemanticError("Intrinsic function '" + name
64-
+ "' compile time evaluation expects different number of arguments",
65-
loc);
63+
// Not all arguments have compile time values; we do not call the callback
64+
return nullptr;
6665
}
6766
return cb(al, loc, arg_values);
6867
} else {

0 commit comments

Comments
 (0)