xo-interpreter: + Env + LocalEnv + GlobalEnv scaffold
This commit is contained in:
parent
75f5aa91a6
commit
85bfd34c0d
7 changed files with 518 additions and 2 deletions
|
|
@ -18,7 +18,7 @@ namespace xo {
|
|||
**/
|
||||
class Env : public Object {
|
||||
public:
|
||||
//gp<Object> lookup_symbol(xxx);
|
||||
//gp<Object> lookup_symbol(const std::string & name) const;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
57
xo-interpreter/include/xo/interpreter/GlobalEnv.hpp
Normal file
57
xo-interpreter/include/xo/interpreter/GlobalEnv.hpp
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/** @file GlobalEnv.hpp **/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Env.hpp"
|
||||
#include "xo/alloc/IAlloc.hpp"
|
||||
#include "xo/expression/GlobalSymtab.hpp"
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** @class GlobalEnv
|
||||
* @brief Top-level global environment
|
||||
**/
|
||||
class GlobalEnv : public Env {
|
||||
public:
|
||||
/** Create top-level global environment, allocating via @p mm.
|
||||
* Expect one of these per interpreter session.
|
||||
**/
|
||||
static gp<GlobalEnv> make_empty(gc::IAlloc * mm, const rp<GlobalSymtab> & symtab);
|
||||
|
||||
// inherited from Object..
|
||||
virtual TaggedPtr self_tp() const final override;
|
||||
virtual void display(std::ostream & os) const final override;
|
||||
virtual std::size_t _shallow_size() const final override;
|
||||
virtual Object * _shallow_copy() const final override;
|
||||
virtual std::size_t _forward_children() final override;
|
||||
|
||||
private:
|
||||
GlobalEnv(gc::IAlloc * mm, const rp<GlobalSymtab> & symtab);
|
||||
|
||||
private:
|
||||
/** memory manager to use **/
|
||||
gc::IAlloc * mm_;
|
||||
|
||||
/** global symbol table.
|
||||
* variables known to @c symtab_ are represented by
|
||||
* corresponding values in @p slot_map_
|
||||
**/
|
||||
rp<GlobalSymtab> symtab_;
|
||||
|
||||
/** environment contents.
|
||||
* expression @c symtab_->lookup_binding(vname)
|
||||
* has associated value @c slot_map_.at(vname)
|
||||
*
|
||||
* TODO: replace with something subject to GC ?
|
||||
* every member of @ref slot_map_ will have to be a
|
||||
* GC root
|
||||
*
|
||||
* TODO: probably want to hash here instead.
|
||||
* May also want lhs names to be separately hashed symbols
|
||||
**/
|
||||
std::map<std::string, gp<Object>> slot_map_;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end GlobalEnv.hpp */
|
||||
113
xo-interpreter/include/xo/interpreter/LocalEnv.hpp
Normal file
113
xo-interpreter/include/xo/interpreter/LocalEnv.hpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/** @file LocalEnv.hpp **/
|
||||
|
||||
#include "Env.hpp"
|
||||
#include "xo/alloc/IAlloc.hpp"
|
||||
#include "xo/expression/LocalSymtab.hpp"
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace xo {
|
||||
namespace scm {
|
||||
/** gc-only vector
|
||||
**/
|
||||
template <typename ElementType>
|
||||
class CVector {
|
||||
public:
|
||||
using value_type = ElementType;
|
||||
|
||||
public:
|
||||
CVector(gc::IAlloc * mm, std::size_t n)
|
||||
: n_{n}, v_{nullptr}
|
||||
{
|
||||
if (n_ > 0) {
|
||||
std::byte * mem = mm->alloc(n_ * sizeof(ElementType));
|
||||
this->v_ = new (mem) ElementType[n];
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t size() const { return n_; }
|
||||
|
||||
ElementType operator[](std::size_t i) const { return v_[i]; }
|
||||
ElementType & operator[](std::size_t i) { return v_[i]; }
|
||||
|
||||
friend class LocalEnv;
|
||||
private:
|
||||
/** number of elements in @ref v_ **/
|
||||
std::size_t n_ = 0;
|
||||
/** contiguous array of pointers **/
|
||||
ElementType * v_ = nullptr;
|
||||
};
|
||||
|
||||
/** @class LocalEnv
|
||||
* @brief Represent a single runtime stack frame for a Schematika function
|
||||
*
|
||||
* LocalEnv intended to be used for interpreted functions.
|
||||
*
|
||||
* Compiled functions will still likely have stack frames, but need not use the
|
||||
* @ref LocalEnv class
|
||||
*
|
||||
* memory layout:
|
||||
* ^
|
||||
* +-----------------------+ |
|
||||
* | vtable | |
|
||||
* +-----------------------+ |
|
||||
* | .parent +------/
|
||||
* +------------+----------+
|
||||
* | .slot_v_ | .n_ |
|
||||
* | +----------+
|
||||
* | | .v_ +------\
|
||||
* +------------+----------+ <--/
|
||||
* | .v_[0] +---------> Object(1)
|
||||
* +-----------------------+
|
||||
* . .. .
|
||||
* +-----------------------+
|
||||
* | .v_[.n_-1] +---------> Object(n)
|
||||
* +-----------------------+
|
||||
**/
|
||||
class LocalEnv : public Env {
|
||||
public:
|
||||
using TaggedPtr = xo::reflect::TaggedPtr;
|
||||
|
||||
public:
|
||||
LocalEnv(gc::IAlloc * mm, gp<LocalEnv> p, const rp<LocalSymtab> & s, std::size_t n);
|
||||
|
||||
/** create frame using allocator @p mm,
|
||||
* with parent @p p and exactly @p n_slot object pointers.
|
||||
* variable types are taken from symbol table @p s.
|
||||
**/
|
||||
static gp<LocalEnv> make(gc::IAlloc * mm,
|
||||
gp<LocalEnv> p,
|
||||
const rp<LocalSymtab> & s,
|
||||
std::size_t n_slot);
|
||||
|
||||
/** reflect LocalEnv object representation **/
|
||||
static void reflect_self();
|
||||
|
||||
gp<LocalEnv> parent() const { return parent_; }
|
||||
std::size_t size() const { return slot_v_.size(); }
|
||||
|
||||
gp<Object> operator[](std::size_t i) const { return slot_v_[i]; }
|
||||
gp<Object> & operator[](std::size_t i) { return slot_v_[i]; }
|
||||
|
||||
// inherited from Object..
|
||||
virtual TaggedPtr self_tp() const final override;
|
||||
virtual void display(std::ostream & os) const final override;
|
||||
virtual std::size_t _shallow_size() const final override;
|
||||
virtual Object * _shallow_copy() const final override;
|
||||
virtual std::size_t _forward_children() final override;
|
||||
|
||||
private:
|
||||
/** parent stack frame **/
|
||||
gp<LocalEnv> parent_;
|
||||
/** origin symbol table. records variable names and bindings.
|
||||
* for a binding path p with leaf slot index j = p.j_slot_:
|
||||
* @c slot_v_[j] holds value associated with variable @c symtab_->argv_[j]
|
||||
**/
|
||||
rp<LocalSymtab> symtab_;
|
||||
/** environment contents **/
|
||||
CVector<gp<Object>> slot_v_;
|
||||
};
|
||||
} /*namespace scm*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/* end LocalEnv.hpp */
|
||||
Loading…
Add table
Add a link
Reference in a new issue