diff --git a/include/xo/expression/Lambda.hpp b/include/xo/expression/Lambda.hpp index 7f6434f8..7f5ebab4 100644 --- a/include/xo/expression/Lambda.hpp +++ b/include/xo/expression/Lambda.hpp @@ -35,6 +35,9 @@ namespace xo { const std::vector & argv() const { return argv_; } const ref::rp & body() const { return body_; } + /** return number of arguments expected by this function **/ + int n_arg() const { return argv_.size(); } + // ----- Expression ----- virtual void display(std::ostream & os) const override; diff --git a/include/xo/expression/Primitive.hpp b/include/xo/expression/Primitive.hpp index afae94a9..f4fc21b7 100644 --- a/include/xo/expression/Primitive.hpp +++ b/include/xo/expression/Primitive.hpp @@ -33,17 +33,20 @@ namespace xo { FunctionPointer fnptr) : PrimitiveInterface(), name_{name}, - value_td_{Reflect::require()}, + value_td_{Reflect::require_function()}, value_{fnptr} - {} + { + if (!value_td_->is_function()) + throw std::runtime_error("Primitive: expected function pointer"); + if (!value_td_->fn_retval()) + throw std::runtime_error("Primitive: expected non-null function return value"); + } FunctionPointer value() const { return value_; } // ----- PrimitiveInterface ----- - virtual std::string const & name() const { return name_; } - /** FIXME for now **/ - virtual int n_arg() const { return 1; } + virtual std::string const & name() const override { return name_; } // ----- ConstantInterface ----- diff --git a/include/xo/expression/PrimitiveInterface.hpp b/include/xo/expression/PrimitiveInterface.hpp index c294a440..3274195f 100644 --- a/include/xo/expression/PrimitiveInterface.hpp +++ b/include/xo/expression/PrimitiveInterface.hpp @@ -22,7 +22,9 @@ namespace xo { } virtual const std::string & name() const = 0; - virtual int n_arg() const = 0; + int n_arg() const { return this->value_td()->n_fn_arg(); } + TypeDescr fn_retval() const { return this->value_td()->fn_retval(); } + TypeDescr fn_arg(uint32_t i) const { return this->value_td()->fn_arg(i); } //virtual TypeDescr value_td() const override = 0; //virtual TaggedPtr value_tp() const override = 0;