xo-expression: initial commit (constant + primitive)

This commit is contained in:
Roland Conybeare 2024-06-13 15:20:36 -04:00
commit 2d94fd51bf
17 changed files with 544 additions and 0 deletions

View file

@ -0,0 +1,32 @@
/** @file Apply.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "Expression.hpp"
//#include <cstdint>
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*/
} /*namespace xo*/
/** end Apply.hpp **/

View file

@ -0,0 +1,78 @@
/** @file Constant.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "ConstantInterface.hpp"
#include <type_traits>
namespace xo {
namespace ast {
/** @class Constant
* @brief syntax for a literal constant.
*
* Require:
* 1. T must be a POD type (plain old data)
* We need this to be true so that we can generate
* code for constructing a T instance by memcopying
* @ref value_
*
* @tp T type of captured literal.
**/
template <typename T>
class Constant : public ConstantInterface {
public:
using Reflect = xo::reflect::Reflect;
using TaggedPtr = xo::reflect::TaggedPtr;
using TypeDescr = xo::reflect::TypeDescr;
public:
explicit Constant(const T & x)
: ConstantInterface(exprtype::constant),
value_td_{Reflect::require<T>()},
value_(x)
{
static_assert(std::is_standard_layout_v<T> && std::is_trivial_v<T>);
}
const T & value() const { return value_; }
// ----- ConstantInterface -----
virtual TypeDescr value_td() const override { return value_td_; }
virtual TaggedPtr value_tp() const override {
/* 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);
return TaggedPtr(value_td_, erased_ptr);
}
// ----- Expression -----
virtual void display(std::ostream & os) const override {
os << "<Constant"
<< xtag("type", value_td_->short_name())
<< xtag("value", value_)
<< ">";
}
private:
/** type description for T **/
TypeDescr value_td_;
/** value of this constant **/
T value_;
}; /*Constant*/
template <typename T>
ref::rp<Constant<std::remove_reference_t<T>>>
make_constant(const T & x) {
return new Constant(x);
}
} /*namespace ast*/
} /*namespace xo*/
/** end Constant.hpp **/

View file

@ -0,0 +1,41 @@
/** @file ConstantInterface.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "Expression.hpp"
#include "xo/reflect/Reflect.hpp"
#include "xo/reflect/TypeDescr.hpp"
#include <type_traits>
namespace xo {
namespace ast {
/** @class ConstantInterface
* @brief syntax for a literal constant.
**/
class ConstantInterface : public Expression {
public:
using TaggedPtr = xo::reflect::TaggedPtr;
using TypeDescr = xo::reflect::TypeDescr;
public:
/** @p extype sets expression-type; could be constant|primitive **/
ConstantInterface(exprtype extype) : Expression{extype} {}
/** downcast from Expression **/
static ref::brw<ConstantInterface> from(ref::brw<Expression> x) {
return ref::brw<ConstantInterface>::from(x);
}
/** type description for representation of literal value **/
virtual TypeDescr value_td() const = 0;
/** reflection-tagged pointer to literal value of this constant **/
virtual TaggedPtr value_tp() const = 0;
}; /*ConstantInterface*/
} /*namespace ast*/
} /*namespace xo*/
/** end ConstantInterface.hpp **/

View file

@ -0,0 +1,48 @@
/** @file Expression.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "xo/refcnt/Refcounted.hpp"
#include "exprtype.hpp"
namespace xo {
namespace ast {
/** @class Expression
* @brief abstract syntax tree for an EGAD program
*
* (Expression Graph with Automagic Derivation)
*
* Things you can do with an Expression:
* - evaluate it using an interpreter
* - execute it on a VM
* - compile using LLVM
* see xo-jit/
**/
class Expression : public ref::Refcount {
public:
explicit Expression(exprtype extype) : extype_{extype} {}
exprtype extype() const { return extype_; }
/** write human-readable representation to stream **/
virtual void display(std::ostream & os) const = 0;
/** human-readable string representation **/
virtual std::string display_string() const;
private:
/** expression type (constant | apply | ..) for this expression **/
exprtype extype_ = exprtype::invalid;
}; /*Expression*/
inline std::ostream &
operator<<(std::ostream & os, const Expression & x) {
x.display(os);
return os;
}
} /*namespace ast*/
} /*namespace xo*/
/** end Expression.hpp **/

View file

@ -0,0 +1,89 @@
/** @file Primitive.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "PrimitiveInterface.hpp"
//#include <cstdint>
namespace xo {
namespace ast {
/** @class Primitive
* @brief syntax for a constant that refers to a known function.
*
* Two cases here:
* 1. primitive refers to a function that is supported directly in llvm
* (e.g. floating-point addition)
* 2. primitive refers to a compiled (C/C++) function that we can invoke at runtime
*
* In any case, a primitive serves as both declaration and definition
* (May be possible to relax this to declaration-only using null value_ as sentinel..?)
**/
template <typename FunctionPointer>
class Primitive: public PrimitiveInterface {
public:
using Reflect = xo::reflect::Reflect;
using TaggedPtr = xo::reflect::TaggedPtr;
using TypeDescr = xo::reflect::TypeDescr;
public:
Primitive(const std::string & name,
FunctionPointer fnptr)
: PrimitiveInterface(),
name_{name},
value_td_{Reflect::require<FunctionPointer>()},
value_{fnptr}
{}
FunctionPointer value() const { return value_; }
// ----- PrimitiveInterface -----
virtual std::string const & name() const { return name_; }
// ----- ConstantInterface -----
virtual TypeDescr value_td() const override { return value_td_; }
virtual TaggedPtr value_tp() const override {
/* 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);
return TaggedPtr(value_td_, erased_ptr);
}
// ----- Expression -----
virtual void display(std::ostream & os) const override {
os << "<Primitive"
<< xtag("name", name_)
<< xtag("type", this->value_td()->short_name())
<< xtag("value", this->value())
<< ">";
}
private:
// from Expression:
// exprtype extype_
/** name of this primitive, e.g. '+', 'sqrt' **/
std::string name_;
/** type description for FunctionPointer **/
TypeDescr value_td_;
/** address of executable function **/
FunctionPointer value_;
}; /*Primitive*/
/** adopt function @p x as a callable primitive function named @p name **/
template <typename FunctionPointer>
ref::rp<Primitive<FunctionPointer>>
make_primitive(const std::string & name, FunctionPointer x) {
return new Primitive(name, x);
}
} /*namespace ast*/
} /*namespace xo*/
/** end Primitive.hpp **/

View file

@ -0,0 +1,35 @@
/** @file PrimitiveInterface.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "ConstantInterface.hpp"
//#include <cstdint>
#include <type_traits>
namespace xo {
namespace ast {
class PrimitiveInterface : public ConstantInterface {
public:
PrimitiveInterface() : ConstantInterface(exprtype::primitive) {}
/** 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;
//virtual TypeDescr value_td() const override = 0;
//virtual TaggedPtr value_tp() const override = 0;
private:
}; /*PrimitiveInterface*/
} /*namespace ast*/
} /*namespace xo*/
/** end PrimitiveInterface.hpp **/

View file

@ -0,0 +1,51 @@
/** @file exprtype.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include <cstdint>
namespace xo {
namespace ast {
/** @enum exprtype
* @brief enum to identify subclasses of xo::ast::Expression.
*
**/
enum class exprtype {
/** sentinel value **/
invalid = -1,
/** literal constant. must satisfy both standard_layout_type + trivial **/
constant,
/** a literal constant that refers to a linkable named function **/
primitive,
/** function call **/
apply,
/** not an expression. comes last, counts entries **/
n_expr
};
inline const char *
expr2str(exprtype x)
{
switch(x) {
case exprtype::invalid: return "?exprtype";
case exprtype::constant: return "constant";
case exprtype::primitive: return "primitive";
case exprtype::apply: return "apply";
default: break;
}
return "???exprtype???";
}
/** @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);
} /*namespace ast*/
} /*namespace xo*/
/** end exprtype.hpp **/