From 8d495a642724d3fab6c6176687d98ff250000ab4 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 19 Aug 2024 17:28:35 -0400 Subject: [PATCH] xo-reader: wip: + envframe, envframestack [not used] --- include/xo/reader/envframe.hpp | 48 ++++++++++++++++++++ include/xo/reader/envframestack.hpp | 66 +++++++++++++++++++++++++++ src/reader/CMakeLists.txt | 5 ++- src/reader/envframe.cpp | 34 ++++++++++++++ src/reader/envframestack.cpp | 70 +++++++++++++++++++++++++++++ 5 files changed, 221 insertions(+), 2 deletions(-) create mode 100644 include/xo/reader/envframe.hpp create mode 100644 include/xo/reader/envframestack.hpp create mode 100644 src/reader/envframe.cpp create mode 100644 src/reader/envframestack.cpp diff --git a/include/xo/reader/envframe.hpp b/include/xo/reader/envframe.hpp new file mode 100644 index 00000000..a0fcfb7d --- /dev/null +++ b/include/xo/reader/envframe.hpp @@ -0,0 +1,48 @@ +/* file envframe.hpp + * + * author: Roland Conybeare, Aug 2024 + */ + +#pragma once + +#include "xo/expression/Variable.hpp" +#include + +namespace xo { + namespace scm { + /** @class envframe + * @brief names/types of formal paremeters introduced by a function + * + * + **/ + class envframe { + public: + using Variable = xo::ast::Variable; + + public: + envframe() = default; + envframe(const std::vector> & argl) : argl_(argl) {} + + const std::vector> & argl() const { return argl_; } + + /** lookup variable by name. If found, return it. + * Otherwise return nullptr + **/ + rp lookup(const std::string & name) const; + + void print (std::ostream & os) const; + + private: + std::vector> argl_; + }; + + inline std::ostream & + operator<< (std::ostream & os, const envframe & x) { + x.print(os); + return os; + } + } /*namespace scm*/ +} /*namespace xo*/ + + +/* end envframe.hpp */ diff --git a/include/xo/reader/envframestack.hpp b/include/xo/reader/envframestack.hpp new file mode 100644 index 00000000..d27713ac --- /dev/null +++ b/include/xo/reader/envframestack.hpp @@ -0,0 +1,66 @@ +/* file envframestack.hpp + * + * author: Roland Conybeare, Aug 2024 + */ + +#pragma once + +#include "envframe.hpp" + +namespace xo { + namespace scm { + /** @class envframestack + * @brief A stack of envframe objects + **/ + class envframestack { + public: + envframestack() {} + + bool empty() const { return stack_.empty(); } + std::size_t size() const { return stack_.size(); } + + envframe & top_envframe(); + void push_envframe(envframe x); + void pop_envframe(); + + /** relative to top-of-stack. + * 0 -> top (last in), z-1 -> bottom (first in) + **/ + envframe & operator[](std::size_t i) { + std::size_t z = stack_.size(); + + assert(i < z); + + return stack_[z - i - 1]; + } + + const envframe & operator[](std::size_t i) const { + std::size_t z = stack_.size(); + + assert(i < z); + + return stack_[z - i - 1]; + } + + void print (std::ostream & os) const; + + private: + std::vector stack_; + }; + + inline std::ostream & + operator<< (std::ostream & os, const envframestack & x) { + x.print(os); + return os; + } + + inline std::ostream & + operator<< (std::ostream & os, const envframestack * x) { + x->print(os); + return os; + } + } /*namespace scm*/ +} /*namespace xo*/ + + +/* end envframestack.hpp */ diff --git a/src/reader/CMakeLists.txt b/src/reader/CMakeLists.txt index 5edbb2e4..83dc675d 100644 --- a/src/reader/CMakeLists.txt +++ b/src/reader/CMakeLists.txt @@ -13,11 +13,12 @@ set(SELF_SRCS exprseq_xs.cpp expect_expr_xs.cpp expect_symbol_xs.cpp - expect_formal_xs.cpp expect_formal_arglist_xs.cpp expect_type_xs.cpp - lambda_xs.cpp) + lambda_xs.cpp + envframestack.cpp + envframe.cpp) xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS}) xo_dependency(${SELF_LIB} xo_expression) diff --git a/src/reader/envframe.cpp b/src/reader/envframe.cpp new file mode 100644 index 00000000..38ff1fcb --- /dev/null +++ b/src/reader/envframe.cpp @@ -0,0 +1,34 @@ +/* file envframe.cpp + * + * author: Roland Conybeare + */ + +#include "envframe.hpp" +#include "xo/indentlog/print/vector.hpp" + +namespace xo { + using xo::ast::Variable; + + namespace scm { + rp + envframe::lookup(const std::string & x) const { + for (const auto & var : argl_) { + if (x == var->name()) + return var; + } + + return nullptr; + } + + void + envframe::print(std::ostream & os) const { + os << ""; + } + + } /*namespace scm */ +} /*namespace xo*/ + + +/* end envframe.cpp */ diff --git a/src/reader/envframestack.cpp b/src/reader/envframestack.cpp new file mode 100644 index 00000000..52543dc4 --- /dev/null +++ b/src/reader/envframestack.cpp @@ -0,0 +1,70 @@ +/* file envframestack.cpp + * + * author: Roland Conybeare + */ + +#include "envframestack.hpp" + +namespace xo { + namespace scm { + envframe & + envframestack::top_envframe() { + std::size_t z = stack_.size(); + + if (z == 0) { + throw std::runtime_error + ("parser::top_exprstate: unexpected empty stack"); + } + + return stack_[z-1]; + } + + void + envframestack::push_envframe(envframe frame) { + constexpr bool c_debug_flag = true; + scope log(XO_DEBUG(c_debug_flag), + xtag("frame", frame)); + + std::size_t z = stack_.size(); + + stack_.resize(z+1); + + stack_[z] = std::move(frame); + } + + void + envframestack::pop_envframe() { + constexpr bool c_debug_flag = true; + scope log(XO_DEBUG(c_debug_flag)); + + std::size_t z = stack_.size(); + + if (z > 0) { + //std::unique_ptr top = std::move(stack_[z-1]); + + stack_.resize(z-1); + + //return top; + } else { + //return nullptr; + } + } + + void + envframestack::print(std::ostream & os) const { + os << "" << std::endl; + } + } /*namespace scm*/ +} /*namespace xo*/ + +/* end envframestack.cpp */