From 20b23b50fc7c584f386361e0d794b3a9bc5f3b80 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 13 Jun 2024 17:05:38 -0400 Subject: [PATCH] xo-expression: + Lambda (function definitions) --- include/xo/expression/Lambda.hpp | 44 ++++++++++++++++++++++++++++++ include/xo/expression/exprtype.hpp | 3 ++ src/expression/CMakeLists.txt | 1 + src/expression/Lambda.cpp | 19 +++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 include/xo/expression/Lambda.hpp create mode 100644 src/expression/Lambda.cpp diff --git a/include/xo/expression/Lambda.hpp b/include/xo/expression/Lambda.hpp new file mode 100644 index 00000000..581ba274 --- /dev/null +++ b/include/xo/expression/Lambda.hpp @@ -0,0 +1,44 @@ +/** @file Lambda.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "Expression.hpp" +#include +#include +//#include + +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 & argv, + const ref::rp & body) + : Expression(exprtype::lambda), argv_{argv}, body_{body} {} + + const std::vector & argv() const { return argv_; } + const ref::rp & body() const { return body_; } + + // ----- Expression ----- + + virtual void display(std::ostream & os) const override; + + private: + /** formal argument names **/ + std::vector argv_; + /** function body **/ + ref::rp body_; + }; /*Lambda*/ + } /*namespace ast*/ +} /*namespace xo*/ + +/** end Lambda.hpp **/ diff --git a/include/xo/expression/exprtype.hpp b/include/xo/expression/exprtype.hpp index 1988f7e9..7afa8ff3 100644 --- a/include/xo/expression/exprtype.hpp +++ b/include/xo/expression/exprtype.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; } diff --git a/src/expression/CMakeLists.txt b/src/expression/CMakeLists.txt index 59e754a5..5db806b2 100644 --- a/src/expression/CMakeLists.txt +++ b/src/expression/CMakeLists.txt @@ -4,6 +4,7 @@ set(SELF_LIB xo_expression) set(SELF_SRCS Expression.cpp Apply.cpp + Lambda.cpp #init_reflect.cpp ) diff --git a/src/expression/Lambda.cpp b/src/expression/Lambda.cpp new file mode 100644 index 00000000..d228bb2c --- /dev/null +++ b/src/expression/Lambda.cpp @@ -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 << ""; + } /*display*/ + } /*namespace ast*/ +} /*namespace xo*/ + + +/* end Lambda.cpp */