From 9d2b0f17f84975e51235d87514ad8c2d33e2724d Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 13 Jun 2024 17:59:51 -0400 Subject: [PATCH] xo-expression: + Variable + apply/lambda fixes --- include/xo/expression/Apply.hpp | 2 +- include/xo/expression/Lambda.hpp | 27 +++++++++++++++++-- include/xo/expression/Variable.hpp | 42 ++++++++++++++++++++++++++++++ include/xo/expression/exprtype.hpp | 3 +++ src/expression/CMakeLists.txt | 1 + src/expression/Lambda.cpp | 1 + src/expression/Variable.cpp | 17 ++++++++++++ 7 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 include/xo/expression/Variable.hpp create mode 100644 src/expression/Variable.cpp diff --git a/include/xo/expression/Apply.hpp b/include/xo/expression/Apply.hpp index 56f6b886..1f2b9a8f 100644 --- a/include/xo/expression/Apply.hpp +++ b/include/xo/expression/Apply.hpp @@ -44,7 +44,7 @@ namespace xo { inline ref::rp make_apply(const ref::rp & fn, - const ref::rp & arg1) { + const ref::rp & arg1) { std::vector> argv; argv.push_back(arg1); diff --git a/include/xo/expression/Lambda.hpp b/include/xo/expression/Lambda.hpp index 581ba274..7f6434f8 100644 --- a/include/xo/expression/Lambda.hpp +++ b/include/xo/expression/Lambda.hpp @@ -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 & argv, + Lambda(const std::string & name, + const std::vector & argv, const ref::rp & body) - : Expression(exprtype::lambda), argv_{argv}, body_{body} {} + : Expression(exprtype::lambda), name_{name}, argv_{argv}, body_{body} {} + /** downcast from Expression **/ + static ref::brw from(ref::brw x) { + return ref::brw::from(x); + } + + const std::string & name() const { return name_; } const std::vector & argv() const { return argv_; } const ref::rp & 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 argv_; /** function body **/ ref::rp body_; }; /*Lambda*/ + + inline ref::rp + make_lambda(const std::string & name, + const std::vector & argv, + const ref::rp & body) + { + return new Lambda(name, argv, body); + } } /*namespace ast*/ } /*namespace xo*/ diff --git a/include/xo/expression/Variable.hpp b/include/xo/expression/Variable.hpp new file mode 100644 index 00000000..fc565236 --- /dev/null +++ b/include/xo/expression/Variable.hpp @@ -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 from(ref::brw x) { + return ref::brw::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 + make_var(const std::string & name) { + return new Variable(name); + } + } /*namespace ast*/ +} /*namespace xo*/ + + +/** end Variable.hpp **/ diff --git a/include/xo/expression/exprtype.hpp b/include/xo/expression/exprtype.hpp index 7afa8ff3..91bcfbb5 100644 --- a/include/xo/expression/exprtype.hpp +++ b/include/xo/expression/exprtype.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; } diff --git a/src/expression/CMakeLists.txt b/src/expression/CMakeLists.txt index 5db806b2..0ef351e1 100644 --- a/src/expression/CMakeLists.txt +++ b/src/expression/CMakeLists.txt @@ -5,6 +5,7 @@ set(SELF_SRCS Expression.cpp Apply.cpp Lambda.cpp + Variable.cpp #init_reflect.cpp ) diff --git a/src/expression/Lambda.cpp b/src/expression/Lambda.cpp index d228bb2c..87f70759 100644 --- a/src/expression/Lambda.cpp +++ b/src/expression/Lambda.cpp @@ -8,6 +8,7 @@ namespace xo { void Lambda::display(std::ostream & os) const { os << ""; diff --git a/src/expression/Variable.cpp b/src/expression/Variable.cpp new file mode 100644 index 00000000..c8ab19fc --- /dev/null +++ b/src/expression/Variable.cpp @@ -0,0 +1,17 @@ +/* @file Variable.cpp */ + +#include "Variable.hpp" + +namespace xo { + namespace ast { + void + Variable::display(std::ostream & os) const { + os << ""; + } /*display*/ + } /*namespace ast*/ +} /*namespace xo*/ + + +/* end Variable.cpp */