xo-expression: + Expression::visit_preorder()

This commit is contained in:
Roland Conybeare 2024-06-24 15:10:06 -04:00
commit 829bffd007
7 changed files with 65 additions and 2 deletions

View file

@ -36,7 +36,20 @@ namespace xo {
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;
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
std::size_t n = 1;
visitor_fn(this);
n += fn_->visit_preorder(visitor_fn);
for (const auto & arg : argv_)
n += arg->visit_preorder(visitor_fn);
return n;
}
virtual void display(std::ostream & os) const override;
private:
Apply(TypeDescr apply_valuetype,

View file

@ -51,6 +51,11 @@ namespace xo {
// ----- Expression -----
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
visitor_fn(this);
return 1;
}
virtual void display(std::ostream & os) const override {
os << "<Constant"
<< xtag("type", value_td_->short_name())

View file

@ -8,6 +8,7 @@
#include "xo/reflect/TypeDescr.hpp"
#include "xo/refcnt/Refcounted.hpp"
#include "exprtype.hpp"
#include <functional>
namespace xo {
namespace ast {
@ -29,6 +30,7 @@ namespace xo {
**/
class Expression : public ref::Refcount {
public:
using VisitFn = std::function<void (ref::brw<Expression>)>;
using TypeDescr = xo::reflect::TypeDescr;
public:
@ -38,6 +40,13 @@ namespace xo {
exprtype extype() const { return extype_; }
TypeDescr valuetype() const { return valuetype_; }
/** visit each Expression node in this AST,
* and invoke @p fn for each.
* Returns the number of nodes visited.
* Preorder: call @p fn for a node before visiting children
**/
virtual std::size_t visit_preorder(VisitFn visitor_fn) = 0;
/** write human-readable representation to stream **/
virtual void display(std::ostream & os) const = 0;
/** human-readable string representation **/

View file

@ -40,6 +40,17 @@ namespace xo {
// ----- Expression -----
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
std::size_t n = 1;
visitor_fn(this);
n += this->when_true_->visit_preorder(visitor_fn);
n += this->when_false_->visit_preorder(visitor_fn);
return n;
}
virtual void display(std::ostream & os) const override;
private:

View file

@ -48,6 +48,19 @@ namespace xo {
// ----- Expression -----
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
std::size_t n = 1;
visitor_fn(this);
for (const auto & arg : argv_)
n += arg->visit_preorder(visitor_fn);
n += body_->visit_preorder(visitor_fn);
return n;
}
virtual void display(std::ostream & os) const override;
private:

View file

@ -46,6 +46,13 @@ namespace xo {
// virtual TypeDescr fn_retval() const;
// virtual TypeDescr fn_arg(uint32_t i) const;
// ----- Expression -----
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
visitor_fn(this);
return 1;
}
private:
}; /*PrimitiveInterface*/
} /*namespace ast*/

View file

@ -31,7 +31,12 @@ namespace xo {
const std::string & name() const { return name_; }
virtual void display(std::ostream & os) const;
virtual std::size_t visit_preorder(VisitFn visitor_fn) override {
visitor_fn(this);
return 1;
}
virtual void display(std::ostream & os) const override;
private:
Variable(const std::string & name,