mirror of
https://github.com/usatiuk/psil.git
synced 2025-10-29 03:07:49 +01:00
simple repl!
This commit is contained in:
21
src/main.cpp
21
src/main.cpp
@@ -1,6 +1,25 @@
|
|||||||
#include "VM.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "MemoryContext.h"
|
||||||
|
#include "Parser.h"
|
||||||
|
#include "VM.h"
|
||||||
|
|
||||||
int main() {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ private:
|
|||||||
|
|
||||||
Handle EVAL = Handle::makeStrCell("EVAL");
|
Handle EVAL = Handle::makeStrCell("EVAL");
|
||||||
Handle PRINT = Handle::makeStrCell("PRINT");
|
Handle PRINT = Handle::makeStrCell("PRINT");
|
||||||
|
Handle READ = Handle::makeStrCell("READ");
|
||||||
|
|
||||||
Handle CONS = Handle::makeStrCell("CONS");
|
Handle CONS = Handle::makeStrCell("CONS");
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ Handle Compiler::compile(Handle src, Handle fake_env, Handle suffix) {
|
|||||||
if (car.strval() == "+") {
|
if (car.strval() == "+") {
|
||||||
out.splice(compileArgsRaw(cdr));
|
out.splice(compileArgsRaw(cdr));
|
||||||
out.append(Handle("ADD"));
|
out.append(Handle("ADD"));
|
||||||
|
} else if (car.strval() == "read") {
|
||||||
|
out.append(Handle("READ"));
|
||||||
} else if (car.strval() == "lambda") {
|
} else if (car.strval() == "lambda") {
|
||||||
out.append(Handle("LDF"));
|
out.append(Handle("LDF"));
|
||||||
out.append(compile(cdr.cdr().car(), Handle::cons(cdr.car(), fake_env), Handle("RET")));
|
out.append(compile(cdr.cdr().car(), Handle::cons(cdr.car(), fake_env), Handle("RET")));
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ void Parser::Tokenizer::load(std::string_view input) {
|
|||||||
while (curpos != std::string_view::npos) {
|
while (curpos != std::string_view::npos) {
|
||||||
if (alnum.find(input.at(curpos)) != std::string::npos) {
|
if (alnum.find(input.at(curpos)) != std::string::npos) {
|
||||||
std::string_view::size_type end = input.find_first_not_of(alnum, curpos);
|
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);
|
_tokens.emplace(input.cbegin() + curpos, input.cbegin() + end);
|
||||||
curpos = end;
|
curpos = end;
|
||||||
} else if (special.find(input.at(curpos)) != std::string::npos) {
|
} else if (special.find(input.at(curpos)) != std::string::npos) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
#include "Parser.h"
|
||||||
#include "VM.h"
|
#include "VM.h"
|
||||||
|
|
||||||
|
|
||||||
@@ -136,7 +137,13 @@ void VM::step() {
|
|||||||
newc.splice(_c);
|
newc.splice(_c);
|
||||||
_c = newc;
|
_c = newc;
|
||||||
} else if (poppedH == PRINT) {
|
} 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 {
|
} else {
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user