xo-expression: refactor: Lambda/Primitive share FunctionInterface

This commit is contained in:
Roland Conybeare 2024-06-19 10:53:04 -04:00
commit 5c4b062cf8
6 changed files with 63 additions and 22 deletions

View file

@ -0,0 +1,34 @@
/** @file FunctionInterface.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "Expression.hpp"
//#include <cstdint>
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<FunctionInterface> from(ref::brw<Expression> x) {
return ref::brw<FunctionInterface>::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 **/

View file

@ -6,6 +6,7 @@
#pragma once
#include "Expression.hpp"
#include "FunctionInterface.hpp"
#include "Variable.hpp"
#include <vector>
#include <string>
@ -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<Lambda>::from(x);
}
const std::string & name() const { return name_; }
const std::string & type_str() const { return type_str_; }
const std::vector<ref::rp<Variable>> & argv() const { return argv_; }
const ref::rp<Expression> & 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 -----

View file

@ -6,6 +6,7 @@
#pragma once
#include "PrimitiveInterface.hpp"
#include "xo/reflect/Reflect.hpp"
//#include <cstdint>
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<void*>(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 {

View file

@ -5,30 +5,27 @@
#pragma once
#include "ConstantInterface.hpp"
#include "FunctionInterface.hpp"
//#include <cstdint>
#include <type_traits>
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<PrimitiveInterface> from(ref::brw<Expression> x) {
return ref::brw<PrimitiveInterface>::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*/

View file

@ -49,7 +49,7 @@ namespace xo {
TypeDescr lambda_type,
const std::vector<rp<Variable>> & argv,
const ref::rp<Expression> & body)
: Expression(exprtype::lambda, lambda_type),
: FunctionInterface(exprtype::lambda, lambda_type),
name_{name},
argv_{argv},
body_{body}

View file

@ -8,6 +8,7 @@ namespace xo {
Variable::display(std::ostream & os) const {
os << "<Variable"
<< xtag("name", name_)
<< xtag("type", this->valuetype()->short_name())
<< ">";
} /*display*/
} /*namespace ast*/