xo-expression: + Variable + apply/lambda fixes
This commit is contained in:
parent
20b23b50fc
commit
9d2b0f17f8
7 changed files with 90 additions and 3 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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*/
|
||||
|
||||
|
|
|
|||
42
include/xo/expression/Variable.hpp
Normal file
42
include/xo/expression/Variable.hpp
Normal 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 **/
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ set(SELF_SRCS
|
|||
Expression.cpp
|
||||
Apply.cpp
|
||||
Lambda.cpp
|
||||
Variable.cpp
|
||||
#init_reflect.cpp
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ namespace xo {
|
|||
void
|
||||
Lambda::display(std::ostream & os) const {
|
||||
os << "<Lambda"
|
||||
<< xtag("name", name_)
|
||||
<< xtag("argv", argv_)
|
||||
<< xtag("body", body_)
|
||||
<< ">";
|
||||
|
|
|
|||
17
src/expression/Variable.cpp
Normal file
17
src/expression/Variable.cpp
Normal 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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue