diff --git a/.clang-format b/.clang-format index 833a44c..f6b0e1f 100644 --- a/.clang-format +++ b/.clang-format @@ -33,7 +33,7 @@ BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon -ColumnLimit: 140 +ColumnLimit: 120 CompactNamespaces: false ContinuationIndentWidth: 8 IndentCaseLabels: true diff --git a/src/main.cpp b/src/main.cpp index 362f9d1..1c06168 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 split; diff --git a/src/support/include/Options.h b/src/support/include/Options.h index 9720f36..a8883da 100644 --- a/src/support/include/Options.h +++ b/src/support/include/Options.h @@ -8,9 +8,11 @@ #include #include +#include #include #include #include + class Options { public: template diff --git a/src/support/src/Logger.cpp b/src/support/src/Logger.cpp index 048fca9..dec6c6b 100644 --- a/src/support/src/Logger.cpp +++ b/src/support/src/Logger.cpp @@ -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(std::chrono::duration_cast(now - get()._start_time).count()) / 1000.0 << "s]" + << static_cast( + std::chrono::duration_cast(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(); diff --git a/src/vm/include/Command.h b/src/vm/include/Command.h index 81ddde3..e911157 100644 --- a/src/vm/include/Command.h +++ b/src/vm/include/Command.h @@ -51,15 +51,17 @@ namespace Command { }; static inline std::unordered_map 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 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("command_strs")) { diff --git a/src/vm/include/Handle.h b/src/vm/include/Handle.h index 7dd2d57..5929cf4 100644 --- a/src/vm/include/Handle.h +++ b/src/vm/include/Handle.h @@ -5,6 +5,8 @@ #ifndef PSIL_HANDLE_H #define PSIL_HANDLE_H +#include + #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(*_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(*_target)._val; } diff --git a/src/vm/include/MemoryContext.h b/src/vm/include/MemoryContext.h index 662f4f6..3912c64 100644 --- a/src/vm/include/MemoryContext.h +++ b/src/vm/include/MemoryContext.h @@ -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("cell_limit") / 2)) { request_gc(); } + if ((_cells_num + _temp_cells.size() + 1) >= (size_t) (Options::get("cell_limit") / 2)) { + request_gc(); + } return ret; } } diff --git a/src/vm/src/Compiler.cpp b/src/vm/src/Compiler.cpp index 642a194..b5c5d26 100644 --- a/src/vm/src/Compiler.cpp +++ b/src/vm/src/Compiler.cpp @@ -17,9 +17,9 @@ using namespace Command; -static std::unordered_map builtins{{"+", ADD}, {"-", SUB}, {"cons", CONS}, {"car", CAR}, {"cdr", CDR}, - {"=", EQ}, {">", GT}, {"<", LT}, {"nil", NIL}, {"nil?", NILC}, - {"atom", ATOM}, {"*", MULT}, {"/", DIV}}; +static std::unordered_map 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)); } diff --git a/src/vm/src/Handle.cpp b/src/vm/src/Handle.cpp index 7abe617..b5a9c10 100644 --- a/src/vm/src/Handle.cpp +++ b/src/vm/src/Handle.cpp @@ -4,6 +4,8 @@ #include "Handle.h" +#include + #include "MemoryContext.h" Handle::Handle(Cell *target) : _target(target) { diff --git a/src/vm/src/MemoryContext.cpp b/src/vm/src/MemoryContext.cpp index 2d01e9b..057d77d 100644 --- a/src/vm/src/MemoryContext.cpp +++ b/src/vm/src/MemoryContext.cpp @@ -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(c->_type) << " NUM: " << _new_roots[c]; }, + [&](std::ostream &out) { + out << "new root: " << c << " T: " << static_cast(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(c->_type) << " NUM: " << _new_roots[c]; }, + [&](std::ostream &out) { + out << "del root: " << c << " T: " << static_cast(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(c->_type); }, + "MemoryContext", + [&](std::ostream &out) { out << "visiting c " << c << " " << static_cast(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(stop - start).count()), - Logger::INFO); + Logger::log( + "MemoryContext", + "New roots processing time: " + + std::to_string(std::chrono::duration_cast(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(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(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(stop - start).count()), - Logger::INFO); + Logger::log( + "MemoryContext", + "Dirty scan time: " + + std::to_string(std::chrono::duration_cast(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(stop - start).count()), - Logger::INFO); + Logger::log( + "MemoryContext", + "Sweep time: " + + std::to_string(std::chrono::duration_cast(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(gcstop - gcstart).count()), - Logger::INFO); + Logger::log( + "MemoryContext", + "GC total time: " + + std::to_string(std::chrono::duration_cast(gcstop - gcstart).count()), + Logger::INFO); { diff --git a/src/vm/src/Parser.cpp b/src/vm/src/Parser.cpp index 9b57987..862cb47 100644 --- a/src/vm/src/Parser.cpp +++ b/src/vm/src/Parser.cpp @@ -6,6 +6,7 @@ #include #include +#include #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 { diff --git a/src/vm/src/VM.cpp b/src/vm/src/VM.cpp index 2846821..85e4d8b 100644 --- a/src/vm/src/VM.cpp +++ b/src/vm/src/VM.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "Command.h" diff --git a/test/vm/CompilerTest.cpp b/test/vm/CompilerTest.cpp index e165cb7..3f1556e 100644 --- a/test/vm/CompilerTest.cpp +++ b/test/vm/CompilerTest.cpp @@ -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(); } diff --git a/test/vm/VMWithParserTest.cpp b/test/vm/VMWithParserTest.cpp index 7146ca8..34c1139 100644 --- a/test/vm/VMWithParserTest.cpp +++ b/test/vm/VMWithParserTest.cpp @@ -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();