reformat, fix includes

This commit is contained in:
2024-01-04 00:59:24 +01:00
parent 61c681317a
commit 04aa0d24b6
14 changed files with 110 additions and 58 deletions

View File

@@ -33,7 +33,7 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 140
ColumnLimit: 120
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true

View File

@@ -21,7 +21,9 @@ void parse_options(int argc, char *argv[]) {
continue;
}
if (arg.length() < 2 || arg.substr(0, 2) != "--") { throw std::invalid_argument("Can't parse argument " + arg); }
if (arg.length() < 2 || arg.substr(0, 2) != "--") {
throw std::invalid_argument("Can't parse argument " + arg);
}
std::string rest = arg.substr(2);
std::vector<std::string> split;

View File

@@ -8,9 +8,11 @@
#include <mutex>
#include <shared_mutex>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <variant>
class Options {
public:
template<typename T>

View File

@@ -20,7 +20,10 @@ void Logger::log(const std::string &tag, const std::string &what, int level) {
auto now = std::chrono::high_resolution_clock::now();
std::stringstream out;
out << std::setprecision(3) << std::fixed << "["
<< static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(now - get()._start_time).count()) / 1000.0 << "s]"
<< static_cast<double>(
std::chrono::duration_cast<std::chrono::milliseconds>(now - get()._start_time).count()) /
1000.0
<< "s]"
<< "[" << tag << "][" << get()._level_names.at(level) << "] " << what << '\n';
if (level == 1) get()._out_err.get() << out.str();

View File

@@ -51,15 +51,17 @@ namespace Command {
};
static inline std::unordered_map<std::string_view, CellValType> str_to_cmd{
{"NIL", 1}, {"LDC", 2}, {"LD", 3}, {"SEL", 4}, {"JOIN", 5}, {"LDF", 6}, {"AP", 7}, {"RET", 8},
{"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12}, {"ADD", 13}, {"SUB", 14}, {"READCHAR", 15}, {"PUTCHAR", 16},
{"PUTNUM", 17}, {"EVAL", 18}, {"PRINT", 19}, {"READ", 20}, {"CONS", 21}, {"LDG", 22}, {"CAR", 23}, {"CDR", 24},
{"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}, {"MULT", 29}, {"DIV", 30}};
{"NIL", 1}, {"LDC", 2}, {"LD", 3}, {"SEL", 4}, {"JOIN", 5}, {"LDF", 6},
{"AP", 7}, {"RET", 8}, {"DUM", 9}, {"RAP", 10}, {"STOP", 11}, {"ATOM", 12},
{"ADD", 13}, {"SUB", 14}, {"READCHAR", 15}, {"PUTCHAR", 16}, {"PUTNUM", 17}, {"EVAL", 18},
{"PRINT", 19}, {"READ", 20}, {"CONS", 21}, {"LDG", 22}, {"CAR", 23}, {"CDR", 24},
{"EQ", 25}, {"LT", 26}, {"GT", 27}, {"NILC", 28}, {"MULT", 29}, {"DIV", 30}};
static inline std::unordered_map<CellValType, std::string> cmd_to_str{
{1, "NIL"}, {2, "LDC"}, {3, "LD"}, {4, "SEL"}, {5, "JOIN"}, {6, "LDF"}, {7, "AP"}, {8, "RET"},
{9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"}, {13, "ADD"}, {14, "SUB"}, {15, "READCHAR"}, {16, "PUTCHAR"},
{17, "PUTNUM"}, {18, "EVAL"}, {19, "PRINT"}, {20, "READ"}, {21, "CONS"}, {22, "LDG"}, {23, "CAR"}, {24, "CDR"},
{25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}, {29, "MULT"}, {30, "DIV"}};
{1, "NIL"}, {2, "LDC"}, {3, "LD"}, {4, "SEL"}, {5, "JOIN"}, {6, "LDF"},
{7, "AP"}, {8, "RET"}, {9, "DUM"}, {10, "RAP"}, {11, "STOP"}, {12, "ATOM"},
{13, "ADD"}, {14, "SUB"}, {15, "READCHAR"}, {16, "PUTCHAR"}, {17, "PUTNUM"}, {18, "EVAL"},
{19, "PRINT"}, {20, "READ"}, {21, "CONS"}, {22, "LDG"}, {23, "CAR"}, {24, "CDR"},
{25, "EQ"}, {26, "LT"}, {27, "GT"}, {28, "NILC"}, {29, "MULT"}, {30, "DIV"}};
static inline Handle make_cmd(CellValType cmd) {
if (Options::get<bool>("command_strs")) {

View File

@@ -5,6 +5,8 @@
#ifndef PSIL_HANDLE_H
#define PSIL_HANDLE_H
#include <stdexcept>
#include "Cell.h"
class MemoryContext;
@@ -64,11 +66,13 @@ public:
}
CellValType val() const {
if (_target->_type != CellType::NUMATOM) throw std::invalid_argument("Expected number cell, got something else");
if (_target->_type != CellType::NUMATOM)
throw std::invalid_argument("Expected number cell, got something else");
return dynamic_cast<NumAtomCell &>(*_target)._val;
}
std::string_view strval() const {
if (_target->_type != CellType::STRATOM) throw std::invalid_argument("Expected string cell, got something else");
if (_target->_type != CellType::STRATOM)
throw std::invalid_argument("Expected string cell, got something else");
return dynamic_cast<StrAtomCell &>(*_target)._val;
}

View File

@@ -95,7 +95,9 @@ private:
std::lock_guard tmplg(_new_roots_lock);
Handle ret(cell);
_temp_cells.emplace_back(cell);
if ((_cells_num + _temp_cells.size() + 1) >= (size_t) (Options::get<size_t>("cell_limit") / 2)) { request_gc(); }
if ((_cells_num + _temp_cells.size() + 1) >= (size_t) (Options::get<size_t>("cell_limit") / 2)) {
request_gc();
}
return ret;
}
}

View File

@@ -17,9 +17,9 @@
using namespace Command;
static std::unordered_map<std::string_view, CommandE> builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, {"cdr", CDR},
{"=", EQ}, {">", GT}, {"<", LT}, {"nil", NIL}, {"nil?", NILC},
{"atom", ATOM}, {"*", MULT}, {"/", DIV}};
static std::unordered_map<std::string_view, CommandE> builtins{
{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, {"cdr", CDR}, {"=", EQ}, {">", GT},
{"<", LT}, {"nil", NIL}, {"nil?", NILC}, {"atom", ATOM}, {"*", MULT}, {"/", DIV}};
Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffix) {
Handle out;
@@ -128,7 +128,8 @@ Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffi
out.append(make_cmd(LD));
Handle idx = findIndex(car, fake_env);
if (idx.null()) throw std::invalid_argument("Could not find function to apply: " + std::string(car.strval()));
if (idx.null())
throw std::invalid_argument("Could not find function to apply: " + std::string(car.strval()));
out.append(idx);
out.append(make_cmd(AP));
}

View File

@@ -4,6 +4,8 @@
#include "Handle.h"
#include <stdexcept>
#include "MemoryContext.h"
Handle::Handle(Cell *target) : _target(target) {

View File

@@ -34,7 +34,9 @@ void MemoryContext::add_root(Cell *c) {
_new_roots[c]++;
Logger::log(
"MemoryContext",
[&](std::ostream &out) { out << "new root: " << c << " T: " << static_cast<int>(c->_type) << " NUM: " << _new_roots[c]; },
[&](std::ostream &out) {
out << "new root: " << c << " T: " << static_cast<int>(c->_type) << " NUM: " << _new_roots[c];
},
Logger::TRACE);
}
}
@@ -44,7 +46,9 @@ void MemoryContext::remove_root(Cell *c) {
_new_roots[c]--;
Logger::log(
"MemoryContext",
[&](std::ostream &out) { out << "del root: " << c << " T: " << static_cast<int>(c->_type) << " NUM: " << _new_roots[c]; },
[&](std::ostream &out) {
out << "del root: " << c << " T: " << static_cast<int>(c->_type) << " NUM: " << _new_roots[c];
},
Logger::TRACE);
if (_new_roots[c] == 0) _new_roots.erase(c);
}
@@ -72,7 +76,8 @@ void MemoryContext::gc_thread_entry() {
if (c->_live) continue;
c->_live = true;
Logger::log(
"MemoryContext", [&](std::ostream &out) { out << "visiting c " << c << " " << static_cast<int>(c->_type); },
"MemoryContext",
[&](std::ostream &out) { out << "visiting c " << c << " " << static_cast<int>(c->_type); },
Logger::TRACE);
if (c->_type == CellType::CONS) {
@@ -99,7 +104,8 @@ void MemoryContext::gc_thread_entry() {
for (auto const &r: new_roots) {
Logger::log(
"MemoryContext", [&](std::ostream &out) { out << "processing new " << r.first << " diff " << r.second; },
"MemoryContext",
[&](std::ostream &out) { out << "processing new " << r.first << " diff " << r.second; },
Logger::TRACE);
if (r.second == 0) continue;
_roots[r.first] += r.second;
@@ -108,10 +114,11 @@ void MemoryContext::gc_thread_entry() {
auto stop = std::chrono::high_resolution_clock::now();
Logger::log("MemoryContext",
"New roots processing time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
Logger::log(
"MemoryContext",
"New roots processing time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
}
{
@@ -120,16 +127,21 @@ void MemoryContext::gc_thread_entry() {
for (const auto &r: _roots) {
Logger::log(
"MemoryContext", [&](std::ostream &out) { out << "processing r " << r.first << " diff " << r.second; },
"MemoryContext",
[&](std::ostream &out) { out << "processing r " << r.first << " diff " << r.second; },
Logger::TRACE);
toVisit.emplace(r.first);
}
visitAll();
auto stop = std::chrono::high_resolution_clock::now();
Logger::log("MemoryContext", [&](std::ostream &out) { out << "Scanned " << _roots.size() << " roots"; }, Logger::DEBUG);
Logger::log("MemoryContext",
"Roots scan time: " + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
Logger::log(
"MemoryContext", [&](std::ostream &out) { out << "Scanned " << _roots.size() << " roots"; },
Logger::DEBUG);
Logger::log(
"MemoryContext",
"Roots scan time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
}
{
@@ -141,7 +153,9 @@ void MemoryContext::gc_thread_entry() {
}
while (!dirtied.empty()) {
for (const auto &r: dirtied) {
Logger::log("MemoryContext", [&](std::ostream &out) { out << "processing dirty " << r; }, Logger::DEBUG);
Logger::log(
"MemoryContext", [&](std::ostream &out) { out << "processing dirty " << r; },
Logger::DEBUG);
toVisit.emplace(r);
}
visitAll();
@@ -154,9 +168,11 @@ void MemoryContext::gc_thread_entry() {
}
auto stop = std::chrono::high_resolution_clock::now();
Logger::log("MemoryContext",
"Dirty scan time: " + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
Logger::log(
"MemoryContext",
"Dirty scan time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
}
{
auto start = std::chrono::high_resolution_clock::now();
@@ -174,17 +190,23 @@ void MemoryContext::gc_thread_entry() {
});
auto stop = std::chrono::high_resolution_clock::now();
Logger::log("MemoryContext",
"Sweep time: " + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
Logger::log(
"MemoryContext",
"Sweep time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count()),
Logger::INFO);
_cells_num = _cells.size();
Logger::log("MemoryContext", "GC Freed: " + std::to_string(freed) + " cells left: " + std::to_string(_cells_num), Logger::INFO);
Logger::log("MemoryContext",
"GC Freed: " + std::to_string(freed) + " cells left: " + std::to_string(_cells_num),
Logger::INFO);
}
auto gcstop = std::chrono::high_resolution_clock::now();
Logger::log("MemoryContext",
"GC total time: " + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(gcstop - gcstart).count()),
Logger::INFO);
Logger::log(
"MemoryContext",
"GC total time: " +
std::to_string(std::chrono::duration_cast<std::chrono::microseconds>(gcstop - gcstart).count()),
Logger::INFO);
{

View File

@@ -6,6 +6,7 @@
#include <ranges>
#include <stack>
#include <stdexcept>
#include "MemoryContext.h"
#include "VM.h"
@@ -46,7 +47,8 @@ Handle Parser::parseExpr() {
} else {
token = _tokenizer.getNext();
if (token.find_first_not_of("0123456789") == std::string::npos ||
(token.length() > 1 && token.at(0) == '-' && token.find_first_not_of("0123456789", 1) == std::string::npos)) {
(token.length() > 1 && token.at(0) == '-' &&
token.find_first_not_of("0123456789", 1) == std::string::npos)) {
CellValType val = std::stoi(token);
return Handle::makeNumCell(val);
} else {

View File

@@ -5,6 +5,7 @@
#include <iostream>
#include <optional>
#include <set>
#include <stdexcept>
#include <utility>
#include "Command.h"

View File

@@ -93,7 +93,8 @@ TEST(CompilerTest, MultiLet) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn 2 3)) EVAL PRINT STOP)");
parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn 2 3)) EVAL "
"PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -101,7 +102,8 @@ TEST(CompilerTest, MultiLet) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn2 2 3)) EVAL PRINT STOP)");
parser.loadStr("(LDC (let ((plfn (lambda (a b) (- a b))) (plfn2 (lambda (a b) (- b a)))) (plfn2 2 3)) EVAL "
"PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -145,8 +147,8 @@ TEST(CompilerTest, RecursiveFn) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr(
"(LDC (letrec ((fib (lambda (n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ))) (fib 10)) EVAL PRINT STOP)");
parser.loadStr("(LDC (letrec ((fib (lambda (n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ))) "
"(fib 10)) EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -207,8 +209,8 @@ TEST(CompilerTest, GlobalDefineFnCar) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr(
"(LDC (define (carr l) (car l)) EVAL LDC (carr (quote (1 2))) EVAL PRINT LDC (carr (cdr (quote (1 2)))) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (carr l) (car l)) EVAL LDC (carr (quote (1 2))) EVAL PRINT LDC (carr (cdr (quote "
"(1 2)))) EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -223,7 +225,8 @@ TEST(CompilerTest, GlobalDefineFnEq) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (define (eqtest l) (= l ())) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest (nil)) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (eqtest l) (= l ())) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest (nil)) "
"EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -231,7 +234,8 @@ TEST(CompilerTest, GlobalDefineFnEq) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (define (eqtest l) (= l (nil))) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest (nil)) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (eqtest l) (= l (nil))) EVAL LDC (eqtest (quote ())) EVAL PRINT LDC (eqtest "
"(nil)) EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -246,7 +250,8 @@ TEST(CompilerTest, GlobalDefineFnMulti) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (define (one x y) (+ x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (one x y) (+ x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) "
"EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -261,7 +266,8 @@ TEST(CompilerTest, GlobalDefineFnMultiTwo) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC (define (one x y) (- x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (one x y) (- x y)) EVAL LDC (define (two x y) (one (+ x 1) y)) EVAL LDC (two 2 3) "
"EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -278,8 +284,8 @@ TEST(CompilerTest, GlobalDefineFnRec) {
{
VM vm(ssin, ssout);
Parser parser;
parser.loadStr(
"(LDC (define (fib n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ) EVAL LDC (fib 20) EVAL PRINT STOP)");
parser.loadStr("(LDC (define (fib n) (if n (if (+ n -1) (+ (fib (+ n -1)) (fib(+ n -2))) 1) 0) ) EVAL LDC (fib "
"20) EVAL PRINT STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}

View File

@@ -43,7 +43,8 @@ TEST(VMWithParserTest, BasicBranch) {
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("(LDC 1 SEL (LDC 10 PUTNUM JOIN) (LDC 20 PUTNUM JOIN) LDC 0 SEL (LDC 30 PUTNUM JOIN) (LDC 40 PUTNUM JOIN) STOP)");
parser.loadStr("(LDC 1 SEL (LDC 10 PUTNUM JOIN) (LDC 20 PUTNUM JOIN) LDC 0 SEL (LDC 30 PUTNUM JOIN) (LDC 40 "
"PUTNUM JOIN) STOP)");
vm.loadControl(parser.parseExpr());
vm.run();
}
@@ -73,8 +74,10 @@ TEST(VMWithParserTest, RecFunction) {
{
VM vm(ssin, ssout);
Parser parser;
parser.loadStr("( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD CONS LD ( 2 . 1 ) AP "
"NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN ) RET ) CONS LDF ( "
parser.loadStr("( DUM NIL LDF ( LD ( 1 . 1 ) SEL ( LD ( 1 . 1 ) LDC -1 ADD SEL ( NIL LD ( 1 . 1 ) LDC -1 ADD "
"CONS LD ( 2 . 1 ) AP "
"NIL LD ( 1 . 1 ) LDC -2 ADD CONS LD ( 2 . 1 ) AP ADD JOIN ) ( LDC 1 JOIN ) JOIN ) ( LDC 0 JOIN "
") RET ) CONS LDF ( "
"NIL LDC 20 CONS LD ( 1 . 1 ) AP RET ) RAP PUTNUM STOP )");
vm.loadControl(parser.parseExpr());
vm.run();