Revert "remove celltype"

This reverts commit 294fb4f864.
This commit is contained in:
2023-12-29 19:12:13 +01:00
parent 294fb4f864
commit ff76fed17f
4 changed files with 32 additions and 24 deletions

View File

@@ -11,12 +11,19 @@
#include <sstream>
#include <string>
#include <utility>
enum class CellType {
NUMATOM,
STRATOM,
CONS
};
using CellValType = int64_t;
struct Cell {
explicit Cell(CellType type) : _type(type) {}
virtual ~Cell() = 0;
CellType _type;
std::atomic<bool> _live = false;
virtual void print(std::ostream &out) = 0;
@@ -24,7 +31,7 @@ struct Cell {
struct NumAtomCell : public Cell {
NumAtomCell() = delete;
explicit NumAtomCell(CellValType val) : _val(val) {}
explicit NumAtomCell(CellValType val) : Cell(CellType::NUMATOM), _val(val) {}
CellValType _val;
@@ -35,7 +42,7 @@ struct NumAtomCell : public Cell {
struct StrAtomCell : public Cell {
StrAtomCell() = delete;
explicit StrAtomCell(std::string val) : _val(std::move(val)) {}
explicit StrAtomCell(std::string val) : Cell(CellType::STRATOM), _val(std::move(val)) {}
std::string _val;
@@ -45,8 +52,9 @@ struct StrAtomCell : public Cell {
};
struct ConsCell : public Cell {
explicit ConsCell(Cell *car) : _car(car) {}
ConsCell(Cell *car, Cell *cdr) : _car(car), _cdr(cdr) {}
ConsCell() : Cell(CellType::CONS) {}
explicit ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {}
ConsCell(Cell *car, Cell *cdr) : Cell(CellType::CONS), _car(car), _cdr(cdr) {}
std::atomic<Cell *> _car = nullptr;
std::atomic<Cell *> _cdr = nullptr;
@@ -54,7 +62,7 @@ struct ConsCell : public Cell {
void print(std::ostream &out) override {
std::stringstream res;
if (_car) {
if (dynamic_cast<ConsCell *>(_car.load())) {
if (_car.load()->_type == CellType::CONS) {
res << "(";
_car.load()->print(res);
res << ")";
@@ -63,7 +71,7 @@ struct ConsCell : public Cell {
}
}
if (_cdr) {
if (dynamic_cast<ConsCell *>(_cdr.load())) {
if (_cdr.load()->_type == CellType::CONS) {
res << " ";
_cdr.load()->print(res);
} else {

View File

@@ -24,11 +24,15 @@ public:
bool operator==(const Handle &rhs) const {
if (_target == rhs._target) {
return true;
} else if ((_target != nullptr && rhs._target != nullptr) && (typeid(_target) == typeid(rhs._target))) {
if (auto p = dynamic_cast<NumAtomCell *>(_target)) {
} else if ((_target != nullptr && rhs._target != nullptr) && (_target->_type == rhs._target->_type)) {
if (_target->_type == CellType::NUMATOM) {
return dynamic_cast<NumAtomCell &>(*_target)._val == dynamic_cast<NumAtomCell &>(*rhs._target)._val;
} else if (auto p = dynamic_cast<StrAtomCell *>(_target)) {
} else if (_target->_type == CellType::STRATOM) {
return dynamic_cast<StrAtomCell &>(*_target)._val == dynamic_cast<StrAtomCell &>(*rhs._target)._val;
} else if (_target->_type == CellType::CONS) {
// This is questionable
return dynamic_cast<ConsCell &>(*_target)._car == dynamic_cast<ConsCell &>(*rhs._target)._car &&
dynamic_cast<ConsCell &>(*_target)._cdr == dynamic_cast<ConsCell &>(*rhs._target)._cdr;
}
}
return false;
@@ -41,23 +45,18 @@ public:
CellValType val() { return dynamic_cast<NumAtomCell &>(*_target)._val; }
std::string_view strval() { return dynamic_cast<StrAtomCell &>(*_target)._val; }
bool num() const {
return dynamic_cast<NumAtomCell *>(_target) != nullptr;
}
bool str() const {
return dynamic_cast<StrAtomCell *>(_target) != nullptr;
CellType type() const {
if (!_target) return CellType::CONS;
return _target->_type;
}
bool atom() const {
if (!_target) return false;
return dynamic_cast<ConsCell *>(_target) == nullptr;
return type() != CellType::CONS;
}
bool null() {
if (!_target) return true;
if (auto p = dynamic_cast<ConsCell *>(_target)) {
return car() == nullptr && cdr() == nullptr;
}
if (type() == CellType::CONS && car() == nullptr && cdr() == nullptr) return true;
return false;
}

View File

@@ -34,10 +34,10 @@ Handle Compiler::compile(Handle src, Handle fake_env, Handle suffix) {
if (expr.null()) {
out.append(Handle("NIL"));
} else if (expr.atom()) {
if (expr.num()) {
if (expr.type() == CellType::NUMATOM) {
out.append(Handle("LDC"));
out.append(expr);
} else if (expr.str()) {
} else if (expr.type() == CellType::STRATOM) {
Handle idx = findIndex(expr, fake_env);
if (idx == nullptr) {
out.append(Handle("LDC"));

View File

@@ -87,12 +87,13 @@ void MemoryContext::gc_thread_entry() {
// std::cerr << out.str();
// }
if (auto cc = dynamic_cast<ConsCell *>(c)) {
if (c->_type == CellType::CONS) {
ConsCell &cc = dynamic_cast<ConsCell &>(*c);
// {std::stringstream out; out << "processing car " << cc._car << "\n"; std::cerr<<out.str();}
toVisit.emplace(cc->_car);
toVisit.emplace(cc._car);
// {std::stringstream out; out << "real car " << toVisit.back() << "\n"; std::cerr<<out.str();}
// {std::stringstream out; out << "processing cdr " << cc._cdr << "\n"; std::cerr<<out.str();}
toVisit.emplace(cc->_cdr);
toVisit.emplace(cc._cdr);
// {std::stringstream out; out << "real cdr " << toVisit.back() << "\n"; std::cerr<<out.str();}
}
}