xo-reader: wip: + envframe, envframestack [not used]

This commit is contained in:
Roland Conybeare 2024-08-19 17:28:35 -04:00
commit 8d495a6427
5 changed files with 221 additions and 2 deletions

View file

@ -0,0 +1,48 @@
/* file envframe.hpp
*
* author: Roland Conybeare, Aug 2024
*/
#pragma once
#include "xo/expression/Variable.hpp"
#include <vector>
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<rp<Variable>> & argl) : argl_(argl) {}
const std::vector<rp<Variable>> & argl() const { return argl_; }
/** lookup variable by name. If found, return it.
* Otherwise return nullptr
**/
rp<Variable> lookup(const std::string & name) const;
void print (std::ostream & os) const;
private:
std::vector<rp<Variable>> argl_;
};
inline std::ostream &
operator<< (std::ostream & os, const envframe & x) {
x.print(os);
return os;
}
} /*namespace scm*/
} /*namespace xo*/
/* end envframe.hpp */

View file

@ -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<envframe> 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 */

View file

@ -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)

34
src/reader/envframe.cpp Normal file
View file

@ -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<Variable>
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 << "<envframe"
<< xtag("argl", argl_)
<< ">";
}
} /*namespace scm */
} /*namespace xo*/
/* end envframe.cpp */

View file

@ -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<exprstate> top = std::move(stack_[z-1]);
stack_.resize(z-1);
//return top;
} else {
//return nullptr;
}
}
void
envframestack::print(std::ostream & os) const {
os << "<envframestack"
<< xtag("size", stack_.size())
<< std::endl;
for (std::size_t i = 0, z = stack_.size(); i < z; ++i) {
os << " [" << z-i-1 << "] "
<< stack_[i]
<< std::endl;
}
os << ">" << std::endl;
}
} /*namespace scm*/
} /*namespace xo*/
/* end envframestack.cpp */