|
13 | 13 | #define on_off(val) ((val) ? "on " : "off") |
14 | 14 |
|
15 | 15 | namespace repl { |
| 16 | + static options::Options options_v; |
| 17 | + |
16 | 18 | static void process_load_command(const std::string &arg, |
17 | 19 | std::optional<std::shared_ptr<intp::interp::Env> > &shared_env) { |
18 | | - try { |
19 | | - const std::string &filepath = arg; |
20 | | - if (!std::filesystem::exists(filepath)) { |
21 | | - std::cerr << colors::RED << "error: filepath " << filepath << " does not exist" |
22 | | - << colors::RESET << std::endl; |
23 | | - return; |
| 20 | + const std::string &filepath = arg; |
| 21 | + if (!std::filesystem::exists(filepath)) { |
| 22 | + options_v.logger.error({}, "IO error: filepath ", filepath, "does not exists"); |
| 23 | + } |
| 24 | + fe::lexer::Lexer lexer(filepath, fe::lexer::FromFile{}); |
| 25 | + const auto tokens = lexer.lex_all(); |
| 26 | + if (options_v.debug) { |
| 27 | + for (const auto &tok: tokens) { |
| 28 | + options_v.logger.debug(tok); |
24 | 29 | } |
25 | | - fe::lexer::Lexer lexer(filepath, fe::lexer::FromFile{}); |
26 | | - const auto tokens = lexer.lex_all(); |
27 | | - // TODO: Accept Options and Add a debug flag for printing Lexed Tokens |
28 | | - fe::parser::Parser parser(tokens); |
29 | | - const std::optional<std::shared_ptr<intp::interp::Env> > temp_env = shared_env; |
30 | | - // Merge loaded_env into shared_env |
31 | | - if (const auto [loaded_env, _] = intp::interp::interpret(parser.program, temp_env, {.own_expr = true}); |
32 | | - loaded_env) { |
33 | | - if (!shared_env) { |
34 | | - shared_env = loaded_env; |
35 | | - } else { |
36 | | - // Merge all Bindings from loaded_env into shared_env |
37 | | - for (const auto &[fst, snd]: loaded_env->table) { |
38 | | - (*shared_env)->bind(fst, snd); |
39 | | - } |
| 30 | + } |
| 31 | + fe::parser::Parser parser(tokens); |
| 32 | + const std::optional<std::shared_ptr<intp::interp::Env> > temp_env = shared_env; |
| 33 | + // Merge loaded_env into shared_env |
| 34 | + if (const auto [loaded_env, _] = intp::interp::interpret(parser.program, temp_env, {.own_expr = true}); |
| 35 | + loaded_env) { |
| 36 | + if (!shared_env) { |
| 37 | + shared_env = loaded_env; |
| 38 | + } else { |
| 39 | + // Merge all Bindings from loaded_env into shared_env |
| 40 | + for (const auto &[fst, snd]: loaded_env->table) { |
| 41 | + (*shared_env)->bind(fst, snd); |
40 | 42 | } |
41 | 43 | } |
42 | | - std::cout << colors::BLUE << "info: file loaded: " << filepath << colors::RESET << std::endl; |
43 | | - } catch (const std::exception &ex) { |
44 | | - std::cerr << colors::RED << "error: could not load file: " << ex.what() << colors::RESET << std::endl; |
45 | 44 | } |
| 45 | + options_v.logger.info("info: file loaded ", filepath); |
46 | 46 | } |
47 | 47 |
|
| 48 | + |
48 | 49 | void loop(const bool debug) { |
49 | 50 | enable_virtual_terminal(); |
50 | 51 |
|
51 | | - static bool force_on_env_dump = false; |
52 | 52 | static logs::Logger logger(false, true, false); |
53 | | - static options::Options options = {.own_expr = true, .debug = debug, .logger = logger}; |
| 53 | + options_v = {.own_expr = true, .force_on_env_dump = false, .debug = debug, .logger = logger}; |
54 | 54 |
|
55 | | - std::cout << colors::BLUE << "Welcome to lambda-discipline REPL.\nType :quit to exit." |
56 | | - << colors::RESET << std::endl; |
57 | 55 | std::string line; |
58 | 56 | std::optional<std::shared_ptr<intp::interp::Env> > shared_global_env = std::nullopt; |
| 57 | + |
| 58 | + options_v.logger.info("Welcome to lambda-discipline REPL.\nType :quit to exit."); |
| 59 | + |
59 | 60 | while (true) { |
60 | | - std::cout << colors::CYAN << "\n>> " << colors::RESET; |
61 | | - if (!std::getline(std::cin, line)) { |
62 | | - break; // EOF (C-d / C-z) |
63 | | - } |
64 | | - if (line == ":q" || line == ":quit" || line == ":exit") { |
65 | | - std::cout << "\n" << colors::BLUE << "exiting REPL" << colors::RESET << std::endl; |
66 | | - break; |
67 | | - } |
68 | | - if (line == ":c" || line == ":clear" || line == ":cls") { |
| 61 | + try { |
| 62 | + std::cout << colors::CYAN << "\n>> " << colors::RESET; |
| 63 | + if (!std::getline(std::cin, line)) { |
| 64 | + break; // EOF (C-d / C-z) |
| 65 | + } |
| 66 | + if (line == ":q" || line == ":quit" || line == ":exit") { |
| 67 | + options_v.logger.info("\nexiting REPL."); |
| 68 | + break; |
| 69 | + } |
| 70 | + if (line == ":c" || line == ":clear" || line == ":cls") { |
69 | 71 | #ifdef _WIN32 |
70 | | - std::system("cls"); |
| 72 | + std::system("cls"); |
71 | 73 | #else |
72 | | - std::system("clear"); |
| 74 | + std::system("clear"); |
73 | 75 | #endif |
74 | | - continue; |
75 | | - } |
76 | | - if (line == ":h" || line == ":help" || line == ":?") { |
77 | | - std::cout << std::endl; |
78 | | - print_table({"General Commands", "Argument", "Description"}, { |
79 | | - {":q, :quit, :exit", "", "Clear the screen"}, |
80 | | - {":c, :clear, :cls", "", "Exit the REPL"}, |
81 | | - {":h, :help, :?", "", "Display this help message"}, |
82 | | - {":l, :load", "<filepath>", "Load file into REPL"}, |
83 | | - {":r, :reset", "", "Reset environment"}, |
84 | | - {":d, :debug", "", "Toggle debug mode"} |
85 | | - }, colors::GREEN); |
86 | | - std::cout << std::endl; |
87 | | - print_table({"Inspection Commands", "Argument", "Description"}, { |
88 | | - {":e, :env", "", "Dump environment bindings"}, |
89 | | - {":force", "", "Force thunk evaluation on dump"} |
90 | | - }, colors::GREEN); |
91 | | - std::cout << std::endl; |
92 | | - print_table({"Options", "State", "Help"}, { |
93 | | - {"debug", on_off(options.debug), "use :debug to toggle"}, |
94 | | - {"force-on-env-dump", on_off(force_on_env_dump), "use :force to toggle"} |
95 | | - }, colors::GREEN); |
96 | | - continue; |
97 | | - } |
98 | | - if (line == ":env" || line == ":e") { |
99 | | - std::cout << std::endl; |
100 | | - if (shared_global_env) { |
101 | | - print_table({"Symbol", "Thunk"}, (*shared_global_env)->to_vector(force_on_env_dump), colors::GREEN); |
102 | | - } else { |
103 | | - print_table({"Symbol", "Thunk"}, {{"Empty"}}, colors::GREEN); |
| 76 | + continue; |
| 77 | + } |
| 78 | + if (line == ":h" || line == ":help" || line == ":?") { |
| 79 | + std::cout << std::endl; |
| 80 | + print_table({"General Commands", "Argument", "Description"}, { |
| 81 | + {":q, :quit, :exit", "", "Clear the screen"}, |
| 82 | + {":c, :clear, :cls", "", "Exit the REPL"}, |
| 83 | + {":h, :help, :?", "", "Display this help message"}, |
| 84 | + {":l, :load", "<filepath>", "Load file into REPL"}, |
| 85 | + {":r, :reset", "", "Reset environment"}, |
| 86 | + {":d, :debug", "", "Toggle debug mode"} |
| 87 | + }, colors::GREEN); |
| 88 | + std::cout << std::endl; |
| 89 | + print_table({"Inspection Commands", "Argument", "Description"}, { |
| 90 | + {":e, :env", "", "Dump environment bindings"}, |
| 91 | + {":force", "", "Force thunk evaluation on dump"} |
| 92 | + }, colors::GREEN); |
| 93 | + std::cout << std::endl; |
| 94 | + print_table({"Options", "State", "Help"}, { |
| 95 | + {"debug", on_off(options_v.debug), "use :debug to toggle"}, |
| 96 | + {"force-on-env-dump", on_off(options_v.force_on_env_dump), "use :force to toggle"} |
| 97 | + }, colors::GREEN); |
| 98 | + continue; |
| 99 | + } |
| 100 | + if (line == ":env" || line == ":e") { |
| 101 | + std::cout << std::endl; |
| 102 | + if (shared_global_env) { |
| 103 | + print_table({"Symbol", "Thunk"}, (*shared_global_env)->to_vector(options_v.force_on_env_dump), |
| 104 | + colors::GREEN); |
| 105 | + } else { |
| 106 | + print_table({"Symbol", "Thunk"}, {{"Empty"}}, colors::GREEN); |
| 107 | + } |
| 108 | + continue; |
| 109 | + } |
| 110 | + if (line == ":reset" || line == ":r") { |
| 111 | + shared_global_env.reset(); |
| 112 | + continue; |
| 113 | + } |
| 114 | + if (line == ":debug" || line == ":d") { |
| 115 | + options_v.debug = !options_v.debug; |
| 116 | + continue; |
| 117 | + } |
| 118 | + if (line == ":force") { |
| 119 | + options_v.force_on_env_dump = !options_v.force_on_env_dump; |
| 120 | + continue; |
| 121 | + } |
| 122 | + if (line.rfind(":load ", 0) == 0) { |
| 123 | + process_load_command(line.substr(6), shared_global_env); |
| 124 | + continue; |
| 125 | + } |
| 126 | + if (line.rfind(":l ", 0) == 0) { |
| 127 | + process_load_command(line.substr(3), shared_global_env); |
| 128 | + continue; |
| 129 | + } |
| 130 | + if (line.empty()) { |
| 131 | + continue; |
104 | 132 | } |
105 | | - continue; |
106 | | - } |
107 | | - if (line == ":reset" || line == ":r") { |
108 | | - shared_global_env.reset(); |
109 | | - continue; |
110 | | - } |
111 | | - if (line == ":debug" || line == ":d") { |
112 | | - options.debug = !options.debug; |
113 | | - continue; |
114 | | - } |
115 | | - if (line == ":force") { |
116 | | - force_on_env_dump = !force_on_env_dump; |
117 | | - continue; |
118 | | - } |
119 | | - if (line.rfind(":load ", 0) == 0) { |
120 | | - process_load_command(line.substr(6), shared_global_env); |
121 | | - continue; |
122 | | - } |
123 | | - if (line.rfind(":l ", 0) == 0) { |
124 | | - process_load_command(line.substr(3), shared_global_env); |
125 | | - continue; |
126 | | - } |
127 | | - if (line.empty()) { |
128 | | - continue; |
129 | | - } |
130 | | - try { |
131 | 133 | // Lex |
132 | | - fe::lexer::Lexer lexer_v(line, fe::lexer::FromRepl{}, options); |
| 134 | + fe::lexer::Lexer lexer_v(line, fe::lexer::FromRepl{}, options_v); |
133 | 135 | const std::vector<fe::token::Token> tokens = lexer_v.lex_all(); |
134 | | - if (options.debug) { |
| 136 | + if (options_v.debug) { |
135 | 137 | for (const auto &tok: tokens) { |
136 | | - std::cout << colors::YELLOW << tok << colors::RESET << std::endl; |
| 138 | + options_v.logger.debug(tok); |
137 | 139 | } |
138 | 140 | } |
139 | 141 |
|
140 | 142 | // Parse |
141 | | - fe::parser::Parser parser_v(tokens, options); |
142 | | - if (options.debug) { |
143 | | - std::cout << colors::YELLOW << parser_v.program << colors::RESET << std::endl; |
| 143 | + fe::parser::Parser parser_v(tokens, options_v); |
| 144 | + if (options_v.debug) { |
| 145 | + options_v.logger.debug(parser_v.program); |
144 | 146 | } |
145 | 147 |
|
146 | 148 | // Interpret |
147 | 149 | const auto [global_env, value] = intp::interp::interpret( |
148 | | - parser_v.program, shared_global_env, options); |
| 150 | + parser_v.program, shared_global_env, options_v); |
149 | 151 | std::cout << colors::GREEN << "=> " << value << colors::RESET << std::endl; |
150 | 152 | shared_global_env = global_env; |
151 | 153 | } catch (const ControlledExit &) { |
152 | 154 | } catch (const std::exception &ex) { |
153 | | - std::cerr << colors::RED << "error: " << ex.what() << colors::RESET << std::endl; |
| 155 | + options_v.logger.error({}, "error: ", ex.what());; |
154 | 156 | } |
155 | 157 | } |
156 | 158 | } |
|
0 commit comments