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

5
.idea/misc.xml generated
View File

@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<excludeRoots>
<file path="$PROJECT_DIR$/cmake-build-debug" />
</excludeRoots>
</component>
</project> </project>

View File

@@ -1,5 +1,5 @@
#include "VM.h"
#include <iostream> #include <iostream>
#include "vm/includes/VM.h"
int main() { int main() {
return 0; return 0;

View File

@@ -2,6 +2,6 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(vm src/VM.cpp src/Cell.cpp add_library(vm src/VM.cpp src/Cell.cpp
src/Parser.cpp) src/Parser.cpp src/SParser.cpp src/MemoryContext.cpp src/ConsUtils.cpp)
target_include_directories(vm PUBLIC includes) target_include_directories(vm PUBLIC includes)

View File

@@ -9,66 +9,29 @@
#include <cstdint> #include <cstdint>
enum class CellType { enum class CellType {
NIL,
INT, INT,
CONS CONS
}; };
struct Cell { struct Cell {
explicit Cell(CellType type) : _type(type) {} explicit Cell(CellType type) : _type(type) {}
virtual ~Cell() = 0; virtual ~Cell() = 0;
CellType _type; CellType _type;
bool live = false; bool live = false;
}; };
struct IntCell : public Cell { struct ValueCell : public Cell {
IntCell() : Cell(CellType::INT) {} ValueCell() = delete;
explicit ValueCell(int64_t val) : Cell(CellType::INT), _val(val) {}
IntCell(int64_t val) : Cell(CellType::INT), _val(val) {} int64_t _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);
}
}; };
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) {}
ConsCell(Cell *car) : Cell(CellType::CONS), _car(car) {}
ConsCell(Cell *car, Cell *cdr) : Cell(CellType::CONS), _car(car), _cdr(cdr) {} ConsCell(Cell *car, Cell *cdr) : Cell(CellType::CONS), _car(car), _cdr(cdr) {}
Cell *_car = nullptr; Cell *_car = nullptr;

41
src/vm/includes/Command.h Normal file
View File

@@ -0,0 +1,41 @@
#ifndef PSIL_COMMAND_H
#define PSIL_COMMAND_H
#include <cassert>
namespace Command {
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
};
static inline CommandNum int_to_cmd(int64_t _val) {
assert((_val > 0 && static_cast<CommandNum>(_val) <= CommandNum::END));
return static_cast<CommandNum>(_val);
}
static inline int64_t cmd_to_int(CommandNum _val) {
return static_cast<int64_t>(_val);
}
};// namespace Command
#endif

View File

@@ -0,0 +1,22 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#ifndef PSIL_CONSUTILS_H
#define PSIL_CONSUTILS_H
#include "Cell.h"
#include "MemoryContext.h"
namespace ConsUtils {
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; }
MCHandle cons(const MCHandle &car, const MCHandle &cdr);
MCHandle pop(MCHandle &from);
MCHandle push(MCHandle &to, const MCHandle &what);
void append(MCHandle to, const MCHandle &what);
MCHandle makeIntCell(int64_t val);
};// namespace ConsUtils
#endif//PSIL_CONSUTILS_H

View File

@@ -0,0 +1,48 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#ifndef PSIL_MEMORYCONTEXT_H
#define PSIL_MEMORYCONTEXT_H
#include <atomic>
#include <list>
#include "Cell.h"
class MemoryContext {
public:
class Handle;
MemoryContext();
~MemoryContext();
template<typename CT, typename... Args>
Handle create_cell(Args... args) {
CT *cell = new CT(std::forward<Args>(args)...);
_cells.emplace_back(cell);
return cell;
}
class Handle {
public:
Handle(Cell *target) : _target(target), _type(target ? target->_type : CellType::NIL) {}
Cell *operator->() const { return _target; }
Cell &operator*() const { return *_target; }
Cell *get() const noexcept { return _target; }
private:
Cell *_target = nullptr;
CellType _type;
};
private:
std::list<Cell *> _cells;
};
using MCHandle = MemoryContext::Handle;
extern std::atomic<MemoryContext *> CURRENT_MC;
#endif//PSIL_MEMORYCONTEXT_H

View File

@@ -5,9 +5,12 @@
#ifndef PSIL_PARSER_H #ifndef PSIL_PARSER_H
#define PSIL_PARSER_H #define PSIL_PARSER_H
#include <string_view>
#include <string>
#include <queue> #include <queue>
#include <string>
#include <string_view>
#include "MemoryContext.h"
class VM; class VM;
@@ -20,8 +23,7 @@ public:
void loadSecd(std::string_view input); void loadSecd(std::string_view input);
private: private:
void compileBody(const std::function<void(MCHandle)> &sink);
void compileBody(const std::function<void(Cell *)> &sink);
VM &_vm; VM &_vm;
@@ -43,4 +45,4 @@ private:
}; };
#endif //PSIL_PARSER_H #endif//PSIL_PARSER_H

14
src/vm/includes/SParser.h Normal file
View File

@@ -0,0 +1,14 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#ifndef PSIL_SPARSER_H
#define PSIL_SPARSER_H
class SParser {
// SParser()
};
#endif//PSIL_SPARSER_H

View File

@@ -11,6 +11,7 @@
#include <vector> #include <vector>
#include "Cell.h" #include "Cell.h"
#include "MemoryContext.h"
class VM { class VM {
public: public:
@@ -18,61 +19,21 @@ public:
void run(); void run();
void loadControl(const MCHandle &h) { _c = h; }
void step(); void step();
// template<typename T>
// void appendCommand(T cell) {
// push(_c, makeCell<T>(std::move(cell)));
// }
template<typename T>
void appendCommand(T *cell) {
push(_c, cell);
}
template<typename CT, typename... Args>
CT *makeCell(Args... args) {
return static_cast<CT *>(_cells.emplace_back(new CT(std::forward<Args>(args)...)));
}
Cell *car(ConsCell *cell) {
return cell->_car;
}
Cell *cdr(ConsCell *cell) {
return cell->_cdr;
}
ConsCell *cons(Cell *car, Cell *cdr) {
return dynamic_cast<ConsCell *>(makeCell<ConsCell>(car, cdr));
}
Cell *pop(ConsCell *&what) {
Cell *ret = what->_car;
what = dynamic_cast<ConsCell *>(cdr(what));
return ret;
}
Cell *push(ConsCell *&what, Cell *toAppend) {
what = cons(toAppend, what);
return what;
}
uint64_t cellCount() const;
private: private:
std::list<Cell *> _cells; MCHandle _s = nullptr;
ConsCell *_s = nullptr; MCHandle _e = nullptr;
ConsCell *_e = nullptr; MCHandle _c = nullptr;
ConsCell *_c = nullptr; MCHandle _d = nullptr;
ConsCell *_d = nullptr;
bool _stop = false; bool _stop = false;
std::istream &_instream; std::istream &_instream;
std::ostream &_outstream; std::ostream &_outstream;
void gc(); // void gc();
}; };
#endif//PSIL_VM_H #endif//PSIL_VM_H

View File

@@ -2,6 +2,6 @@
// Created by Stepan Usatiuk on 22.12.2023. // Created by Stepan Usatiuk on 22.12.2023.
// //
#include "../includes/Cell.h" #include "Cell.h"
Cell::~Cell() = default; Cell::~Cell() = default;

37
src/vm/src/ConsUtils.cpp Normal file
View File

@@ -0,0 +1,37 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#include "ConsUtils.h"
#include "MemoryContext.h"
MCHandle ConsUtils::cons(const MCHandle &car, const MCHandle &cdr) {
auto ret = CURRENT_MC.load()->create_cell<ConsCell>(car.get(), cdr.get());
return ret;
}
MCHandle ConsUtils::pop(MCHandle &from) {
auto ret = car(from);
from = cdr(from);
return ret;
}
MCHandle ConsUtils::push(MCHandle &to, const MCHandle &what) {
to = cons(what, to);
return to;
}
void ConsUtils::append(MCHandle to, const MCHandle &what) {
assert(to.get() != nullptr);
if (car(to).get() == nullptr) {
dynamic_cast<ConsCell &>(*to)._car = what.get();
return;
}
while (dynamic_cast<ConsCell &>(*to)._cdr != nullptr) to = cdr(to);
dynamic_cast<ConsCell &>(*to)._cdr = cons(what, nullptr).get();
}
MCHandle ConsUtils::makeIntCell(int64_t val) {
return CURRENT_MC.load()->create_cell<ValueCell>(val);
}

View File

@@ -0,0 +1,23 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#include "MemoryContext.h"
#include <exception>
#include <iostream>
std::atomic<MemoryContext *> CURRENT_MC = nullptr;
MemoryContext::MemoryContext() {
MemoryContext *expected = nullptr;
if (!CURRENT_MC.compare_exchange_strong(expected, this)) throw std::runtime_error("MC already exists!");
}
MemoryContext::~MemoryContext() {
MemoryContext *expected = this;
if (!CURRENT_MC.compare_exchange_strong(expected, nullptr)) {
std::cerr << "Global MC pointer was overwritten!" << std::endl;
std::abort();
}
}

View File

@@ -7,23 +7,23 @@
#include <ranges> #include <ranges>
#include <stack> #include <stack>
#include "Command.h"
#include "ConsUtils.h"
#include "MemoryContext.h"
#include "VM.h" #include "VM.h"
Parser::Parser(VM &vm) : _vm(vm) {} Parser::Parser(VM &vm) : _vm(vm) {}
void Parser::loadSecd(std::string_view input) { void Parser::loadSecd(std::string_view input) {
_tokenizer.load(input); _tokenizer.load(input);
std::stack<Cell *> out;
compileBody([&out](Cell *cmd) { out.emplace(cmd); }); MCHandle out(ConsUtils::cons(nullptr, nullptr));
while (!out.empty()) { compileBody([&out](MCHandle cmd) { ConsUtils::append(out, cmd); });
this->_vm.appendCommand(out.top()); _vm.loadControl(out);
out.pop();
}
} }
void Parser::compileBody(const std::function<void(Cell *)> &sink) { void Parser::compileBody(const std::function<void(MCHandle)> &sink) {
auto token = _tokenizer.getNext(); auto token = _tokenizer.getNext();
if (token != "(") throw std::invalid_argument("Expected ("); if (token != "(") throw std::invalid_argument("Expected (");
@@ -31,84 +31,62 @@ void Parser::compileBody(const std::function<void(Cell *)> &sink) {
token = _tokenizer.getNext(); token = _tokenizer.getNext();
if (token == "NIL") { if (token == "NIL") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
} else if (token == "LDC") { } else if (token == "LDC") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
sink(_vm.makeCell<IntCell>(std::stoi(_tokenizer.getNext()))); sink(ConsUtils::makeIntCell(std::stoi(_tokenizer.getNext())));
} else if (token == "LD") { } else if (token == "LD") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
if (_tokenizer.getNext() != "(") throw std::invalid_argument("Expected ("); if (_tokenizer.getNext() != "(") throw std::invalid_argument("Expected (");
int64_t frame = std::stoi(_tokenizer.getNext()); int64_t frame = std::stoi(_tokenizer.getNext());
if (_tokenizer.getNext() != ".") throw std::invalid_argument("Expected ."); if (_tokenizer.getNext() != ".") throw std::invalid_argument("Expected .");
int64_t loc = std::stoi(_tokenizer.getNext()); int64_t loc = std::stoi(_tokenizer.getNext());
if (_tokenizer.getNext() != ")") throw std::invalid_argument("Expected )"); if (_tokenizer.getNext() != ")") throw std::invalid_argument("Expected )");
sink(_vm.makeCell<ConsCell>(_vm.makeCell<IntCell>(frame), _vm.makeCell<IntCell>(loc))); sink(ConsUtils::cons(ConsUtils::makeIntCell(frame), ConsUtils::makeIntCell(loc)));
} else if (token == "SEL") { } else if (token == "SEL") {
std::stack<Cell *> outt; MCHandle outt(ConsUtils::cons(nullptr, nullptr));
compileBody([&outt](Cell *cmd) { outt.emplace(cmd); }); compileBody([&outt](MCHandle cmd) { ConsUtils::append(outt, cmd); });
std::stack<Cell *> outf; MCHandle outf(ConsUtils::cons(nullptr, nullptr));
compileBody([&outf](Cell *cmd) { outf.emplace(cmd); }); compileBody([&outf](MCHandle cmd) { ConsUtils::append(outf, cmd);; });
if (outt.empty()) throw std::invalid_argument("Function body empty"); if (ConsUtils::car(outt).get() == nullptr) throw std::invalid_argument("Function body empty");
if (outf.empty()) throw std::invalid_argument("Function body empty"); if (ConsUtils::cdr(outt).get() == nullptr) throw std::invalid_argument("Function body empty");
ConsCell *ttop = _vm.makeCell<ConsCell>(outt.top()); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
outt.pop(); sink(outt);
while (!outt.empty()) { sink(outf);
_vm.push(ttop, outt.top());
outt.pop();
}
ConsCell *ftop = _vm.makeCell<ConsCell>(outf.top());
outf.pop();
while (!outf.empty()) {
_vm.push(ftop, outf.top());
outf.pop();
}
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL));
sink(ttop);
sink(ftop);
} else if (token == "JOIN") { } else if (token == "JOIN") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
} else if (token == "LDF") { } else if (token == "LDF") {
std::stack<Cell *> out; MCHandle outt(ConsUtils::cons(nullptr, nullptr));
compileBody([&outt](MCHandle cmd) { ConsUtils::append(outt, cmd); });
compileBody([&out](Cell *cmd) { out.emplace(cmd); }); if (ConsUtils::car(outt).get() == nullptr) throw std::invalid_argument("Function body empty");
if (out.empty()) throw std::invalid_argument("Function body empty"); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
sink(outt);
ConsCell *fntop = _vm.makeCell<ConsCell>(out.top());
out.pop();
while (!out.empty()) {
_vm.push(fntop, out.top());
out.pop();
}
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF));
sink(fntop);
} else if (token == "AP") { } else if (token == "AP") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
} else if (token == "RET") { } else if (token == "RET") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::RET)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
} else if (token == "DUM") { } else if (token == "DUM") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::DUM)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::DUM)));
} else if (token == "RAP") { } else if (token == "RAP") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::RAP)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RAP)));
} else if (token == "STOP") { } else if (token == "STOP") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
} else if (token == "ADD") { } else if (token == "ADD") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
} else if (token == "SUB") { } else if (token == "SUB") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::SUB)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SUB)));
} else if (token == "READCHAR") { } else if (token == "READCHAR") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::READCHAR)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::READCHAR)));
} else if (token == "PUTCHAR") { } else if (token == "PUTCHAR") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
} else if (token == "PUTNUM") { } else if (token == "PUTNUM") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTNUM)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTNUM)));
} else if (token == "CONS") { } else if (token == "CONS") {
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
} else { } else {
if (token != ")") if (token != ")")
throw std::invalid_argument("Unknown token " + token); throw std::invalid_argument("Unknown token " + token);
@@ -127,11 +105,9 @@ std::string_view Parser::Tokenizer::peek() const {
} }
void Parser::Tokenizer::load(std::string_view input) { void Parser::Tokenizer::load(std::string_view input) {
for (const auto &w: input for (const auto &w: input | std::views::split(' ') | std::views::transform([](auto &&rng) {
| std::views::split(' ') return std::string_view(&*rng.begin(), std::ranges::distance(rng));
| std::views::transform([](auto &&rng) { })) {
return std::string_view(&*rng.begin(), std::ranges::distance(rng));
})) {
_tokens.emplace(w); _tokens.emplace(w);
} }
} }
@@ -139,4 +115,3 @@ void Parser::Tokenizer::load(std::string_view input) {
bool Parser::Tokenizer::empty() const { bool Parser::Tokenizer::empty() const {
return _tokens.empty(); return _tokens.empty();
} }

5
src/vm/src/SParser.cpp Normal file
View File

@@ -0,0 +1,5 @@
//
// Created by Stepan Usatiuk on 25.12.2023.
//
#include "SParser.h"

View File

@@ -2,68 +2,71 @@
// Created by Stepan Usatiuk on 22.12.2023. // Created by Stepan Usatiuk on 22.12.2023.
// //
#include "VM.h"
#include <iostream> #include <iostream>
#include <set> #include <set>
#include <utility> #include <utility>
#include "Command.h"
#include "ConsUtils.h"
#include "VM.h"
VM::VM(std::istream &instream, std::ostream &outstream) : _instream(instream), _outstream(outstream) {}
void VM::run() { void VM::run() {
while (!_stop) step(); while (!_stop) step();
} }
void VM::step() { void VM::step() {
CommandCell *popped = dynamic_cast<CommandCell *>(pop(_c)); MCHandle poppedH = ConsUtils::pop(_c);
assert(popped); ValueCell &popped = dynamic_cast<ValueCell &>(*poppedH);
switch (popped->intToCmd()) { switch (Command::int_to_cmd(popped._val)) {
case CommandCell::CommandNum::NIL: { case Command::CommandNum::NIL: {
push(_s, nullptr); ConsUtils::push(_s, nullptr);
break; break;
} }
case CommandCell::CommandNum::LDC: { case Command::CommandNum::LDC: {
push(_s, pop(_c)); ConsUtils::push(_s, ConsUtils::pop(_c));
break; break;
} }
case CommandCell::CommandNum::LD: { case Command::CommandNum::LD: {
ConsCell *popped2 = dynamic_cast<ConsCell *>(pop(_c)); MCHandle poppedH2 = ConsUtils::pop(_c);
assert(popped2); ConsCell &popped2 = dynamic_cast<ConsCell &>(*poppedH2);
assert(dynamic_cast<IntCell *>(popped2->_car)); int64_t frame = dynamic_cast<ValueCell &>(*popped2._car)._val;
int64_t frame = dynamic_cast<IntCell *>(popped2->_car)->_val; int64_t arg = dynamic_cast<ValueCell &>(*popped2._cdr)._val;
assert(dynamic_cast<IntCell *>(popped2->_cdr));
int64_t arg = dynamic_cast<IntCell *>(popped2->_cdr)->_val;
assert(frame > 0); assert(frame > 0);
assert(arg > 0); assert(arg > 0);
ConsCell *curFrame = _e; ConsCell *curFrame = dynamic_cast<ConsCell *>(_e.get());
assert(curFrame);
for (int i = 1; i < frame; i++) { for (int i = 1; i < frame; i++) {
curFrame = dynamic_cast<ConsCell *>(_e->_cdr); curFrame = dynamic_cast<ConsCell *>(curFrame->_cdr);
assert(curFrame); assert(curFrame);
} }
ConsCell *curArg = dynamic_cast<ConsCell *>(curFrame->_car); ConsCell *curArg = dynamic_cast<ConsCell *>(curFrame->_car);
assert(curArg);
for (int i = 1; i < arg; i++) { for (int i = 1; i < arg; i++) {
curArg = dynamic_cast<ConsCell *>(curArg->_cdr); curArg = dynamic_cast<ConsCell *>(curArg->_cdr);
assert(curArg); assert(curArg);
} }
push(_s, curArg->_car); ConsUtils::push(_s, curArg->_car);
break; break;
} }
case CommandCell::CommandNum::SEL: { case Command::CommandNum::SEL: {
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s)); MCHandle popped2H = ConsUtils::pop(_s);
assert(popped2); ValueCell &popped2 = dynamic_cast<ValueCell &>(*popped2H);
ConsCell *ct = dynamic_cast<ConsCell *>(pop(_c));
assert(ct); MCHandle ct = ConsUtils::pop(_c);
ConsCell *cf = dynamic_cast<ConsCell *>(pop(_c)); MCHandle cf = ConsUtils::pop(_c);
assert(cf);
ConsCell *ret = _c; ConsUtils::push(_d, _c);
push(_d, ret); if (popped2._val > 0) {
if (popped2->_val > 0) {
_c = ct; _c = ct;
} else { } else {
_c = cf; _c = cf;
@@ -71,119 +74,103 @@ void VM::step() {
break; break;
} }
case CommandCell::CommandNum::JOIN: { case Command::CommandNum::JOIN: {
ConsCell *ret = dynamic_cast<ConsCell *>(pop(_d)); _c = ConsUtils::pop(_d);
assert(ret);
_c = ret;
break; break;
} }
case CommandCell::CommandNum::LDF: { case Command::CommandNum::LDF: {
ConsCell *fn = dynamic_cast<ConsCell *>(pop(_c)); ConsUtils::push(_s, ConsUtils::cons(ConsUtils::pop(_c), _e));
assert(fn);
push(_s, makeCell<ConsCell>(fn, _e));
break; break;
} }
case CommandCell::CommandNum::AP: { case Command::CommandNum::AP: {
ConsCell *closure = dynamic_cast<ConsCell *>(pop(_s)); MCHandle closureH = ConsUtils::pop(_s);
assert(closure); MCHandle argsH = ConsUtils::pop(_s);
ConsCell *args = dynamic_cast<ConsCell *>(pop(_s));
assert(args);
push(_d, _s); ConsUtils::push(_d, _s);
push(_d, _e); ConsUtils::push(_d, _e);
assert(_c); ConsUtils::push(_d, _c);
push(_d, _c);
_s = makeCell<ConsCell>(); _s = ConsUtils::cons(nullptr, nullptr);
_c = dynamic_cast<ConsCell *>(closure->_car); _c = ConsUtils::car(closureH);
assert(_c); _e = ConsUtils::cdr(closureH);
_e = dynamic_cast<ConsCell *>(closure->_cdr); ConsUtils::push(_e, argsH);
push(_e, args);
break; break;
} }
case CommandCell::CommandNum::RET: { case Command::CommandNum::RET: {
ConsCell *c = dynamic_cast<ConsCell *>(pop(_d)); MCHandle c = ConsUtils::pop(_d);
assert(c); MCHandle e = ConsUtils::pop(_d);
ConsCell *e = dynamic_cast<ConsCell *>(pop(_d)); MCHandle s = ConsUtils::pop(_d);
ConsCell *s = dynamic_cast<ConsCell *>(pop(_d));
Cell *ret = pop(_s); MCHandle ret = ConsUtils::pop(_s);
_c = c; _c = c;
_e = e; _e = e;
_s = s; _s = s;
push(_s, ret); ConsUtils::push(_s, ret);
gc(); // gc();
break; break;
} }
case CommandCell::CommandNum::DUM: { case Command::CommandNum::DUM: {
push(_e, nullptr); ConsUtils::push(_e, nullptr);
break; break;
} }
case CommandCell::CommandNum::RAP: { case Command::CommandNum::RAP: {
ConsCell *closure = dynamic_cast<ConsCell *>(pop(_s)); MCHandle closureH = ConsUtils::pop(_s);
assert(closure); MCHandle argsH = ConsUtils::pop(_s);
ConsCell *args = dynamic_cast<ConsCell *>(pop(_s));
assert(args);
push(_d, _s);
ConsCell *e = dynamic_cast<ConsCell *>(_e->_cdr);
push(_d, e);
assert(_c);
push(_d, _c);
_s = makeCell<ConsCell>(); MCHandle origE = ConsUtils::cdr(_e);
_c = dynamic_cast<ConsCell *>(closure->_car);
assert(_c); ConsUtils::push(_d, _s);
ConsCell *fnenv = dynamic_cast<ConsCell *>(closure->_cdr); ConsUtils::push(_d, origE);
assert(fnenv); ConsUtils::push(_d, _c);
assert(_e == fnenv);
fnenv->_car = args; _s = ConsUtils::cons(nullptr, nullptr);
_c = ConsUtils::car(closureH);
_e = ConsUtils::cdr(closureH);
MCHandle fnEnv = ConsUtils::cdr(closureH);
assert(_e.get() == fnEnv.get());
ConsUtils::push(_e, argsH);
dynamic_cast<ConsCell &>(*fnEnv)._car = argsH.get();
break; break;
} }
case CommandCell::CommandNum::STOP: { case Command::CommandNum::STOP: {
_stop = true; _stop = true;
gc(); // gc();
break; break;
} }
case CommandCell::CommandNum::ADD: { case Command::CommandNum::ADD: {
IntCell *a1 = dynamic_cast<IntCell *>(pop(_s)); int64_t ret = dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val + dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
assert(a1); ConsUtils::push(_s, CURRENT_MC.load()->create_cell<ValueCell>(ret));
IntCell *a2 = dynamic_cast<IntCell *>(pop(_s));
assert(a2);
push(_s, makeCell<IntCell>(a1->_val + a2->_val));
break; break;
} }
case CommandCell::CommandNum::SUB: { case Command::CommandNum::SUB: {
break; break;
} }
case CommandCell::CommandNum::CONS: { case Command::CommandNum::CONS: {
Cell *a1 = pop(_s); MCHandle h1 = ConsUtils::pop(_s);
Cell *a2 = pop(_s); MCHandle h2 = ConsUtils::pop(_s);
push(_s, cons(a1, a2)); ConsUtils::push(_s, ConsUtils::cons(h1, h2));
break; break;
} }
case CommandCell::CommandNum::READCHAR: { case Command::CommandNum::READCHAR: {
char c; char c;
_instream >> c; _instream >> c;
push(_s, makeCell<IntCell>(c)); ConsUtils::push(_s, CURRENT_MC.load()->create_cell<ValueCell>(c));
break; break;
} }
case CommandCell::CommandNum::PUTCHAR: { case Command::CommandNum::PUTCHAR: {
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s)); _outstream << (char) dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
assert(popped2);
_outstream << (char) popped2->_val;
break; break;
} }
case CommandCell::CommandNum::PUTNUM: { case Command::CommandNum::PUTNUM: {
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s)); _outstream << dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
assert(popped2);
_outstream << popped2->_val;
break; break;
} }
case CommandCell::CommandNum::END: { case Command::CommandNum::END: {
assert(false); assert(false);
break; break;
} }
@@ -192,44 +179,40 @@ void VM::step() {
} }
} }
VM::VM(std::istream &instream, std::ostream &outstream) : _instream(instream), _outstream(outstream) {} //void VM::gc() {
// std::function<void(ConsCell *)> visit = [&](ConsCell *c) {
void VM::gc() { // if (c == nullptr) return;
std::function<void(ConsCell *)> visit = [&](ConsCell *c) { // if (c->live) return;
if (c == nullptr) return; //
if (c->live) return; // c->live = true;
//
c->live = true; // if (c->_car) {
// if (c->_car->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_car));
if (c->_car) { // c->_car->live = true;
if (c->_car->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_car)); // }
c->_car->live = true; // if (c->_cdr) {
} // if (c->_cdr->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_cdr));
if (c->_cdr) { // c->_cdr->live = true;
if (c->_cdr->_type == CellType::CONS) visit(dynamic_cast<ConsCell *>(c->_cdr)); // }
c->_cdr->live = true; // };
} // visit(_s);
}; // visit(_e);
visit(_s); // visit(_c);
visit(_e); // visit(_d);
visit(_c); //
visit(_d); // uint64_t freed = 0;
//
uint64_t freed = 0; // _cells.remove_if([&](Cell *l) {
// bool ret = !l->live;
_cells.remove_if([&](Cell *l) { // if (ret) {
bool ret = !l->live; // freed += 1;
if (ret) { // delete l;
freed += 1; // } else {
delete l; // l->live = false;
} else { // }
l->live = false; // return ret;
} // });
return ret; //
}); // std::cout << "GC Freed " << freed << std::endl;
//}
std::cout << "GC Freed " << freed << std::endl; //
}
uint64_t VM::cellCount() const {
return _cells.size();
}

View File

@@ -1,239 +1,244 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "Command.h"
#include "ConsUtils.h"
#include "VM.h" #include "VM.h"
TEST(VMTest, BasicHello) { TEST(VMTest, BasicHello) {
std::stringstream ssin; std::stringstream ssin;
std::stringstream ssout; std::stringstream ssout;
{ {
MemoryContext mc;
VM vm(ssin, ssout); VM vm(ssin, ssout);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP)); MCHandle newc(ConsUtils::cons(nullptr, nullptr));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
vm.appendCommand(vm.makeCell<IntCell>('h')); ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); ConsUtils::append(newc, ConsUtils::makeIntCell('h'));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
vm.loadControl(newc);
vm.run(); vm.run();
} }
ssout.flush(); ssout.flush();
EXPECT_EQ(ssout.str(), "h"); EXPECT_EQ(ssout.str(), "h");
} }
TEST(VMTest, SelTest) { //TEST(VMTest, SelTest) {
std::stringstream ssin; // std::stringstream ssin;
std::stringstream ssout; // std::stringstream ssout;
{ // {
VM vm(ssin, ssout); // VM vm(ssin, ssout);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
//
// True branch true test // // True branch true test
ConsCell *tbtt = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *tbtt = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(tbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); // vm.push(tbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(tbtt, vm.makeCell<IntCell>('1')); // vm.push(tbtt, vm.makeCell<IntCell>('1'));
vm.push(tbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(tbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
// False branch true test // // False branch true test
ConsCell *fbtt = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *fbtt = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(fbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); // vm.push(fbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fbtt, vm.makeCell<IntCell>('2')); // vm.push(fbtt, vm.makeCell<IntCell>('2'));
vm.push(fbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(fbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
// True branch false test // // True branch false test
ConsCell *tbft = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *tbft = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(tbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); // vm.push(tbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(tbft, vm.makeCell<IntCell>('3')); // vm.push(tbft, vm.makeCell<IntCell>('3'));
vm.push(tbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(tbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
// False branch false test // // False branch false test
ConsCell *fbft = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *fbft = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(fbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); // vm.push(fbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fbft, vm.makeCell<IntCell>('4')); // vm.push(fbft, vm.makeCell<IntCell>('4'));
vm.push(fbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(fbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
vm.appendCommand(fbft); // vm.appendCommand(fbft);
vm.appendCommand(tbft); // vm.appendCommand(tbft);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
vm.appendCommand(vm.makeCell<IntCell>(0)); // vm.appendCommand(vm.makeCell<IntCell>(0));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.appendCommand(fbtt); // vm.appendCommand(fbtt);
vm.appendCommand(tbtt); // vm.appendCommand(tbtt);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
vm.appendCommand(vm.makeCell<IntCell>(1)); // vm.appendCommand(vm.makeCell<IntCell>(1));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.run(); // vm.run();
} // }
ssout.flush(); // ssout.flush();
EXPECT_EQ(ssout.str(), "14"); // EXPECT_EQ(ssout.str(), "14");
} //}
//
TEST(VMTest, SimpleFunction) { //TEST(VMTest, SimpleFunction) {
std::stringstream ssin; // std::stringstream ssin;
std::stringstream ssout; // std::stringstream ssout;
{ // {
VM vm(ssin, ssout); // VM vm(ssin, ssout);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
//
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
//
// Add function // // Add function
ConsCell *addfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET)); // ConsCell *addfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
vm.push(addfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); // vm.push(addfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(addfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(2))); // vm.push(addfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(2)));
vm.push(addfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); // vm.push(addfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(addfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); // vm.push(addfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(addfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); // vm.push(addfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.appendCommand(addfn); // vm.appendCommand(addfn);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
vm.appendCommand( // vm.appendCommand(
vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<ConsCell>(vm.makeCell<IntCell>('2')))); // vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<ConsCell>(vm.makeCell<IntCell>('2'))));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.run(); // vm.run();
} // }
ssout.flush(); // ssout.flush();
EXPECT_EQ(ssout.str(), "3"); // EXPECT_EQ(ssout.str(), "3");
} //}
//
TEST(VMTest, RecursiveFunction) { //TEST(VMTest, RecursiveFunction) {
std::stringstream ssin; // std::stringstream ssin;
std::stringstream ssout; // std::stringstream ssout;
{ // {
VM vm(ssin, ssout); // VM vm(ssin, ssout);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
//
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::RAP)); // vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RAP)));
//
// Fib function // // Fib function
ConsCell *fibfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET)); // ConsCell *fibfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
//
// 0 case // // 0 case
ConsCell *zcase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *zcase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(zcase, vm.makeCell<IntCell>(0)); // vm.push(zcase, vm.makeCell<IntCell>(0));
vm.push(zcase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(zcase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
// 1 case // // 1 case
ConsCell *ocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *ocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(ocase, vm.makeCell<IntCell>(1)); // vm.push(ocase, vm.makeCell<IntCell>(1));
vm.push(ocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(ocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
//
// >1 case // // >1 case
ConsCell *gocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); // ConsCell *gocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
//
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(2), vm.makeCell<IntCell>(1))); // vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(2), vm.makeCell<IntCell>(1)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
//
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(gocase, vm.makeCell<IntCell>(-2)); // vm.push(gocase, vm.makeCell<IntCell>(-2));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); // vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(2), vm.makeCell<IntCell>(1))); // vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(2), vm.makeCell<IntCell>(1)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
//
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(gocase, vm.makeCell<IntCell>(-1)); // vm.push(gocase, vm.makeCell<IntCell>(-1));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); // vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); // vm.push(gocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
// >=1 case //// >=1 case
ConsCell *geocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN)); //ConsCell *geocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
vm.push(geocase, ocase); //vm.push(geocase, ocase);
vm.push(geocase, gocase); //vm.push(geocase, gocase);
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL)); //vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); //vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(geocase, vm.makeCell<IntCell>(-1)); //vm.push(geocase, vm.makeCell<IntCell>(-1));
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(geocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(geocase, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
//
vm.push(fibfn, zcase); //vm.push(fibfn, zcase);
vm.push(fibfn, geocase); //vm.push(fibfn, geocase);
vm.push(fibfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL)); //vm.push(fibfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
vm.push(fibfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
//
// Fib caller function //// Fib caller function
ConsCell *fibcallfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET)); //ConsCell *fibcallfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTNUM)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTNUM)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fibcallfn, vm.makeCell<IntCell>(' ')); //vm.push(fibcallfn, vm.makeCell<IntCell>(' '));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(fibcallfn, vm.makeCell<IntCell>(10)); //vm.push(fibcallfn, vm.makeCell<IntCell>(10));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(fibcallfn, vm.makeCell<IntCell>('0')); //vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(fibcallfn, vm.makeCell<IntCell>(6)); //vm.push(fibcallfn, vm.makeCell<IntCell>(6));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(fibcallfn, vm.makeCell<IntCell>('0')); //vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(fibcallfn, vm.makeCell<IntCell>(5)); //vm.push(fibcallfn, vm.makeCell<IntCell>(5));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(fibcallfn, vm.makeCell<IntCell>('0')); //vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(fibcallfn, vm.makeCell<IntCell>(4)); //vm.push(fibcallfn, vm.makeCell<IntCell>(4));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
vm.push(fibcallfn, vm.makeCell<IntCell>('0')); //vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1))); //vm.push(fibcallfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.push(fibcallfn, vm.makeCell<IntCell>(3)); //vm.push(fibcallfn, vm.makeCell<IntCell>(3));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
//
vm.appendCommand(fibcallfn); //vm.appendCommand(fibcallfn);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF)); //vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
//
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS)); //vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
vm.appendCommand(fibfn); //vm.appendCommand(fibfn);
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF)); //vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL)); //vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
//
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::DUM)); //vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::DUM)));
vm.run(); //vm.run();
} //}
ssout.flush(); //ssout.flush();
EXPECT_EQ(ssout.str(), "2358 55"); //EXPECT_EQ(ssout.str(), "2358 55");
} //}

View File

@@ -7,6 +7,7 @@ TEST(VMWithParserTest, BasicHello) {
std::stringstream ssin; std::stringstream ssin;
std::stringstream ssout; std::stringstream ssout;
{ {
MemoryContext mc;
VM vm(ssin, ssout); VM vm(ssin, ssout);
Parser parser(vm); Parser parser(vm);
parser.loadSecd("( LDC 104 PUTCHAR STOP )"); parser.loadSecd("( LDC 104 PUTCHAR STOP )");
@@ -20,6 +21,7 @@ TEST(VMWithParserTest, BasicBranch) {
std::stringstream ssin; std::stringstream ssin;
std::stringstream ssout; std::stringstream ssout;
{ {
MemoryContext mc;
VM vm(ssin, ssout); VM vm(ssin, ssout);
Parser parser(vm); Parser parser(vm);
parser.loadSecd( parser.loadSecd(
@@ -34,6 +36,7 @@ TEST(VMWithParserTest, BasicFunction) {
std::stringstream ssin; std::stringstream ssin;
std::stringstream ssout; std::stringstream ssout;
{ {
MemoryContext mc;
VM vm(ssin, ssout); VM vm(ssin, ssout);
Parser parser(vm); Parser parser(vm);
parser.loadSecd("( NIL LDC 1 CONS LDC 2 CONS LDF ( LD ( 1 . 1 ) LD ( 1 . 2 ) ADD RET ) AP PUTNUM STOP )"); parser.loadSecd("( NIL LDC 1 CONS LDC 2 CONS LDF ( LD ( 1 . 1 ) LD ( 1 . 2 ) ADD RET ) AP PUTNUM STOP )");
@@ -47,6 +50,7 @@ TEST(VMWithParserTest, RecFunction) {
std::stringstream ssin; std::stringstream ssin;
std::stringstream ssout; std::stringstream ssout;
{ {
MemoryContext mc;
VM vm(ssin, ssout); VM vm(ssin, ssout);
Parser parser(vm); Parser parser(vm);
parser.loadSecd( parser.loadSecd(