simple repl!

This commit is contained in:
2023-12-29 01:42:35 +01:00
parent 4a02d7f231
commit 4da2473958
5 changed files with 32 additions and 2 deletions

View File

@@ -1,6 +1,25 @@
#include "VM.h"
#include <iostream>
#include "MemoryContext.h"
#include "Parser.h"
#include "VM.h"
int main() {
MemoryContext mc;
Handle repl;
{
Parser parser;
parser.loadStr("(READ EVAL PRINT STOP)");
repl = parser.parseExpr();
}
while (true) {
VM vm;
std::cout << std::endl;
vm.loadControl(repl);
vm.run();
std::cout << std::endl;
}
return 0;
}

View File

@@ -55,6 +55,7 @@ private:
Handle EVAL = Handle::makeStrCell("EVAL");
Handle PRINT = Handle::makeStrCell("PRINT");
Handle READ = Handle::makeStrCell("READ");
Handle CONS = Handle::makeStrCell("CONS");
};

View File

@@ -54,6 +54,8 @@ Handle Compiler::compile(Handle src, Handle fake_env, Handle suffix) {
if (car.strval() == "+") {
out.splice(compileArgsRaw(cdr));
out.append(Handle("ADD"));
} else if (car.strval() == "read") {
out.append(Handle("READ"));
} else if (car.strval() == "lambda") {
out.append(Handle("LDF"));
out.append(compile(cdr.cdr().car(), Handle::cons(cdr.car(), fake_env), Handle("RET")));

View File

@@ -69,6 +69,7 @@ void Parser::Tokenizer::load(std::string_view input) {
while (curpos != std::string_view::npos) {
if (alnum.find(input.at(curpos)) != std::string::npos) {
std::string_view::size_type end = input.find_first_not_of(alnum, curpos);
if (end == std::string_view ::npos) end = input.size();
_tokens.emplace(input.cbegin() + curpos, input.cbegin() + end);
curpos = end;
} else if (special.find(input.at(curpos)) != std::string::npos) {

View File

@@ -7,6 +7,7 @@
#include <utility>
#include "Compiler.h"
#include "Parser.h"
#include "VM.h"
@@ -136,7 +137,13 @@ void VM::step() {
newc.splice(_c);
_c = newc;
} else if (poppedH == PRINT) {
_outstream << _s.pop().val();
_outstream << _s.pop();
} else if (poppedH == READ) {
std::string read;
std::getline(_instream, read);
Parser parser;
parser.loadStr(read);
_s.push(parser.parseExpr());
} else {
assert(false);
}