xo-expression: + Lambda (function definitions)
This commit is contained in:
parent
d6371ee369
commit
20b23b50fc
4 changed files with 67 additions and 0 deletions
44
include/xo/expression/Lambda.hpp
Normal file
44
include/xo/expression/Lambda.hpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/** @file Lambda.hpp
|
||||
*
|
||||
* Author: Roland Conybeare
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Expression.hpp"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
//#include <cstdint>
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
/** @class Lambda
|
||||
* @brief abstract syntax tree for a function definition
|
||||
*
|
||||
**/
|
||||
class Lambda : public Expression {
|
||||
public:
|
||||
/** @p argv Formal parameters, in left-to-right order
|
||||
* @p body Expression for body of this function
|
||||
**/
|
||||
Lambda(const std::vector<std::string> & argv,
|
||||
const ref::rp<Expression> & body)
|
||||
: Expression(exprtype::lambda), argv_{argv}, body_{body} {}
|
||||
|
||||
const std::vector<std::string> & argv() const { return argv_; }
|
||||
const ref::rp<Expression> & body() const { return body_; }
|
||||
|
||||
// ----- Expression -----
|
||||
|
||||
virtual void display(std::ostream & os) const override;
|
||||
|
||||
private:
|
||||
/** formal argument names **/
|
||||
std::vector<std::string> argv_;
|
||||
/** function body **/
|
||||
ref::rp<Expression> body_;
|
||||
}; /*Lambda*/
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/** end Lambda.hpp **/
|
||||
|
|
@ -24,6 +24,8 @@ namespace xo {
|
|||
primitive,
|
||||
/** function call **/
|
||||
apply,
|
||||
/** function definition **/
|
||||
lambda,
|
||||
|
||||
/** not an expression. comes last, counts entries **/
|
||||
n_expr
|
||||
|
|
@ -37,6 +39,7 @@ namespace xo {
|
|||
case exprtype::constant: return "constant";
|
||||
case exprtype::primitive: return "primitive";
|
||||
case exprtype::apply: return "apply";
|
||||
case exprtype::lambda: return "lambda";
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ set(SELF_LIB xo_expression)
|
|||
set(SELF_SRCS
|
||||
Expression.cpp
|
||||
Apply.cpp
|
||||
Lambda.cpp
|
||||
#init_reflect.cpp
|
||||
)
|
||||
|
||||
|
|
|
|||
19
src/expression/Lambda.cpp
Normal file
19
src/expression/Lambda.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* @file Lambda.cpp */
|
||||
|
||||
#include "Lambda.hpp"
|
||||
#include "xo/indentlog/print/vector.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace ast {
|
||||
void
|
||||
Lambda::display(std::ostream & os) const {
|
||||
os << "<Lambda"
|
||||
<< xtag("argv", argv_)
|
||||
<< xtag("body", body_)
|
||||
<< ">";
|
||||
} /*display*/
|
||||
} /*namespace ast*/
|
||||
} /*namespace xo*/
|
||||
|
||||
|
||||
/* end Lambda.cpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue