refactor memory

This commit is contained in:
2023-12-26 00:17:41 +01:00
parent 0ccb7e9b56
commit 58f56f158d
18 changed files with 620 additions and 532 deletions

View File

@@ -9,66 +9,29 @@
#include <cstdint>
enum class CellType {
NIL,
INT,
CONS
};
struct Cell {
explicit Cell(CellType type) : _type(type) {}
virtual ~Cell() = 0;
CellType _type;
bool live = false;
};
struct IntCell : public Cell {
IntCell() : Cell(CellType::INT) {}
struct ValueCell : public Cell {
ValueCell() = delete;
explicit ValueCell(int64_t val) : Cell(CellType::INT), _val(val) {}
IntCell(int64_t val) : Cell(CellType::INT), _val(val) {}
int64_t _val{};
};
struct CommandCell : public IntCell {
enum class CommandNum {
NIL = 1,
LDC = 2,
LD = 3,
SEL = 4,
JOIN = 5,
LDF = 6,
AP = 7,
RET = 8,
DUM = 9,
RAP = 10,
STOP = 11,
ADD = 100,
SUB = 101,
READCHAR = 201,
PUTCHAR = 202,
PUTNUM = 203,
CONS = 301,
END = 1000
};
CommandCell(CommandNum cmd) : IntCell(static_cast<int64_t>(cmd)) {}
CommandNum intToCmd() {
assert((_val > 0 && static_cast<CommandNum>(_val) <= CommandNum::END));
return static_cast<CommandNum>(_val);
}
int64_t _val;
};
struct ConsCell : public Cell {
ConsCell() : Cell(CellType::CONS) {}
ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {}
explicit ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {}
ConsCell(Cell *car, Cell *cdr) : Cell(CellType::CONS), _car(car), _cdr(cdr) {}
Cell *_car = nullptr;