mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-28 18:57:48 +01:00
refactor memory
This commit is contained in:
5
.idea/misc.xml
generated
5
.idea/misc.xml
generated
@@ -1,4 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
<component name="CidrRootsConfiguration">
|
||||
<excludeRoots>
|
||||
<file path="$PROJECT_DIR$/cmake-build-debug" />
|
||||
</excludeRoots>
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "VM.h"
|
||||
#include <iostream>
|
||||
#include "vm/includes/VM.h"
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
|
||||
@@ -2,6 +2,6 @@ set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
41
src/vm/includes/Command.h
Normal file
41
src/vm/includes/Command.h
Normal 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
|
||||
22
src/vm/includes/ConsUtils.h
Normal file
22
src/vm/includes/ConsUtils.h
Normal 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
|
||||
48
src/vm/includes/MemoryContext.h
Normal file
48
src/vm/includes/MemoryContext.h
Normal 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
|
||||
@@ -5,9 +5,12 @@
|
||||
#ifndef PSIL_PARSER_H
|
||||
#define PSIL_PARSER_H
|
||||
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
#include "MemoryContext.h"
|
||||
|
||||
class VM;
|
||||
|
||||
@@ -20,8 +23,7 @@ public:
|
||||
void loadSecd(std::string_view input);
|
||||
|
||||
private:
|
||||
|
||||
void compileBody(const std::function<void(Cell *)> &sink);
|
||||
void compileBody(const std::function<void(MCHandle)> &sink);
|
||||
|
||||
VM &_vm;
|
||||
|
||||
@@ -43,4 +45,4 @@ private:
|
||||
};
|
||||
|
||||
|
||||
#endif //PSIL_PARSER_H
|
||||
#endif//PSIL_PARSER_H
|
||||
|
||||
14
src/vm/includes/SParser.h
Normal file
14
src/vm/includes/SParser.h
Normal 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
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Cell.h"
|
||||
#include "MemoryContext.h"
|
||||
|
||||
class VM {
|
||||
public:
|
||||
@@ -18,61 +19,21 @@ public:
|
||||
|
||||
void run();
|
||||
|
||||
void loadControl(const MCHandle &h) { _c = h; }
|
||||
|
||||
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:
|
||||
std::list<Cell *> _cells;
|
||||
ConsCell *_s = nullptr;
|
||||
ConsCell *_e = nullptr;
|
||||
ConsCell *_c = nullptr;
|
||||
ConsCell *_d = nullptr;
|
||||
MCHandle _s = nullptr;
|
||||
MCHandle _e = nullptr;
|
||||
MCHandle _c = nullptr;
|
||||
MCHandle _d = nullptr;
|
||||
bool _stop = false;
|
||||
|
||||
std::istream &_instream;
|
||||
std::ostream &_outstream;
|
||||
|
||||
void gc();
|
||||
// void gc();
|
||||
};
|
||||
|
||||
|
||||
#endif//PSIL_VM_H
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
// Created by Stepan Usatiuk on 22.12.2023.
|
||||
//
|
||||
|
||||
#include "../includes/Cell.h"
|
||||
#include "Cell.h"
|
||||
|
||||
Cell::~Cell() = default;
|
||||
37
src/vm/src/ConsUtils.cpp
Normal file
37
src/vm/src/ConsUtils.cpp
Normal 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);
|
||||
}
|
||||
23
src/vm/src/MemoryContext.cpp
Normal file
23
src/vm/src/MemoryContext.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -7,23 +7,23 @@
|
||||
#include <ranges>
|
||||
#include <stack>
|
||||
|
||||
#include "Command.h"
|
||||
#include "ConsUtils.h"
|
||||
#include "MemoryContext.h"
|
||||
#include "VM.h"
|
||||
|
||||
Parser::Parser(VM &vm) : _vm(vm) {}
|
||||
|
||||
void Parser::loadSecd(std::string_view input) {
|
||||
_tokenizer.load(input);
|
||||
std::stack<Cell *> out;
|
||||
|
||||
compileBody([&out](Cell *cmd) { out.emplace(cmd); });
|
||||
MCHandle out(ConsUtils::cons(nullptr, nullptr));
|
||||
|
||||
while (!out.empty()) {
|
||||
this->_vm.appendCommand(out.top());
|
||||
out.pop();
|
||||
}
|
||||
compileBody([&out](MCHandle cmd) { ConsUtils::append(out, cmd); });
|
||||
_vm.loadControl(out);
|
||||
}
|
||||
|
||||
void Parser::compileBody(const std::function<void(Cell *)> &sink) {
|
||||
void Parser::compileBody(const std::function<void(MCHandle)> &sink) {
|
||||
auto token = _tokenizer.getNext();
|
||||
if (token != "(") throw std::invalid_argument("Expected (");
|
||||
|
||||
@@ -31,84 +31,62 @@ void Parser::compileBody(const std::function<void(Cell *)> &sink) {
|
||||
token = _tokenizer.getNext();
|
||||
|
||||
if (token == "NIL") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
} else if (token == "LDC") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
sink(_vm.makeCell<IntCell>(std::stoi(_tokenizer.getNext())));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
sink(ConsUtils::makeIntCell(std::stoi(_tokenizer.getNext())));
|
||||
} 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 (");
|
||||
int64_t frame = std::stoi(_tokenizer.getNext());
|
||||
if (_tokenizer.getNext() != ".") throw std::invalid_argument("Expected .");
|
||||
int64_t loc = std::stoi(_tokenizer.getNext());
|
||||
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") {
|
||||
std::stack<Cell *> outt;
|
||||
compileBody([&outt](Cell *cmd) { outt.emplace(cmd); });
|
||||
std::stack<Cell *> outf;
|
||||
compileBody([&outf](Cell *cmd) { outf.emplace(cmd); });
|
||||
MCHandle outt(ConsUtils::cons(nullptr, nullptr));
|
||||
compileBody([&outt](MCHandle cmd) { ConsUtils::append(outt, cmd); });
|
||||
MCHandle outf(ConsUtils::cons(nullptr, nullptr));
|
||||
compileBody([&outf](MCHandle cmd) { ConsUtils::append(outf, cmd);; });
|
||||
|
||||
if (outt.empty()) throw std::invalid_argument("Function body empty");
|
||||
if (outf.empty()) throw std::invalid_argument("Function body empty");
|
||||
if (ConsUtils::car(outt).get() == nullptr) 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());
|
||||
outt.pop();
|
||||
while (!outt.empty()) {
|
||||
_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);
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
|
||||
sink(outt);
|
||||
sink(outf);
|
||||
} 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") {
|
||||
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");
|
||||
|
||||
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);
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
|
||||
sink(outt);
|
||||
} 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") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::RET));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
|
||||
} 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") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::RAP));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RAP)));
|
||||
} 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") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
} 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") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::READCHAR));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::READCHAR)));
|
||||
} 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") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTNUM));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTNUM)));
|
||||
} else if (token == "CONS") {
|
||||
sink(_vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
sink(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
} else {
|
||||
if (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) {
|
||||
for (const auto &w: input
|
||||
| std::views::split(' ')
|
||||
| std::views::transform([](auto &&rng) {
|
||||
return std::string_view(&*rng.begin(), std::ranges::distance(rng));
|
||||
})) {
|
||||
for (const auto &w: input | std::views::split(' ') | std::views::transform([](auto &&rng) {
|
||||
return std::string_view(&*rng.begin(), std::ranges::distance(rng));
|
||||
})) {
|
||||
_tokens.emplace(w);
|
||||
}
|
||||
}
|
||||
@@ -139,4 +115,3 @@ void Parser::Tokenizer::load(std::string_view input) {
|
||||
bool Parser::Tokenizer::empty() const {
|
||||
return _tokens.empty();
|
||||
}
|
||||
|
||||
|
||||
5
src/vm/src/SParser.cpp
Normal file
5
src/vm/src/SParser.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 25.12.2023.
|
||||
//
|
||||
|
||||
#include "SParser.h"
|
||||
@@ -2,68 +2,71 @@
|
||||
// Created by Stepan Usatiuk on 22.12.2023.
|
||||
//
|
||||
|
||||
#include "VM.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#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() {
|
||||
while (!_stop) step();
|
||||
}
|
||||
|
||||
void VM::step() {
|
||||
CommandCell *popped = dynamic_cast<CommandCell *>(pop(_c));
|
||||
assert(popped);
|
||||
MCHandle poppedH = ConsUtils::pop(_c);
|
||||
ValueCell &popped = dynamic_cast<ValueCell &>(*poppedH);
|
||||
|
||||
switch (popped->intToCmd()) {
|
||||
case CommandCell::CommandNum::NIL: {
|
||||
push(_s, nullptr);
|
||||
switch (Command::int_to_cmd(popped._val)) {
|
||||
case Command::CommandNum::NIL: {
|
||||
ConsUtils::push(_s, nullptr);
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::LDC: {
|
||||
push(_s, pop(_c));
|
||||
case Command::CommandNum::LDC: {
|
||||
ConsUtils::push(_s, ConsUtils::pop(_c));
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::LD: {
|
||||
ConsCell *popped2 = dynamic_cast<ConsCell *>(pop(_c));
|
||||
assert(popped2);
|
||||
case Command::CommandNum::LD: {
|
||||
MCHandle poppedH2 = ConsUtils::pop(_c);
|
||||
ConsCell &popped2 = dynamic_cast<ConsCell &>(*poppedH2);
|
||||
|
||||
assert(dynamic_cast<IntCell *>(popped2->_car));
|
||||
int64_t frame = dynamic_cast<IntCell *>(popped2->_car)->_val;
|
||||
assert(dynamic_cast<IntCell *>(popped2->_cdr));
|
||||
int64_t arg = dynamic_cast<IntCell *>(popped2->_cdr)->_val;
|
||||
int64_t frame = dynamic_cast<ValueCell &>(*popped2._car)._val;
|
||||
int64_t arg = dynamic_cast<ValueCell &>(*popped2._cdr)._val;
|
||||
|
||||
assert(frame > 0);
|
||||
assert(arg > 0);
|
||||
|
||||
ConsCell *curFrame = _e;
|
||||
ConsCell *curFrame = dynamic_cast<ConsCell *>(_e.get());
|
||||
assert(curFrame);
|
||||
|
||||
for (int i = 1; i < frame; i++) {
|
||||
curFrame = dynamic_cast<ConsCell *>(_e->_cdr);
|
||||
curFrame = dynamic_cast<ConsCell *>(curFrame->_cdr);
|
||||
assert(curFrame);
|
||||
}
|
||||
|
||||
ConsCell *curArg = dynamic_cast<ConsCell *>(curFrame->_car);
|
||||
assert(curArg);
|
||||
|
||||
for (int i = 1; i < arg; i++) {
|
||||
curArg = dynamic_cast<ConsCell *>(curArg->_cdr);
|
||||
assert(curArg);
|
||||
}
|
||||
|
||||
push(_s, curArg->_car);
|
||||
|
||||
ConsUtils::push(_s, curArg->_car);
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::SEL: {
|
||||
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s));
|
||||
assert(popped2);
|
||||
ConsCell *ct = dynamic_cast<ConsCell *>(pop(_c));
|
||||
assert(ct);
|
||||
ConsCell *cf = dynamic_cast<ConsCell *>(pop(_c));
|
||||
assert(cf);
|
||||
ConsCell *ret = _c;
|
||||
push(_d, ret);
|
||||
if (popped2->_val > 0) {
|
||||
case Command::CommandNum::SEL: {
|
||||
MCHandle popped2H = ConsUtils::pop(_s);
|
||||
ValueCell &popped2 = dynamic_cast<ValueCell &>(*popped2H);
|
||||
|
||||
MCHandle ct = ConsUtils::pop(_c);
|
||||
MCHandle cf = ConsUtils::pop(_c);
|
||||
|
||||
ConsUtils::push(_d, _c);
|
||||
if (popped2._val > 0) {
|
||||
_c = ct;
|
||||
} else {
|
||||
_c = cf;
|
||||
@@ -71,119 +74,103 @@ void VM::step() {
|
||||
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::JOIN: {
|
||||
ConsCell *ret = dynamic_cast<ConsCell *>(pop(_d));
|
||||
assert(ret);
|
||||
_c = ret;
|
||||
case Command::CommandNum::JOIN: {
|
||||
_c = ConsUtils::pop(_d);
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::LDF: {
|
||||
ConsCell *fn = dynamic_cast<ConsCell *>(pop(_c));
|
||||
assert(fn);
|
||||
push(_s, makeCell<ConsCell>(fn, _e));
|
||||
case Command::CommandNum::LDF: {
|
||||
ConsUtils::push(_s, ConsUtils::cons(ConsUtils::pop(_c), _e));
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::AP: {
|
||||
ConsCell *closure = dynamic_cast<ConsCell *>(pop(_s));
|
||||
assert(closure);
|
||||
ConsCell *args = dynamic_cast<ConsCell *>(pop(_s));
|
||||
assert(args);
|
||||
case Command::CommandNum::AP: {
|
||||
MCHandle closureH = ConsUtils::pop(_s);
|
||||
MCHandle argsH = ConsUtils::pop(_s);
|
||||
|
||||
push(_d, _s);
|
||||
push(_d, _e);
|
||||
assert(_c);
|
||||
push(_d, _c);
|
||||
ConsUtils::push(_d, _s);
|
||||
ConsUtils::push(_d, _e);
|
||||
ConsUtils::push(_d, _c);
|
||||
|
||||
_s = makeCell<ConsCell>();
|
||||
_c = dynamic_cast<ConsCell *>(closure->_car);
|
||||
assert(_c);
|
||||
_e = dynamic_cast<ConsCell *>(closure->_cdr);
|
||||
push(_e, args);
|
||||
_s = ConsUtils::cons(nullptr, nullptr);
|
||||
_c = ConsUtils::car(closureH);
|
||||
_e = ConsUtils::cdr(closureH);
|
||||
ConsUtils::push(_e, argsH);
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::RET: {
|
||||
ConsCell *c = dynamic_cast<ConsCell *>(pop(_d));
|
||||
assert(c);
|
||||
ConsCell *e = dynamic_cast<ConsCell *>(pop(_d));
|
||||
ConsCell *s = dynamic_cast<ConsCell *>(pop(_d));
|
||||
case Command::CommandNum::RET: {
|
||||
MCHandle c = ConsUtils::pop(_d);
|
||||
MCHandle e = ConsUtils::pop(_d);
|
||||
MCHandle s = ConsUtils::pop(_d);
|
||||
|
||||
Cell *ret = pop(_s);
|
||||
MCHandle ret = ConsUtils::pop(_s);
|
||||
|
||||
_c = c;
|
||||
_e = e;
|
||||
_s = s;
|
||||
|
||||
push(_s, ret);
|
||||
gc();
|
||||
ConsUtils::push(_s, ret);
|
||||
// gc();
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::DUM: {
|
||||
push(_e, nullptr);
|
||||
case Command::CommandNum::DUM: {
|
||||
ConsUtils::push(_e, nullptr);
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::RAP: {
|
||||
ConsCell *closure = dynamic_cast<ConsCell *>(pop(_s));
|
||||
assert(closure);
|
||||
ConsCell *args = dynamic_cast<ConsCell *>(pop(_s));
|
||||
assert(args);
|
||||
case Command::CommandNum::RAP: {
|
||||
MCHandle closureH = ConsUtils::pop(_s);
|
||||
MCHandle argsH = ConsUtils::pop(_s);
|
||||
|
||||
push(_d, _s);
|
||||
ConsCell *e = dynamic_cast<ConsCell *>(_e->_cdr);
|
||||
push(_d, e);
|
||||
assert(_c);
|
||||
push(_d, _c);
|
||||
|
||||
_s = makeCell<ConsCell>();
|
||||
_c = dynamic_cast<ConsCell *>(closure->_car);
|
||||
assert(_c);
|
||||
ConsCell *fnenv = dynamic_cast<ConsCell *>(closure->_cdr);
|
||||
assert(fnenv);
|
||||
assert(_e == fnenv);
|
||||
fnenv->_car = args;
|
||||
MCHandle origE = ConsUtils::cdr(_e);
|
||||
|
||||
ConsUtils::push(_d, _s);
|
||||
ConsUtils::push(_d, origE);
|
||||
ConsUtils::push(_d, _c);
|
||||
|
||||
_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;
|
||||
}
|
||||
case CommandCell::CommandNum::STOP: {
|
||||
case Command::CommandNum::STOP: {
|
||||
_stop = true;
|
||||
gc();
|
||||
// gc();
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::ADD: {
|
||||
IntCell *a1 = dynamic_cast<IntCell *>(pop(_s));
|
||||
assert(a1);
|
||||
IntCell *a2 = dynamic_cast<IntCell *>(pop(_s));
|
||||
assert(a2);
|
||||
push(_s, makeCell<IntCell>(a1->_val + a2->_val));
|
||||
case Command::CommandNum::ADD: {
|
||||
int64_t ret = dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val + dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
|
||||
ConsUtils::push(_s, CURRENT_MC.load()->create_cell<ValueCell>(ret));
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::SUB: {
|
||||
case Command::CommandNum::SUB: {
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::CONS: {
|
||||
Cell *a1 = pop(_s);
|
||||
Cell *a2 = pop(_s);
|
||||
case Command::CommandNum::CONS: {
|
||||
MCHandle h1 = ConsUtils::pop(_s);
|
||||
MCHandle h2 = ConsUtils::pop(_s);
|
||||
|
||||
push(_s, cons(a1, a2));
|
||||
ConsUtils::push(_s, ConsUtils::cons(h1, h2));
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::READCHAR: {
|
||||
case Command::CommandNum::READCHAR: {
|
||||
char c;
|
||||
_instream >> c;
|
||||
push(_s, makeCell<IntCell>(c));
|
||||
ConsUtils::push(_s, CURRENT_MC.load()->create_cell<ValueCell>(c));
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::PUTCHAR: {
|
||||
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s));
|
||||
assert(popped2);
|
||||
_outstream << (char) popped2->_val;
|
||||
case Command::CommandNum::PUTCHAR: {
|
||||
_outstream << (char) dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::PUTNUM: {
|
||||
IntCell *popped2 = dynamic_cast<IntCell *>(pop(_s));
|
||||
assert(popped2);
|
||||
_outstream << popped2->_val;
|
||||
case Command::CommandNum::PUTNUM: {
|
||||
_outstream << dynamic_cast<ValueCell &>(*ConsUtils::pop(_s))._val;
|
||||
break;
|
||||
}
|
||||
case CommandCell::CommandNum::END: {
|
||||
case Command::CommandNum::END: {
|
||||
assert(false);
|
||||
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) {
|
||||
if (c == nullptr) return;
|
||||
if (c->live) return;
|
||||
|
||||
c->live = true;
|
||||
|
||||
if (c->_car) {
|
||||
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));
|
||||
c->_cdr->live = true;
|
||||
}
|
||||
};
|
||||
visit(_s);
|
||||
visit(_e);
|
||||
visit(_c);
|
||||
visit(_d);
|
||||
|
||||
uint64_t freed = 0;
|
||||
|
||||
_cells.remove_if([&](Cell *l) {
|
||||
bool ret = !l->live;
|
||||
if (ret) {
|
||||
freed += 1;
|
||||
delete l;
|
||||
} else {
|
||||
l->live = false;
|
||||
}
|
||||
return ret;
|
||||
});
|
||||
|
||||
std::cout << "GC Freed " << freed << std::endl;
|
||||
}
|
||||
uint64_t VM::cellCount() const {
|
||||
return _cells.size();
|
||||
}
|
||||
//void VM::gc() {
|
||||
// std::function<void(ConsCell *)> visit = [&](ConsCell *c) {
|
||||
// if (c == nullptr) return;
|
||||
// if (c->live) return;
|
||||
//
|
||||
// c->live = true;
|
||||
//
|
||||
// if (c->_car) {
|
||||
// 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));
|
||||
// c->_cdr->live = true;
|
||||
// }
|
||||
// };
|
||||
// visit(_s);
|
||||
// visit(_e);
|
||||
// visit(_c);
|
||||
// visit(_d);
|
||||
//
|
||||
// uint64_t freed = 0;
|
||||
//
|
||||
// _cells.remove_if([&](Cell *l) {
|
||||
// bool ret = !l->live;
|
||||
// if (ret) {
|
||||
// freed += 1;
|
||||
// delete l;
|
||||
// } else {
|
||||
// l->live = false;
|
||||
// }
|
||||
// return ret;
|
||||
// });
|
||||
//
|
||||
// std::cout << "GC Freed " << freed << std::endl;
|
||||
//}
|
||||
//
|
||||
@@ -1,239 +1,244 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Command.h"
|
||||
#include "ConsUtils.h"
|
||||
#include "VM.h"
|
||||
|
||||
TEST(VMTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.appendCommand(vm.makeCell<IntCell>('h'));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
MCHandle newc(ConsUtils::cons(nullptr, nullptr));
|
||||
ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
ConsUtils::append(newc, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
ConsUtils::append(newc, ConsUtils::makeIntCell('h'));
|
||||
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();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "h");
|
||||
}
|
||||
|
||||
TEST(VMTest, SelTest) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
VM vm(ssin, ssout);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP));
|
||||
|
||||
// True branch true test
|
||||
ConsCell *tbtt = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(tbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(tbtt, vm.makeCell<IntCell>('1'));
|
||||
vm.push(tbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
// False branch true test
|
||||
ConsCell *fbtt = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(fbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fbtt, vm.makeCell<IntCell>('2'));
|
||||
vm.push(fbtt, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
// True branch false test
|
||||
ConsCell *tbft = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(tbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(tbft, vm.makeCell<IntCell>('3'));
|
||||
vm.push(tbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
// False branch false test
|
||||
ConsCell *fbft = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(fbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fbft, vm.makeCell<IntCell>('4'));
|
||||
vm.push(fbft, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
vm.appendCommand(fbft);
|
||||
vm.appendCommand(tbft);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL));
|
||||
vm.appendCommand(vm.makeCell<IntCell>(0));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.appendCommand(fbtt);
|
||||
vm.appendCommand(tbtt);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL));
|
||||
vm.appendCommand(vm.makeCell<IntCell>(1));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "14");
|
||||
}
|
||||
|
||||
TEST(VMTest, SimpleFunction) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
VM vm(ssin, ssout);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
|
||||
// Add function
|
||||
ConsCell *addfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET));
|
||||
vm.push(addfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
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, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
|
||||
vm.push(addfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD));
|
||||
vm.appendCommand(addfn);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF));
|
||||
vm.appendCommand(
|
||||
vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<ConsCell>(vm.makeCell<IntCell>('2'))));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "3");
|
||||
}
|
||||
|
||||
TEST(VMTest, RecursiveFunction) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
VM vm(ssin, ssout);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::STOP));
|
||||
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::RAP));
|
||||
|
||||
// Fib function
|
||||
ConsCell *fibfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET));
|
||||
|
||||
// 0 case
|
||||
ConsCell *zcase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(zcase, vm.makeCell<IntCell>(0));
|
||||
vm.push(zcase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
// 1 case
|
||||
ConsCell *ocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(ocase, vm.makeCell<IntCell>(1));
|
||||
vm.push(ocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
|
||||
// >1 case
|
||||
ConsCell *gocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(gocase, vm.makeCell<IntCell>(-2));
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(gocase, vm.makeCell<IntCell>(-1));
|
||||
vm.push(gocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
// >=1 case
|
||||
ConsCell *geocase = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::JOIN));
|
||||
vm.push(geocase, ocase);
|
||||
vm.push(geocase, gocase);
|
||||
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL));
|
||||
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(geocase, vm.makeCell<IntCell>(-1));
|
||||
vm.push(geocase, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
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(fibfn, zcase);
|
||||
vm.push(fibfn, geocase);
|
||||
vm.push(fibfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::SEL));
|
||||
vm.push(fibfn, vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<IntCell>(1)));
|
||||
vm.push(fibfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LD));
|
||||
|
||||
// Fib caller function
|
||||
ConsCell *fibcallfn = vm.makeCell<ConsCell>(vm.makeCell<CommandCell>(CommandCell::CommandNum::RET));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTNUM));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(' '));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(10));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(6));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(5));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(4));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::PUTCHAR));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::ADD));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::AP));
|
||||
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, vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.push(fibcallfn, vm.makeCell<IntCell>(3));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::LDC));
|
||||
vm.push(fibcallfn, vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
|
||||
vm.appendCommand(fibcallfn);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF));
|
||||
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::CONS));
|
||||
vm.appendCommand(fibfn);
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::LDF));
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::NIL));
|
||||
|
||||
vm.appendCommand(vm.makeCell<CommandCell>(CommandCell::CommandNum::DUM));
|
||||
vm.run();
|
||||
}
|
||||
ssout.flush();
|
||||
EXPECT_EQ(ssout.str(), "2358 55");
|
||||
}
|
||||
//TEST(VMTest, SelTest) {
|
||||
// std::stringstream ssin;
|
||||
// std::stringstream ssout;
|
||||
// {
|
||||
// VM vm(ssin, ssout);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
|
||||
//
|
||||
// // True branch true test
|
||||
// ConsCell *tbtt = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(tbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
// vm.push(tbtt, vm.makeCell<IntCell>('1'));
|
||||
// vm.push(tbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// // False branch true test
|
||||
// ConsCell *fbtt = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(fbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
// vm.push(fbtt, vm.makeCell<IntCell>('2'));
|
||||
// vm.push(fbtt, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// // True branch false test
|
||||
// ConsCell *tbft = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(tbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
// vm.push(tbft, vm.makeCell<IntCell>('3'));
|
||||
// vm.push(tbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// // False branch false test
|
||||
// ConsCell *fbft = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(fbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
// vm.push(fbft, vm.makeCell<IntCell>('4'));
|
||||
// vm.push(fbft, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// vm.appendCommand(fbft);
|
||||
// vm.appendCommand(tbft);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
|
||||
// vm.appendCommand(vm.makeCell<IntCell>(0));
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
// vm.appendCommand(fbtt);
|
||||
// vm.appendCommand(tbtt);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
|
||||
// vm.appendCommand(vm.makeCell<IntCell>(1));
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
// vm.run();
|
||||
// }
|
||||
// ssout.flush();
|
||||
// EXPECT_EQ(ssout.str(), "14");
|
||||
//}
|
||||
//
|
||||
//TEST(VMTest, SimpleFunction) {
|
||||
// std::stringstream ssin;
|
||||
// std::stringstream ssout;
|
||||
// {
|
||||
// VM vm(ssin, ssout);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::AP)));
|
||||
//
|
||||
// // Add function
|
||||
// ConsCell *addfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
|
||||
// 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, 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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
// vm.appendCommand(addfn);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
|
||||
// vm.appendCommand(
|
||||
// vm.makeCell<ConsCell>(vm.makeCell<IntCell>(1), vm.makeCell<ConsCell>(vm.makeCell<IntCell>('2'))));
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
// vm.run();
|
||||
// }
|
||||
// ssout.flush();
|
||||
// EXPECT_EQ(ssout.str(), "3");
|
||||
//}
|
||||
//
|
||||
//TEST(VMTest, RecursiveFunction) {
|
||||
// std::stringstream ssin;
|
||||
// std::stringstream ssout;
|
||||
// {
|
||||
// VM vm(ssin, ssout);
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::STOP)));
|
||||
//
|
||||
// vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RAP)));
|
||||
//
|
||||
// // Fib function
|
||||
// ConsCell *fibfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
|
||||
//
|
||||
// // 0 case
|
||||
// ConsCell *zcase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(zcase, vm.makeCell<IntCell>(0));
|
||||
// vm.push(zcase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// // 1 case
|
||||
// ConsCell *ocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(ocase, vm.makeCell<IntCell>(1));
|
||||
// vm.push(ocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//
|
||||
// // >1 case
|
||||
// ConsCell *gocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//
|
||||
// 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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
// vm.push(gocase, vm.makeCell<IntCell>(-2));
|
||||
// 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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
// 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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
// vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
// vm.push(gocase, vm.makeCell<IntCell>(-1));
|
||||
// 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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(gocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//// >=1 case
|
||||
//ConsCell *geocase = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::JOIN)));
|
||||
//vm.push(geocase, ocase);
|
||||
//vm.push(geocase, gocase);
|
||||
//vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::SEL)));
|
||||
//vm.push(geocase, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//vm.push(geocase, vm.makeCell<IntCell>(-1));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//
|
||||
//vm.push(fibfn, zcase);
|
||||
//vm.push(fibfn, geocase);
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//
|
||||
//// Fib caller function
|
||||
//ConsCell *fibcallfn = vm.makeCell<ConsCell>(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::RET)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTNUM)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(' '));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(10));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(6));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(5));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(4));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::PUTCHAR)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::ADD)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>('0'));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//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, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LD)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.push(fibcallfn, vm.makeCell<IntCell>(3));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDC)));
|
||||
//vm.push(fibcallfn, ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//
|
||||
//vm.appendCommand(fibcallfn);
|
||||
//vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
|
||||
//
|
||||
//vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::CONS)));
|
||||
//vm.appendCommand(fibfn);
|
||||
//vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::LDF)));
|
||||
//vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::NIL)));
|
||||
//
|
||||
//vm.appendCommand(ConsUtils::makeIntCell(Command::cmd_to_int(Command::CommandNum::DUM)));
|
||||
//vm.run();
|
||||
//}
|
||||
//ssout.flush();
|
||||
//EXPECT_EQ(ssout.str(), "2358 55");
|
||||
//}
|
||||
@@ -7,6 +7,7 @@ TEST(VMWithParserTest, BasicHello) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser(vm);
|
||||
parser.loadSecd("( LDC 104 PUTCHAR STOP )");
|
||||
@@ -20,6 +21,7 @@ TEST(VMWithParserTest, BasicBranch) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser(vm);
|
||||
parser.loadSecd(
|
||||
@@ -34,6 +36,7 @@ TEST(VMWithParserTest, BasicFunction) {
|
||||
std::stringstream ssin;
|
||||
std::stringstream ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser(vm);
|
||||
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 ssout;
|
||||
{
|
||||
MemoryContext mc;
|
||||
VM vm(ssin, ssout);
|
||||
Parser parser(vm);
|
||||
parser.loadSecd(
|
||||
|
||||
Reference in New Issue
Block a user