xo-expression: add explicit types to all Expressions

This commit is contained in:
Roland Conybeare 2024-06-18 16:55:46 -04:00
commit 9ff173f68a
12 changed files with 223 additions and 54 deletions

View file

@ -4,7 +4,29 @@
#include "xo/indentlog/print/vector.hpp"
namespace xo {
using xo::ref::rp;
namespace ast {
rp<Apply>
Apply::make(const rp<Expression> & fn,
const std::vector<rp<Expression>> & argv)
{
/* extract result type from function type */
TypeDescr fn_valuetype = fn->valuetype();
if (!fn_valuetype->is_function()) {
throw std::runtime_error
(tostr("Apply::make: found expression F in function position,"
" with value-type FT where a function type expected",
xtag("FT", fn_valuetype->short_name()),
xtag("F", fn_valuetype)));
}
TypeDescr fn_retval_type = fn_valuetype->fn_retval();
return new Apply(fn_retval_type, fn, argv);
}
void
Apply::display(std::ostream & os) const {
os << "<Apply"

View file

@ -4,7 +4,34 @@
#include "xo/indentlog/print/vector.hpp"
namespace xo {
using xo::ref::rp;
namespace ast {
rp<IfExpr>
IfExpr::make(const rp<Expression> & test,
const rp<Expression> & when_true,
const rp<Expression> & when_false)
{
/** TODO: verify test returns _boolean_ type **/
if (when_true->valuetype() != when_false->valuetype()) {
throw std::runtime_error
(tostr("IfExpr::make:"
" types {T1,T2} found for branches of if-expr"
" where equal types expected",
xtag("T1", when_true->valuetype()->canonical_name()),
xtag("T2", when_false->valuetype()->canonical_name())));
}
/* arbitrary choice here */
auto ifexpr_type = when_true->valuetype();
return new IfExpr(ifexpr_type,
test,
when_true,
when_false);
} /*make*/
void
IfExpr::display(std::ostream & os) const {
os << "<IfExpr"

View file

@ -1,16 +1,55 @@
/* @file Lambda.cpp */
#include "Lambda.hpp"
#include "xo/reflect/TypeDescr.hpp"
#include "xo/reflect/function/FunctionTdx.hpp"
#include "xo/indentlog/print/vector.hpp"
namespace xo {
using xo::reflect::TypeDescrBase;
using xo::reflect::FunctionTdxInfo;
using xo::ref::rp;
using std::stringstream;
namespace ast {
rp<Lambda>
Lambda::make(const std::string & name,
const std::vector<rp<Expression>> & argv,
const ref::rp<Expression> & body)
{
using xo::reflect::FunctionTdx;
/** assemble function type.
*
* NOTE: need this to be unique!
**/
std::vector<TypeDescr> arg_td_v;
arg_td_v.reserve(argv.size());
for (const auto & arg : argv) {
arg_td_v.push_back(arg->valuetype());
}
auto function_info
= FunctionTdxInfo(body->valuetype(),
arg_td_v,
false /*!is_noexcept*/);
TypeDescr lambda_td
= TypeDescrBase::require_by_fn_info(function_info);
return new Lambda(name,
lambda_td,
argv,
body);
} /*make*/
Lambda::Lambda(const std::string & name,
const std::vector<std::string> & argv,
TypeDescr lambda_type,
const std::vector<rp<Expression>> & argv,
const ref::rp<Expression> & body)
: Expression(exprtype::lambda),
: Expression(exprtype::lambda, lambda_type),
name_{name},
argv_{argv},
body_{body}