xo-expression: + Variable + apply/lambda fixes

This commit is contained in:
Roland Conybeare 2024-06-13 17:59:51 -04:00
commit 9d2b0f17f8
7 changed files with 90 additions and 3 deletions

View file

@ -44,7 +44,7 @@ namespace xo {
inline ref::rp<Apply>
make_apply(const ref::rp<Expression> & fn,
const ref::rp<Expression> & arg1) {
const ref::rp<Expression> & arg1) {
std::vector<ref::rp<Expression>> argv;
argv.push_back(arg1);

View file

@ -21,10 +21,17 @@ namespace xo {
/** @p argv Formal parameters, in left-to-right order
* @p body Expression for body of this function
**/
Lambda(const std::vector<std::string> & argv,
Lambda(const std::string & name,
const std::vector<std::string> & argv,
const ref::rp<Expression> & body)
: Expression(exprtype::lambda), argv_{argv}, body_{body} {}
: Expression(exprtype::lambda), name_{name}, argv_{argv}, body_{body} {}
/** downcast from Expression **/
static ref::brw<Lambda> from(ref::brw<Expression> x) {
return ref::brw<Lambda>::from(x);
}
const std::string & name() const { return name_; }
const std::vector<std::string> & argv() const { return argv_; }
const ref::rp<Expression> & body() const { return body_; }
@ -33,11 +40,27 @@ namespace xo {
virtual void display(std::ostream & os) const override;
private:
/** lambda name. Initially supporting only form like
* (define (foo x y z)
* (+ (* x x) (* y y) (* z z)))
*
* In any case need to supply names for distinct things-for-which-code-is-generated
* so that they can be linked etc.
**/
std::string name_;
/** formal argument names **/
std::vector<std::string> argv_;
/** function body **/
ref::rp<Expression> body_;
}; /*Lambda*/
inline ref::rp<Lambda>
make_lambda(const std::string & name,
const std::vector<std::string> & argv,
const ref::rp<Expression> & body)
{
return new Lambda(name, argv, body);
}
} /*namespace ast*/
} /*namespace xo*/

View file

@ -0,0 +1,42 @@
/** @file Variable.hpp
*
* Author: Roland Conybeare
**/
#pragma once
#include "Expression.hpp"
namespace xo {
namespace ast {
/** @class Variable
* @brief syntax for a variable reference
**/
class Variable : public Expression {
public:
Variable(const std::string & name) : Expression(exprtype::variable), name_{name} {}
/** downcast from Expression **/
static ref::brw<Variable> from(ref::brw<Expression> x) {
return ref::brw<Variable>::from(x);
}
const std::string & name() const { return name_; }
virtual void display(std::ostream & os) const;
private:
/** variable name **/
std::string name_;
}; /*Variable*/
inline ref::rp<Variable>
make_var(const std::string & name) {
return new Variable(name);
}
} /*namespace ast*/
} /*namespace xo*/
/** end Variable.hpp **/

View file

@ -26,6 +26,8 @@ namespace xo {
apply,
/** function definition **/
lambda,
/** variable reference **/
variable,
/** not an expression. comes last, counts entries **/
n_expr
@ -40,6 +42,7 @@ namespace xo {
case exprtype::primitive: return "primitive";
case exprtype::apply: return "apply";
case exprtype::lambda: return "lambda";
case exprtype::variable: return "variable";
default: break;
}

View file

@ -5,6 +5,7 @@ set(SELF_SRCS
Expression.cpp
Apply.cpp
Lambda.cpp
Variable.cpp
#init_reflect.cpp
)

View file

@ -8,6 +8,7 @@ namespace xo {
void
Lambda::display(std::ostream & os) const {
os << "<Lambda"
<< xtag("name", name_)
<< xtag("argv", argv_)
<< xtag("body", body_)
<< ">";

View file

@ -0,0 +1,17 @@
/* @file Variable.cpp */
#include "Variable.hpp"
namespace xo {
namespace ast {
void
Variable::display(std::ostream & os) const {
os << "<Variable"
<< xtag("name", name_)
<< ">";
} /*display*/
} /*namespace ast*/
} /*namespace xo*/
/* end Variable.cpp */