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

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

View File

@@ -10,7 +10,7 @@ enum class InstructionType {
};
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;

View File

@@ -5,11 +5,12 @@
#include <iostream>
#include <string>
#include "../data/data.h"
#include "../vendor/linenoise/linenoise.hpp"
void repl() {
data::initScopes();
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::string full_command;
@@ -17,14 +18,11 @@ void repl() {
int brace_level = 0;
while (true) {
if (brace_level == 0) {
std::cout << "kyn> ";
} else {
std::cout << ".. > ";
}
auto prompt = brace_level == 0 ? "kyn> " : ".. > ";
auto quit = linenoise::Readline(prompt, line_buf);
if (!getline(std::cin, line_buf)) {
break; // Handle EOF (Ctrl+D)
if (quit) {
break;
}
for (char c : line_buf) {
@@ -44,9 +42,13 @@ void repl() {
if (full_command.find_first_not_of(" \t\n\r") != std::string::npos) {
std::vector<Instruction> parsed = parse(full_command);
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();
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