diff --git a/include/xo/expression/FunctionInterface.hpp b/include/xo/expression/FunctionInterface.hpp new file mode 100644 index 00000000..a51dc69b --- /dev/null +++ b/include/xo/expression/FunctionInterface.hpp @@ -0,0 +1,34 @@ +/** @file FunctionInterface.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "Expression.hpp" +//#include + +namespace xo { + namespace ast { + class FunctionInterface : public Expression { + public: + FunctionInterface(exprtype extype, TypeDescr fn_type) + : Expression(extype, fn_type) {} + + /** downcast from Expression **/ + static ref::brw from(ref::brw x) { + return ref::brw::from(x); + } + + virtual const std::string & name() const = 0; + virtual int n_arg() const = 0; // { return this->value_td()->n_fn_arg(); } + virtual TypeDescr fn_retval() const = 0; // { return this->value_td()->fn_retval(); } + virtual TypeDescr fn_arg(uint32_t i) const = 0; // { return this->value_td()->fn_arg(i); } + + private: + }; /*FunctionInterface*/ + } /*namespace ast*/ +} /*namespace xo*/ + + +/** end FunctionInterface.hpp **/ diff --git a/include/xo/expression/Lambda.hpp b/include/xo/expression/Lambda.hpp index cdf72ed9..ce63860d 100644 --- a/include/xo/expression/Lambda.hpp +++ b/include/xo/expression/Lambda.hpp @@ -6,6 +6,7 @@ #pragma once #include "Expression.hpp" +#include "FunctionInterface.hpp" #include "Variable.hpp" #include #include @@ -17,7 +18,7 @@ namespace xo { * @brief abstract syntax tree for a function definition * **/ - class Lambda : public Expression { + class Lambda : public FunctionInterface { public: /** * @p name Name for this lambda -- must be unique @@ -33,13 +34,17 @@ namespace xo { return ref::brw::from(x); } - const std::string & name() const { return name_; } const std::string & type_str() const { return type_str_; } const std::vector> & argv() const { return argv_; } const ref::rp & body() const { return body_; } + // ----- FunctionInterface ----- + + virtual const std::string & name() const override { return name_; } /** return number of arguments expected by this function **/ - int n_arg() const { return argv_.size(); } + virtual int n_arg() const override { return argv_.size(); } + virtual TypeDescr fn_retval() const override { return body_->valuetype(); } + virtual TypeDescr fn_arg(uint32_t i) const override { return argv_[i]->valuetype(); } // ----- Expression ----- diff --git a/include/xo/expression/Primitive.hpp b/include/xo/expression/Primitive.hpp index 507bee8e..f251465d 100644 --- a/include/xo/expression/Primitive.hpp +++ b/include/xo/expression/Primitive.hpp @@ -6,6 +6,7 @@ #pragma once #include "PrimitiveInterface.hpp" +#include "xo/reflect/Reflect.hpp" //#include namespace xo { @@ -42,14 +43,8 @@ namespace xo { FunctionPointer value() const { return value_; } - // ----- PrimitiveInterface ----- - - virtual std::string const & name() const override { return name_; } - - // ----- ConstantInterface ----- - - virtual TypeDescr value_td() const override { return value_td_; } - virtual TaggedPtr value_tp() const override { + TypeDescr value_td() const { return value_td_; } + TaggedPtr value_tp() const { /* note: idk why, but need to spell this out in two steps with gcc 13.2 */ const void * erased_cptr = &value_; void * erased_ptr = const_cast(erased_cptr); @@ -57,6 +52,15 @@ namespace xo { return TaggedPtr(value_td_, erased_ptr); } + // ----- PrimitiveInterface ----- + + // ----- FunctionInterface ----- + + virtual std::string const & name() const override { return name_; } + virtual int n_arg() const override { return this->value_td()->n_fn_arg(); } + virtual TypeDescr fn_retval() const override { return this->value_td()->fn_retval(); } + virtual TypeDescr fn_arg(uint32_t i) const override { return this->value_td()->fn_arg(i); } + // ----- Expression ----- virtual void display(std::ostream & os) const override { diff --git a/include/xo/expression/PrimitiveInterface.hpp b/include/xo/expression/PrimitiveInterface.hpp index 3798c329..685d04ae 100644 --- a/include/xo/expression/PrimitiveInterface.hpp +++ b/include/xo/expression/PrimitiveInterface.hpp @@ -5,30 +5,27 @@ #pragma once -#include "ConstantInterface.hpp" +#include "FunctionInterface.hpp" //#include #include namespace xo { namespace ast { - class PrimitiveInterface : public ConstantInterface { + class PrimitiveInterface : public FunctionInterface { public: PrimitiveInterface(TypeDescr fn_type) - : ConstantInterface(exprtype::primitive, fn_type) {} + : FunctionInterface(exprtype::primitive, fn_type) {} /** downcast from Expression **/ static ref::brw from(ref::brw x) { return ref::brw::from(x); } - virtual const std::string & name() 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; + // virtual const std::string & name() const; + // virtual int n_arg() const; + // virtual TypeDescr fn_retval() const; + // virtual TypeDescr fn_arg(uint32_t i) const; private: }; /*PrimitiveInterface*/ diff --git a/src/expression/Lambda.cpp b/src/expression/Lambda.cpp index d92ac801..4cd25da9 100644 --- a/src/expression/Lambda.cpp +++ b/src/expression/Lambda.cpp @@ -49,7 +49,7 @@ namespace xo { TypeDescr lambda_type, const std::vector> & argv, const ref::rp & body) - : Expression(exprtype::lambda, lambda_type), + : FunctionInterface(exprtype::lambda, lambda_type), name_{name}, argv_{argv}, body_{body} diff --git a/src/expression/Variable.cpp b/src/expression/Variable.cpp index c8ab19fc..52890ccc 100644 --- a/src/expression/Variable.cpp +++ b/src/expression/Variable.cpp @@ -8,6 +8,7 @@ namespace xo { Variable::display(std::ostream & os) const { os << "valuetype()->short_name()) << ">"; } /*display*/ } /*namespace ast*/