stratom numatom

This commit is contained in:
2023-12-26 12:04:16 +01:00
parent 4513ec82aa
commit a45494d861
4 changed files with 40 additions and 6 deletions

View File

@@ -7,9 +7,12 @@
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <string>
#include <utility>
enum class CellType { enum class CellType {
INT, NUMATOM,
STRATOM,
CONS CONS
}; };
@@ -23,13 +26,20 @@ struct Cell {
bool live = false; bool live = false;
}; };
struct ValueCell : public Cell { struct NumAtomCell : public Cell {
ValueCell() = delete; NumAtomCell() = delete;
explicit ValueCell(CellValType val) : Cell(CellType::INT), _val(val) {} explicit NumAtomCell(CellValType val) : Cell(CellType::NUMATOM), _val(val) {}
CellValType _val; CellValType _val;
}; };
struct StrAtomCell : public Cell {
StrAtomCell() = delete;
explicit StrAtomCell(std::string val) : Cell(CellType::STRATOM), _val(std::move(val)) {}
std::string _val;
};
struct ConsCell : public Cell { struct ConsCell : public Cell {
ConsCell() : Cell(CellType::CONS) {} ConsCell() : Cell(CellType::CONS) {}
explicit ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {} explicit ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {}

View File

@@ -11,7 +11,7 @@
namespace ConsUtils { namespace ConsUtils {
static inline MCHandle car(const MCHandle &cell) { return dynamic_cast<ConsCell &>(*cell)._car; } static inline MCHandle car(const MCHandle &cell) { return dynamic_cast<ConsCell &>(*cell)._car; }
static inline MCHandle cdr(const MCHandle &cell) { return dynamic_cast<ConsCell &>(*cell)._cdr; } static inline MCHandle cdr(const MCHandle &cell) { return dynamic_cast<ConsCell &>(*cell)._cdr; }
static inline CellValType val(const MCHandle &cell) { return dynamic_cast<ValueCell &>(*cell)._val; } static inline CellValType val(const MCHandle &cell) { return dynamic_cast<NumAtomCell &>(*cell)._val; }
MCHandle cons(const MCHandle &car, const MCHandle &cdr); MCHandle cons(const MCHandle &car, const MCHandle &cdr);
MCHandle pop(MCHandle &from); MCHandle pop(MCHandle &from);
MCHandle push(MCHandle &to, const MCHandle &what); MCHandle push(MCHandle &to, const MCHandle &what);

View File

@@ -24,6 +24,28 @@ public:
return cell; return cell;
} }
template<>
Handle create_cell<NumAtomCell>(CellValType val) {
if (numatom_index.contains(val))
return numatom_index.at(val);
NumAtomCell *cell = new NumAtomCell(val);
_cells.emplace_back(cell);
numatom_index.emplace(val, cell);
return cell;
}
template<>
Handle create_cell<StrAtomCell>(std::string val) {
if (stratom_index.contains(val))
return stratom_index.at(val);
StrAtomCell *cell = new StrAtomCell(std::move(val));
_cells.emplace_back(cell);
stratom_index.emplace(cell->_val, cell);
return cell;
}
class Handle { class Handle {
public: public:
Handle(Cell *target) : _target(target) {} Handle(Cell *target) : _target(target) {}
@@ -38,6 +60,8 @@ public:
private: private:
std::list<Cell *> _cells; std::list<Cell *> _cells;
std::unordered_map<CellValType, Cell *> numatom_index;
std::unordered_map<std::string, Cell *> stratom_index;
}; };
using MCHandle = MemoryContext::Handle; using MCHandle = MemoryContext::Handle;

View File

@@ -33,7 +33,7 @@ void ConsUtils::append(MCHandle to, const MCHandle &what) {
} }
MCHandle ConsUtils::makeIntCell(int64_t val) { MCHandle ConsUtils::makeIntCell(int64_t val) {
return CURRENT_MC.load()->create_cell<ValueCell>(val); return CURRENT_MC.load()->create_cell<NumAtomCell>(val);
} }
void ConsUtils::setcar(const MCHandle &to, const MCHandle &car) { void ConsUtils::setcar(const MCHandle &to, const MCHandle &car) {