From d6371ee3694a37aeea9f6b8f2e91d23e87d409f8 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 13 Jun 2024 16:20:53 -0400 Subject: [PATCH] xo-expression: + Apply expressions --- include/xo/expression/Apply.hpp | 40 ++++++++++++++++---- include/xo/expression/Primitive.hpp | 2 + include/xo/expression/PrimitiveInterface.hpp | 1 + include/xo/expression/exprtype.hpp | 9 +++++ src/expression/Apply.cpp | 19 ++++++++++ src/expression/CMakeLists.txt | 1 + 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/expression/Apply.cpp diff --git a/include/xo/expression/Apply.hpp b/include/xo/expression/Apply.hpp index 518415f0..56f6b886 100644 --- a/include/xo/expression/Apply.hpp +++ b/include/xo/expression/Apply.hpp @@ -11,21 +11,47 @@ namespace xo { namespace ast { + /** @class Apply * @brief syntax for a function call. * * In general we don't know function to be invoked * until runtime, depending on the nature of Expression. - * - * For first cut, we'll just handle the case of a Constant - * that refers to a known function **/ class Apply : public Expression { - ref::rp fn_; - std::vector> args_; - }; - } /*namespace ast*/ + public: + Apply(const ref::rp & fn, + const std::vector> & argv) + : Expression(exprtype::apply), fn_{fn}, argv_(argv) + {} + /** downcast from Expression **/ + static ref::brw from(ref::brw x) { + return ref::brw::from(x); + } + + const ref::rp & fn() const { return fn_; } + const std::vector> & argv() const { return argv_; } + + virtual void display(std::ostream & os) const; + + private: + /** function to invoke **/ + ref::rp fn_; + /** argument expressions, in l-to-r order **/ + std::vector> argv_; + }; /*Apply*/ + + inline ref::rp + make_apply(const ref::rp & fn, + const ref::rp & arg1) { + std::vector> argv; + argv.push_back(arg1); + + return new Apply(fn, argv); + } /*make_apply*/ + + } /*namespace ast*/ } /*namespace xo*/ diff --git a/include/xo/expression/Primitive.hpp b/include/xo/expression/Primitive.hpp index e0de42d7..afae94a9 100644 --- a/include/xo/expression/Primitive.hpp +++ b/include/xo/expression/Primitive.hpp @@ -42,6 +42,8 @@ namespace xo { // ----- PrimitiveInterface ----- virtual std::string const & name() const { return name_; } + /** FIXME for now **/ + virtual int n_arg() const { return 1; } // ----- ConstantInterface ----- diff --git a/include/xo/expression/PrimitiveInterface.hpp b/include/xo/expression/PrimitiveInterface.hpp index 54e51451..c294a440 100644 --- a/include/xo/expression/PrimitiveInterface.hpp +++ b/include/xo/expression/PrimitiveInterface.hpp @@ -22,6 +22,7 @@ namespace xo { } virtual const std::string & name() const = 0; + virtual int n_arg() const = 0; //virtual TypeDescr value_td() const override = 0; //virtual TaggedPtr value_tp() const override = 0; diff --git a/include/xo/expression/exprtype.hpp b/include/xo/expression/exprtype.hpp index fe810bb7..1988f7e9 100644 --- a/include/xo/expression/exprtype.hpp +++ b/include/xo/expression/exprtype.hpp @@ -5,6 +5,7 @@ #pragma once +#include #include namespace xo { @@ -44,6 +45,14 @@ namespace xo { /** @brief number of built-in expression types, repr convenient for array sizing **/ static constexpr std::size_t n_exprtype = static_cast(exprtype::n_expr); + + inline std::ostream & + operator<<(std::ostream & os, + exprtype x) + { + os << expr2str(x); + return os; + } } /*namespace ast*/ } /*namespace xo*/ diff --git a/src/expression/Apply.cpp b/src/expression/Apply.cpp new file mode 100644 index 00000000..781ec170 --- /dev/null +++ b/src/expression/Apply.cpp @@ -0,0 +1,19 @@ +/* @file Apply.cpp */ + +#include "Apply.hpp" +#include "xo/indentlog/print/vector.hpp" + +namespace xo { + namespace ast { + void + Apply::display(std::ostream & os) const { + os << ""; + } + } /*namespace ast*/ +} /*namespace xo*/ + + +/* end Apply.cpp */ diff --git a/src/expression/CMakeLists.txt b/src/expression/CMakeLists.txt index 94e74d0c..59e754a5 100644 --- a/src/expression/CMakeLists.txt +++ b/src/expression/CMakeLists.txt @@ -3,6 +3,7 @@ set(SELF_LIB xo_expression) set(SELF_SRCS Expression.cpp + Apply.cpp #init_reflect.cpp )