This commit is contained in:
2024-01-03 18:58:43 +01:00
parent 94b3f9681c
commit ff45c72b7a
5 changed files with 54 additions and 9 deletions

View File

@@ -1,14 +1,27 @@
#include <fstream>
#include <iostream> #include <iostream>
#include <optional>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include "MemoryContext.h" #include "MemoryContext.h"
#include "Parser.h" #include "Parser.h"
#include "VM.h" #include "VM.h"
std::optional<std::string> infile;
void parse_options(int argc, char *argv[]) { void parse_options(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
std::string arg = argv[i]; std::string arg = argv[i];
if (arg == "-f") {
if (++i >= argc) throw std::invalid_argument("-f given but no file");
infile = argv[i];
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::string rest = arg.substr(2);
@@ -55,14 +68,43 @@ int main(int argc, char *argv[]) {
parser.loadStr("(READ EVAL PRINT STOP)"); parser.loadStr("(READ EVAL PRINT STOP)");
repl = parser.parseExpr(); repl = parser.parseExpr();
} }
Handle epl;
{
Parser parser;
parser.loadStr("(EVAL PRINT STOP)");
epl = parser.parseExpr();
}
VM vm; VM vm;
while (true) { if (infile) {
std::cout << std::endl; Handle parsed;
vm.loadControl(repl); std::stringstream buffer;
vm.run(); buffer << "(";
std::cout << std::endl; {
std::ifstream t(*infile);
buffer << t.rdbuf();
}
buffer << ")";
Parser parser;
parser.loadStr(buffer.str());
parsed = parser.parseExpr();
Handle cur_expr = parsed;
while (!cur_expr.null()) {
vm.loadStack(Handle::cons(cur_expr.car(), nullptr));
vm.loadControl(epl);
vm.run();
cur_expr = cur_expr.cdr();
}
} }
if (Options::get_bool("repl"))
while (true) {
std::cout << std::endl;
vm.loadControl(repl);
vm.run();
std::cout << std::endl;
}
} catch (const std::exception &e) { } catch (const std::exception &e) {
std::cerr << "\nError: " << e.what() << std::endl; std::cerr << "\nError: " << e.what() << std::endl;
return -1; return -1;

View File

@@ -23,7 +23,8 @@ public:
private: private:
const static inline std::unordered_map<std::string, std::variant<size_t, bool>> _defaults{{"cell_limit", 50000U}, const static inline std::unordered_map<std::string, std::variant<size_t, bool>> _defaults{{"cell_limit", 50000U},
{"command_strs", false}, {"command_strs", false},
{"default_log_level", 1U}}; {"default_log_level", 1U},
{"repl", true}};
std::unordered_map<std::string, std::variant<size_t, bool>> _current = _defaults; std::unordered_map<std::string, std::variant<size_t, bool>> _current = _defaults;
std::shared_mutex _mutex; std::shared_mutex _mutex;

View File

@@ -24,6 +24,8 @@ public:
_stop = false; _stop = false;
} }
void loadStack(const Handle &h) { _s = h; }
void step(); void step();
private: private:

View File

@@ -57,7 +57,7 @@ std::string Parser::Tokenizer::getNext() {
std::string_view Parser::Tokenizer::peek() const { return _tokens.front(); } std::string_view Parser::Tokenizer::peek() const { return _tokens.front(); }
void Parser::Tokenizer::load(std::string_view input) { void Parser::Tokenizer::load(std::string_view input) {
std::string_view::size_type curpos = input.find_first_not_of(' '); std::string_view::size_type curpos = input.find_first_not_of(std::string{' ', '\n', '\r'});
static const std::string alnum = "-+><?=!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static const std::string alnum = "-+><?=!0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static const std::string special = "()."; static const std::string special = "().";
@@ -74,7 +74,7 @@ void Parser::Tokenizer::load(std::string_view input) {
} else { } else {
throw std::invalid_argument("Unexpected symbol " + std::string(1, input.at(curpos))); throw std::invalid_argument("Unexpected symbol " + std::string(1, input.at(curpos)));
} }
curpos = input.find_first_not_of(' ', curpos); curpos = input.find_first_not_of(std::string{' ', '\n', '\r'}, curpos);
} }
} }

View File

@@ -159,7 +159,7 @@ void VM::step() {
} else if (poppedCmd == LDG) { } else if (poppedCmd == LDG) {
_globals_vals.append(Handle::cons(_c.pop(), _e)); _globals_vals.append(Handle::cons(_c.pop(), _e));
} else if (poppedCmd == PRINT) { } else if (poppedCmd == PRINT) {
_outstream << _s.pop(); if (!_s.null()) _outstream << _s.pop() << std::endl;
} else if (poppedCmd == READ) { } else if (poppedCmd == READ) {
std::string read; std::string read;
std::getline(_instream, read); std::getline(_instream, read);