Updated REPL (thanks to cpp-linenoise!)

This commit is contained in:
2025-10-06 13:00:03 +11:00
parent faebaf7d3f
commit 3dc53ed43d
5 changed files with 2605 additions and 12 deletions

View File

@@ -53,7 +53,11 @@ Instruction::Instruction(std::vector<Value> toks) {
} else if (toks[0].valtype == ValueType::Variable) { } else if (toks[0].valtype == ValueType::Variable) {
instruction = InstructionType::Variable; instruction = InstructionType::Variable;
} else { } else {
error("Instruction should be an identifier or variable value"); if (inReplMode) {
instruction = InstructionType::Variable;
} else {
error("Instruction should be an identifier or variable value");
}
} }
if (instruction == InstructionType::Variable) { if (instruction == InstructionType::Variable) {
for (const auto& tok : toks) { for (const auto& tok : toks) {
@@ -85,7 +89,7 @@ Value::Value(std::string stringval) {
} }
if (stringval.empty()) { if (stringval.empty()) {
valtype = ValueType::String; valtype = ValueType::None;
string_val = ""; string_val = "";
return; return;
} }

View File

@@ -10,7 +10,7 @@ enum class InstructionType {
}; };
enum class ValueType { enum class ValueType {
Processed, Variable, InstructionGroup, List, Map, String, Int, Double, Identifier, Custom, TypePlaceholder Processed, Variable, InstructionGroup, List, Map, String, Int, Double, Identifier, Custom, TypePlaceholder, None
}; };
struct Instruction; struct Instruction;

View File

@@ -5,11 +5,12 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "../data/data.h" #include "../data/data.h"
#include "../vendor/linenoise/linenoise.hpp"
void repl() { void repl() {
data::initScopes(); data::initScopes();
inReplMode = true; inReplMode = true;
std::cout << "Kyn REPL v0.0.1" << std::endl; std::cout << "Kyn REPL v0.0.2" << std::endl;
std::cout << "Type 'exit' to exit" << std::endl; std::cout << "Type 'exit' to exit" << std::endl;
std::string full_command; std::string full_command;
@@ -17,14 +18,11 @@ void repl() {
int brace_level = 0; int brace_level = 0;
while (true) { while (true) {
if (brace_level == 0) { auto prompt = brace_level == 0 ? "kyn> " : ".. > ";
std::cout << "kyn> "; auto quit = linenoise::Readline(prompt, line_buf);
} else {
std::cout << ".. > ";
}
if (!getline(std::cin, line_buf)) { if (quit) {
break; // Handle EOF (Ctrl+D) break;
} }
for (char c : line_buf) { for (char c : line_buf) {
@@ -44,9 +42,13 @@ void repl() {
if (full_command.find_first_not_of(" \t\n\r") != std::string::npos) { if (full_command.find_first_not_of(" \t\n\r") != std::string::npos) {
std::vector<Instruction> parsed = parse(full_command); std::vector<Instruction> parsed = parse(full_command);
for (Instruction inst : parsed) { for (Instruction inst : parsed) {
execute(inst); Value instruction = execute(inst);
if (!(instruction.valtype == ValueType::None)) {
std::cout << instruction.toString() << std::endl;
}
} }
} }
linenoise::AddHistory(full_command.c_str());
full_command.clear(); full_command.clear();
brace_level = 0; brace_level = 0;
} }

0
src/vendor/linenoise/linenoise.h vendored Normal file
View File

2587
src/vendor/linenoise/linenoise.hpp vendored Normal file

File diff suppressed because it is too large Load Diff