diff --git a/.clang-format b/.clang-format index f6b0e1f..207e9d3 100644 --- a/.clang-format +++ b/.clang-format @@ -2,8 +2,28 @@ BasedOnStyle: LLVM AccessModifierOffset: -4 AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: None +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveBitFields: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false +AlignTrailingComments: + Kind: Always + OverEmptyLines: 2 +SpacesBeforeTrailingComments: 1 AlignOperands: Align +AlignEscapedNewlines: Right AllowAllArgumentsOnNextLine: false AllowAllConstructorInitializersOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false @@ -33,7 +53,7 @@ BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon -ColumnLimit: 120 +ColumnLimit: 0 CompactNamespaces: false ContinuationIndentWidth: 8 IndentCaseLabels: true @@ -56,7 +76,6 @@ SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements SpaceBeforeRangeBasedForLoopColon: false SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 0 SpacesInAngles: false SpacesInCStyleCastParentheses: false SpacesInContainerLiterals: false diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..35c56fc --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..560de58 --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 83e3413..e3f4ae0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,7 +29,7 @@ void parse_options(int argc, char *argv[]) { std::vector split; { std::istringstream ins(rest); - std::string cur; + std::string cur; while (std::getline(ins, cur, ':')) { split.emplace_back(cur); } } @@ -66,7 +66,7 @@ int main(int argc, char *argv[]) { parse_options(argc, argv); Handle repl = Parser::parse_str("(READ EVAL PRINT STOP)"); - Handle epl = Parser::parse_str("(EVAL PRINT STOP)"); + Handle epl = Parser::parse_str("(EVAL PRINT STOP)"); VM vm; diff --git a/src/support/include/Logger.h b/src/support/include/Logger.h index 2ad9e12..fbcd7de 100644 --- a/src/support/include/Logger.h +++ b/src/support/include/Logger.h @@ -14,17 +14,23 @@ #include #include -class Logger -{ +class Logger { public: Logger(); - enum LogLevel { ALWAYS = 0, ERROR = 1, INFO = 2, DEBUG = 3, TRACE = 4 }; + enum LogLevel { ALWAYS = 0, + ERROR = 1, + INFO = 2, + DEBUG = 3, + TRACE = 4 }; - enum LogTag { MemoryContext, VM, Compiler, LogTagMax }; + enum LogTag { MemoryContext, + VM, + Compiler, + LogTagMax }; - static void log(LogTag tag, const std::string& what, int level); - static void log(LogTag tag, const std::function& fn, int level); + static void log(LogTag tag, const std::string &what, int level); + static void log(LogTag tag, const std::function &fn, int level); // 0 - disabled // 1 - error @@ -32,39 +38,43 @@ public: // 3 - debug // 4 - trace static void set_level(LogTag tag, int level); - static int get_level(LogTag tag); + static int get_level(LogTag tag); static bool en_level(LogTag tag, int level); - static void set_out(std::ostream& out); - static void set_out_err(std::ostream& out_err); + static void set_out(std::ostream &out); + static void set_out_err(std::ostream &out_err); static void reset(); - static Logger& get(); + static Logger &get(); - static LogTag str_to_tag(const std::string& str) { return _str_to_tag.at(str); } - static const std::string& tag_to_str(LogTag tag) { return _tag_to_str.at(tag); } + static LogTag str_to_tag(const std::string &str) { return _str_to_tag.at(str); } + static const std::string &tag_to_str(LogTag tag) { return _tag_to_str.at(tag); } private: - std::array _levels{}; + std::array _levels{}; static inline std::unordered_map _level_names{ - {ALWAYS, "ALWAYS"}, {ERROR, "ERROR"}, {INFO, "INFO"}, {DEBUG, "DEBUG"}, {TRACE, "TRACE"}, + {ALWAYS, "ALWAYS"}, + {ERROR, "ERROR"}, + {INFO, "INFO"}, + {DEBUG, "DEBUG"}, + {TRACE, "TRACE"}, }; static inline std::unordered_map _str_to_tag{ - {"VM", VM}, - {"MemoryContext", MemoryContext}, - {"Compiler", Compiler}, + {"VM", VM}, + {"MemoryContext", MemoryContext}, + {"Compiler", Compiler}, }; static inline std::unordered_map _tag_to_str{ - {VM, "VM"}, - {MemoryContext, "MemoryContext"}, - {Compiler, "Compiler"}, + {VM, "VM"}, + {MemoryContext, "MemoryContext"}, + {Compiler, "Compiler"}, }; std::chrono::time_point _start_time = std::chrono::high_resolution_clock::now(); - std::reference_wrapper _out = std::cout; - std::reference_wrapper _out_err = std::cerr; + std::reference_wrapper _out = std::cout; + std::reference_wrapper _out_err = std::cerr; }; -#endif//PSIL_LOGGER_H +#endif //PSIL_LOGGER_H diff --git a/src/support/include/Options.h b/src/support/include/Options.h index 7883637..f0d2314 100644 --- a/src/support/include/Options.h +++ b/src/support/include/Options.h @@ -31,7 +31,7 @@ public: o._current[opt] = val; } - static void reset(); + static void reset(); static Options &get(); private: @@ -45,4 +45,4 @@ private: }; -#endif//PSIL_OPTIONS_H +#endif //PSIL_OPTIONS_H diff --git a/src/support/src/Logger.cpp b/src/support/src/Logger.cpp index ac39b5b..fe38ed7 100644 --- a/src/support/src/Logger.cpp +++ b/src/support/src/Logger.cpp @@ -17,17 +17,18 @@ Logger &Logger::get() { void Logger::log(LogTag tag, const std::string &what, int level) { if (!en_level(tag, level)) return; { - auto now = std::chrono::high_resolution_clock::now(); + 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]" - << "[" << tag_to_str(tag) << "][" << get()._level_names.at(level) << "] " << what << '\n'; + << static_cast( + std::chrono::duration_cast(now - get()._start_time).count()) / + 1000.0 + << "s]" + << "[" << tag_to_str(tag) << "][" << get()._level_names.at(level) << "] " << what << '\n'; if (level == 1) get()._out_err.get() << out.str(); - else get()._out.get() << out.str(); + else + get()._out.get() << out.str(); } } diff --git a/src/vm/include/Cell.h b/src/vm/include/Cell.h index 798d15b..439d1a5 100644 --- a/src/vm/include/Cell.h +++ b/src/vm/include/Cell.h @@ -14,7 +14,9 @@ #include #include -enum class CellType { NUMATOM, STRATOM, CONS }; +enum class CellType { NUMATOM, + STRATOM, + CONS }; using CellValType = int64_t; @@ -22,7 +24,7 @@ struct Cell { explicit Cell(CellType type) : _type(type) {} virtual ~Cell() = 0; - CellType _type; + CellType _type; std::atomic _live = false; virtual void print(std::ostream &out) const = 0; @@ -90,7 +92,7 @@ struct ConsCell : public Cell { } void print(std::ostream &out) const override { - std::stringstream res; + std::stringstream res; std::set seen{this}; if (_car) { if (_car.load()->_type == CellType::CONS) { @@ -118,4 +120,4 @@ struct ConsCell : public Cell { } }; -#endif//PSIL_CELL_H +#endif //PSIL_CELL_H diff --git a/src/vm/include/Command.h b/src/vm/include/Command.h index b79cfe0..76a003b 100644 --- a/src/vm/include/Command.h +++ b/src/vm/include/Command.h @@ -13,56 +13,108 @@ namespace Command { enum CommandE : CellValType { - NIL = 1, - LDC = 2, - LD = 3, - SEL = 4, + NIL = 1, + LDC = 2, + LD = 3, + SEL = 4, JOIN = 5, - LDF = 6, - AP = 7, - RET = 8, - DUM = 9, - RAP = 10, + LDF = 6, + AP = 7, + RET = 8, + DUM = 9, + RAP = 10, STOP = 11, ATOM = 12, - ADD = 13, - SUB = 14, + ADD = 13, + SUB = 14, // Encountered in tests READCHAR = 15, - PUTCHAR = 16, - PUTNUM = 17, + PUTCHAR = 16, + PUTNUM = 17, - EVAL = 18, + EVAL = 18, PRINT = 19, - READ = 20, + READ = 20, CONS = 21, - LDG = 22, - CAR = 23, - CDR = 24, - EQ = 25, - LT = 26, - GT = 27, + LDG = 22, + CAR = 23, + CDR = 24, + EQ = 25, + LT = 26, + GT = 27, NILC = 28, MULT = 29, - DIV = 30, + DIV = 30, QUIT = 31 }; 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}, {"QUIT", 31}}; + {"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}, + {"QUIT", 31}}; 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"}, {31, "QUIT"}}; + {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"}, + {31, "QUIT"}}; static inline Handle make_cmd(CellValType cmd) { if (Options::get("command_strs")) { @@ -70,7 +122,7 @@ namespace Command { } else return Handle(cmd); }; -};// namespace Command +}; // namespace Command -#endif//PSIL_COMMAND_H +#endif //PSIL_COMMAND_H diff --git a/src/vm/include/Compiler.h b/src/vm/include/Compiler.h index 1d553ce..a79862c 100644 --- a/src/vm/include/Compiler.h +++ b/src/vm/include/Compiler.h @@ -17,4 +17,4 @@ private: }; -#endif//PSIL_COMPILER_H +#endif //PSIL_COMPILER_H diff --git a/src/vm/include/Handle.h b/src/vm/include/Handle.h index 5929cf4..68ea58a 100644 --- a/src/vm/include/Handle.h +++ b/src/vm/include/Handle.h @@ -90,14 +90,14 @@ public: } Handle pop(); - void push(const Handle &what); - void append(const Handle &what); - void splice(const Handle &what); + void push(const Handle &what); + void append(const Handle &what); + void splice(const Handle &what); static Handle makeNumCell(int64_t val); static Handle makeStrCell(std::string val); - void setcar(const Handle &car); - void setcdr(const Handle &cdr); + void setcar(const Handle &car); + void setcdr(const Handle &cdr); friend std::ostream &operator<<(std::ostream &stream, const Handle &h) { if (h._target) h._target->print(stream); @@ -124,6 +124,6 @@ namespace std { return std::hash()((uintptr_t) c.get()); } }; -}// namespace std +} // namespace std -#endif//PSIL_HANDLE_H +#endif //PSIL_HANDLE_H diff --git a/src/vm/include/MemoryContext.h b/src/vm/include/MemoryContext.h index dd16080..a0f47d1 100644 --- a/src/vm/include/MemoryContext.h +++ b/src/vm/include/MemoryContext.h @@ -126,9 +126,9 @@ private: void add_root(Cell *c); void remove_root(Cell *c); - std::list _cells; + std::list _cells; std::atomic _cells_num = 0; - std::list _temp_cells; + std::list _temp_cells; void gc_thread_entry(); @@ -138,19 +138,19 @@ private: std::recursive_mutex _new_roots_lock; std::set _gc_dirty_notif_queue; - std::mutex _gc_dirty_notif_queue_lock; + std::mutex _gc_dirty_notif_queue_lock; - std::atomic _gc_request = false; - std::mutex _gc_request_m; + std::atomic _gc_request = false; + std::mutex _gc_request_m; std::condition_variable _gc_request_cv; - std::atomic _gc_done = false; - std::mutex _gc_done_m; + std::atomic _gc_done = false; + std::mutex _gc_done_m; std::condition_variable _gc_done_cv; - std::thread _gc_thread; + std::thread _gc_thread; std::atomic _gc_thread_stop = false; #endif }; -#endif//PSIL_MEMORYCONTEXT_H +#endif //PSIL_MEMORYCONTEXT_H diff --git a/src/vm/include/Parser.h b/src/vm/include/Parser.h index 5dfed4e..061a2cb 100644 --- a/src/vm/include/Parser.h +++ b/src/vm/include/Parser.h @@ -20,7 +20,7 @@ class Parser { public: static Handle parse_str(std::string_view input); - void loadStr(std::string_view input); + void loadStr(std::string_view input); Handle parseExpr(); private: @@ -42,4 +42,4 @@ private: }; -#endif//PSIL_PARSER_H +#endif //PSIL_PARSER_H diff --git a/src/vm/include/VM.h b/src/vm/include/VM.h index d6e9415..3db8adb 100644 --- a/src/vm/include/VM.h +++ b/src/vm/include/VM.h @@ -20,7 +20,7 @@ public: void run(); void loadControl(const Handle &h) { - _c = h; + _c = h; _stop = false; } @@ -30,20 +30,20 @@ public: void step(); private: - Handle _globals_names = Handle::cons(Handle::cons(nullptr, nullptr), nullptr); - Handle _globals_vals = Handle::cons(nullptr, nullptr); + Handle _globals_names = Handle::cons(Handle::cons(nullptr, nullptr), nullptr); + Handle _globals_vals = Handle::cons(nullptr, nullptr); std::unordered_map _globals_names_map; - size_t _cur_global = 0; - size_t _cur_call_level = 0; - Handle _s = Handle::cons(nullptr, nullptr); - Handle _e = Handle::cons(_globals_vals, nullptr); - Handle _c = Handle::cons(nullptr, nullptr); - Handle _d = Handle::cons(nullptr, nullptr); - bool _stop = false; - bool _quit = false; + size_t _cur_global = 0; + size_t _cur_call_level = 0; + Handle _s = Handle::cons(nullptr, nullptr); + Handle _e = Handle::cons(_globals_vals, nullptr); + Handle _c = Handle::cons(nullptr, nullptr); + Handle _d = Handle::cons(nullptr, nullptr); + bool _stop = false; + bool _quit = false; std::istream &_instream; std::ostream &_outstream; }; -#endif//PSIL_VM_H +#endif //PSIL_VM_H diff --git a/src/vm/src/Compiler.cpp b/src/vm/src/Compiler.cpp index e93ce80..ef75225 100644 --- a/src/vm/src/Compiler.cpp +++ b/src/vm/src/Compiler.cpp @@ -18,15 +18,29 @@ 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}, {"read", READ}, {"eval", EVAL}, {"print", PRINT}, {"quit", QUIT}}; + {"+", ADD}, + {"-", SUB}, + {"cons", CONS}, + {"car", CAR}, + {"cdr", CDR}, + {"=", EQ}, + {">", GT}, + {"<", LT}, + {"nil", NIL}, + {"nil?", NILC}, + {"atom", ATOM}, + {"*", MULT}, + {"/", DIV}, + {"read", READ}, + {"eval", EVAL}, + {"print", PRINT}, + {"quit", QUIT}}; Handle Compiler::compile(const Handle &src, Handle fake_env, const Handle &suffix) { Handle out; std::function compileArgsRaw = [&](Handle args) { - Handle out; + Handle out; std::stack rev; while (!args.null()) { rev.push(args.car()); @@ -146,8 +160,8 @@ Handle Compiler::findIndex(const Handle &symbol, const Handle &env) { Handle curFrame = env; while (!curFrame.null()) { - int64_t arg = 1; - Handle curArg = curFrame.car(); + int64_t arg = 1; + Handle curArg = curFrame.car(); while (!curArg.null()) { if (curArg.car() == symbol) return Handle::cons(frame, arg); diff --git a/src/vm/src/Handle.cpp b/src/vm/src/Handle.cpp index b5a9c10..929b0f5 100644 --- a/src/vm/src/Handle.cpp +++ b/src/vm/src/Handle.cpp @@ -32,7 +32,7 @@ Handle Handle::cons(const Handle &car, const Handle &cdr) { Handle Handle::pop() { auto ret = car(); - *this = cdr(); + *this = cdr(); return ret; } diff --git a/src/vm/src/MemoryContext.cpp b/src/vm/src/MemoryContext.cpp index c4824d6..7dfd107 100644 --- a/src/vm/src/MemoryContext.cpp +++ b/src/vm/src/MemoryContext.cpp @@ -103,7 +103,7 @@ void MemoryContext::gc_thread_entry() { }; { - auto start = std::chrono::high_resolution_clock::now(); + auto start = std::chrono::high_resolution_clock::now(); decltype(_new_roots) new_roots; { decltype(_temp_cells) temp_cells; @@ -162,7 +162,7 @@ void MemoryContext::gc_thread_entry() { #ifndef NO_THREADS { - auto start = std::chrono::high_resolution_clock::now(); + auto start = std::chrono::high_resolution_clock::now(); decltype(_gc_dirty_notif_queue) dirtied; { std::lock_guard dql(_gc_dirty_notif_queue_lock); @@ -231,7 +231,7 @@ void MemoryContext::gc_thread_entry() { { std::unique_lock l(_gc_done_m); std::unique_lock l2(_gc_request_m); - _gc_done = true; + _gc_done = true; _gc_request = false; _gc_done_cv.notify_all(); } diff --git a/src/vm/src/Parser.cpp b/src/vm/src/Parser.cpp index 862cb47..6023592 100644 --- a/src/vm/src/Parser.cpp +++ b/src/vm/src/Parser.cpp @@ -74,7 +74,7 @@ std::string_view Parser::Tokenizer::peek() const { void Parser::Tokenizer::load(std::string_view input) { std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'}); - static const std::string alnum = "-+> 0); assert(arg > 0); @@ -97,7 +97,7 @@ void VM::step() { } case AP: { Handle closureH = _s.pop(); - Handle argsH = _s.pop(); + Handle argsH = _s.pop(); _d.push(_s); _d.push(_e); @@ -167,7 +167,7 @@ void VM::step() { } case RAP: { Handle closureH = _s.pop(); - Handle argsH = _s.pop(); + Handle argsH = _s.pop(); Handle origE = _e.cdr(); @@ -292,8 +292,8 @@ void VM::step() { } case PRINT: { if (!_s.null()) { - Handle val = _s.pop(); - bool cons = !val.atom(); + Handle val = _s.pop(); + bool cons = !val.atom(); if (cons) _outstream << "("; _outstream << val; if (cons) _outstream << ")";