xo-expression: + Apply expressions

This commit is contained in:
Roland Conybeare 2024-06-13 16:20:53 -04:00
commit d6371ee369
6 changed files with 64 additions and 6 deletions

View file

@ -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<Expression> fn_;
std::vector<ref::rp<Expression>> args_;
};
} /*namespace ast*/
public:
Apply(const ref::rp<Expression> & fn,
const std::vector<ref::rp<Expression>> & argv)
: Expression(exprtype::apply), fn_{fn}, argv_(argv)
{}
/** downcast from Expression **/
static ref::brw<Apply> from(ref::brw<Expression> x) {
return ref::brw<Apply>::from(x);
}
const ref::rp<Expression> & fn() const { return fn_; }
const std::vector<ref::rp<Expression>> & argv() const { return argv_; }
virtual void display(std::ostream & os) const;
private:
/** function to invoke **/
ref::rp<Expression> fn_;
/** argument expressions, in l-to-r order **/
std::vector<ref::rp<Expression>> argv_;
}; /*Apply*/
inline ref::rp<Apply>
make_apply(const ref::rp<Expression> & fn,
const ref::rp<Expression> & arg1) {
std::vector<ref::rp<Expression>> argv;
argv.push_back(arg1);
return new Apply(fn, argv);
} /*make_apply*/
} /*namespace ast*/
} /*namespace xo*/

View file

@ -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 -----

View file

@ -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;

View file

@ -5,6 +5,7 @@
#pragma once
#include <ostream>
#include <cstdint>
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<std::size_t>(exprtype::n_expr);
inline std::ostream &
operator<<(std::ostream & os,
exprtype x)
{
os << expr2str(x);
return os;
}
} /*namespace ast*/
} /*namespace xo*/

19
src/expression/Apply.cpp Normal file
View file

@ -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 << "<Apply"
<< xtag("fn", fn_)
<< xtag("argv", argv_)
<< ">";
}
} /*namespace ast*/
} /*namespace xo*/
/* end Apply.cpp */

View file

@ -3,6 +3,7 @@
set(SELF_LIB xo_expression)
set(SELF_SRCS
Expression.cpp
Apply.cpp
#init_reflect.cpp
)