Updated REPL (thanks to cpp-linenoise!)
This commit is contained in:
@@ -52,9 +52,13 @@ Instruction::Instruction(std::vector<Value> toks) {
|
|||||||
instruction = strToInstructionType(toks[0].string_val);
|
instruction = strToInstructionType(toks[0].string_val);
|
||||||
} else if (toks[0].valtype == ValueType::Variable) {
|
} else if (toks[0].valtype == ValueType::Variable) {
|
||||||
instruction = InstructionType::Variable;
|
instruction = InstructionType::Variable;
|
||||||
|
} else {
|
||||||
|
if (inReplMode) {
|
||||||
|
instruction = InstructionType::Variable;
|
||||||
} else {
|
} else {
|
||||||
error("Instruction should be an identifier or variable value");
|
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) {
|
||||||
args.push_back(tok);
|
args.push_back(tok);
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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
0
src/vendor/linenoise/linenoise.h
vendored
Normal file
2587
src/vendor/linenoise/linenoise.hpp
vendored
Normal file
2587
src/vendor/linenoise/linenoise.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user