lambda stuff [wip]
This commit is contained in:
parent
d4fd55b8ed
commit
5038045bdc
10 changed files with 309 additions and 41 deletions
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
set(SELF_LIB xo_expression)
|
||||
set(SELF_SRCS
|
||||
GeneralizedExpression.cpp
|
||||
Expression.cpp
|
||||
DefineExpr.cpp
|
||||
Apply.cpp
|
||||
Lambda.cpp
|
||||
Variable.cpp
|
||||
IfExpr.cpp
|
||||
Sequence.cpp
|
||||
LocalEnv.cpp
|
||||
ConvertExpr.cpp
|
||||
Primitive.cpp
|
||||
|
|
|
|||
|
|
@ -4,10 +4,6 @@
|
|||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
std::string
|
||||
Expression::display_string() const {
|
||||
return tostr(*this);
|
||||
}
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
14
src/expression/GeneralizedExpression.cpp
Normal file
14
src/expression/GeneralizedExpression.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* @file GeneralizedExpression.cpp */
|
||||
|
||||
#include "GeneralizedExpression.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
std::string
|
||||
GeneralizedExpression::display_string() const {
|
||||
return tostr(*this);
|
||||
}
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end GeneralizedExpression.cpp */
|
||||
|
|
@ -5,19 +5,21 @@
|
|||
#include "xo/reflect/function/FunctionTdx.hpp"
|
||||
#include "xo/indentlog/print/vector.hpp"
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
||||
namespace xo {
|
||||
using xo::reflect::TypeDescr;
|
||||
using xo::reflect::TypeDescrBase;
|
||||
using xo::reflect::FunctionTdxInfo;
|
||||
using std::stringstream;
|
||||
|
||||
namespace ast {
|
||||
rp<Lambda>
|
||||
Lambda::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body)
|
||||
TypeDescr
|
||||
Lambda::assemble_lambda_td(const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
using xo::reflect::FunctionTdx;
|
||||
if (!body)
|
||||
return nullptr;
|
||||
|
||||
/** assemble function type.
|
||||
*
|
||||
|
|
@ -41,8 +43,39 @@ namespace xo {
|
|||
TypeDescr lambda_td
|
||||
= TypeDescrBase::require_by_fn_info(function_info);
|
||||
|
||||
return lambda_td;
|
||||
}
|
||||
|
||||
std::string
|
||||
Lambda::assemble_type_str(TypeDescr lambda_td) {
|
||||
assert(lambda_td);
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
ss << lambda_td->fn_retval()->short_name()
|
||||
<< "(";
|
||||
|
||||
for (std::size_t i = 0, n = lambda_td->n_fn_arg(); i < n; ++i) {
|
||||
if (i > 0)
|
||||
ss << ",";
|
||||
ss << lambda_td->fn_arg(i)->short_name();
|
||||
}
|
||||
ss << ")";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
rp<Lambda>
|
||||
Lambda::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
using xo::reflect::FunctionTdx;
|
||||
|
||||
rp<LocalEnv> env = LocalEnv::make(argv);
|
||||
|
||||
TypeDescr lambda_td = assemble_lambda_td(argv, body);
|
||||
|
||||
rp<Lambda> retval
|
||||
= new Lambda(name,
|
||||
lambda_td,
|
||||
|
|
@ -122,14 +155,15 @@ namespace xo {
|
|||
} /*regularize_layer_vars*/
|
||||
|
||||
Lambda::Lambda(const std::string & name,
|
||||
TypeDescr lambda_type,
|
||||
TypeDescr lambda_td,
|
||||
const rp<LocalEnv> & local_env,
|
||||
const rp<Expression> & body)
|
||||
: FunctionInterface(exprtype::lambda, lambda_type),
|
||||
: FunctionInterface(exprtype::lambda, lambda_td),
|
||||
name_{name},
|
||||
body_{body},
|
||||
local_env_{local_env}
|
||||
{
|
||||
#ifdef OBSOLETE
|
||||
stringstream ss;
|
||||
ss << "double";
|
||||
ss << "(";
|
||||
|
|
@ -139,8 +173,10 @@ namespace xo {
|
|||
ss << "double";
|
||||
}
|
||||
ss << ")";
|
||||
#endif
|
||||
|
||||
this->type_str_ = ss.str();
|
||||
if (lambda_td)
|
||||
this->type_str_ = assemble_type_str(lambda_td);
|
||||
|
||||
/* ensure variables are unique within layer for this lambda */
|
||||
this->layer_var_map_ = this->regularize_layer_vars();
|
||||
|
|
@ -209,6 +245,28 @@ namespace xo {
|
|||
<< xtag("body", body_)
|
||||
<< ">";
|
||||
} /*display*/
|
||||
|
||||
// ----- Lambda Access -----
|
||||
|
||||
rp<LambdaAccess>
|
||||
LambdaAccess::make(const std::string & name,
|
||||
const std::vector<rp<Variable>> & argv,
|
||||
const rp<Expression> & body)
|
||||
{
|
||||
TypeDescr lambda_td =
|
||||
|
||||
TypeDescr body_valuetype = nullptr;
|
||||
|
||||
}
|
||||
|
||||
rp<LambdaAccess>
|
||||
LambdaAccess::make_empty()
|
||||
{
|
||||
return new LambdaAccess("" /*name*/,
|
||||
nullptr /*lambda_td*/,
|
||||
nullptr /*local_env*/,
|
||||
nullptr /*body*/);
|
||||
}
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
|
|
|||
78
src/expression/Sequence.cpp
Normal file
78
src/expression/Sequence.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/* @file Sequence.cpp */
|
||||
|
||||
#include "Sequence.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
std::set<std::string>
|
||||
Sequence::get_free_variables() const {
|
||||
std::set<std::string> retval;
|
||||
|
||||
for (const auto & x : expr_v_) {
|
||||
std::set<std::string> free_vars;
|
||||
free_vars = x->get_free_variables();
|
||||
|
||||
for (const auto & y : free_vars)
|
||||
retval.insert(y);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Sequence::visit_preorder(VisitFn visitor_fn) {
|
||||
std::size_t n = 1;
|
||||
|
||||
visitor_fn(this);
|
||||
|
||||
for (const auto & x : expr_v_)
|
||||
n += x->visit_preorder(visitor_fn);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
Sequence::visit_layer(VisitFn visitor_fn) {
|
||||
std::size_t n = 1;
|
||||
|
||||
visitor_fn(this);
|
||||
|
||||
for (const auto & x : expr_v_)
|
||||
n += x->visit_layer(visitor_fn);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
rp<Expression>
|
||||
Sequence::xform_layer(TransformFn xform_fn) {
|
||||
for (std::size_t i = 0, n = expr_v_.size(); i < n; ++i) {
|
||||
expr_v_[i] = expr_v_[i]->xform_layer(xform_fn);
|
||||
}
|
||||
|
||||
return xform_fn(this);
|
||||
}
|
||||
|
||||
void
|
||||
Sequence::attach_envs(ref::brw<Environment> p) {
|
||||
for (const auto & x : expr_v_)
|
||||
x->attach_envs(p);
|
||||
}
|
||||
|
||||
void
|
||||
Sequence::display(std::ostream & os) const {
|
||||
os << "<Sequence";
|
||||
std::size_t i = 0;
|
||||
for (const auto & x : expr_v_) {
|
||||
std::string i_str = tostr("[", i, "]");
|
||||
|
||||
os << xtag(i_str.c_str(), x);
|
||||
}
|
||||
|
||||
os << ">";
|
||||
}
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end Sequence.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue