Simple list implementation

This commit is contained in:
2025-10-02 14:44:48 +10:00
parent d7123aff1e
commit cf85205ff0
12 changed files with 155 additions and 26 deletions

View File

@@ -4,6 +4,7 @@
#include <string>
#include <memory>
#include <utility>
#include "../datatypes/lists.h"
InstructionType strToInstructionType(std::string in) {
if (in == "println") return InstructionType::Println;
@@ -16,6 +17,7 @@ InstructionType strToInstructionType(std::string in) {
else if (in == "compare") return InstructionType::Compare;
else if (in == "input") return InstructionType::Input;
else if (in == "return") return InstructionType::Return;
else if (in == "concat") return InstructionType::Concat;
else return InstructionType::Variable;
}
@@ -69,7 +71,9 @@ Value::Value() : valtype(ValueType::Real) {}
Value::Value(InstructionGroup instgroup) : valtype(ValueType::InstructionGroup), instructionGroup(instgroup) {};
Value::Value(const Value& other) : valtype(other.valtype), real(other.real), varName(other.varName), instructionGroup(other.instructionGroup) {
Value::Value(std::vector<Value> listval) : valtype(ValueType::List), list(std::move(listval)) {}
Value::Value(const Value& other) : valtype(other.valtype), real(other.real), varName(other.varName), instructionGroup(other.instructionGroup), list(other.list) {
if (other.processed) {
processed = std::make_unique<Instruction>(*other.processed);
}
@@ -86,6 +90,7 @@ Value& Value::operator=(const Value& other) {
real = other.real;
varName = other.varName;
instructionGroup = other.instructionGroup;
list = other.list;
if (other.processed) {
processed = std::make_unique<Instruction>(*other.processed);
} else {
@@ -113,6 +118,14 @@ std::string Value::toString() const {
case ValueType::InstructionGroup:
out += "InstructionGroup, contains " + std::to_string(instructionGroup.size()) + " instructions)";
break;
case ValueType::List: {
out += "List, items: [";
for (const auto& item : list) {
out += item.toString() + ", ";
}
out += "])";
break;
}
default:
out += "FIXME)";
break;
@@ -161,6 +174,8 @@ std::vector<Value> split(std::string line) {
std::string buf;
bool instring = false;
int brackets = 0;
char bracket_type = 0;
for (char chr : line) {
if (chr == ' ' && !instring && brackets == 0 && !buf.empty()) {
if (buf[0] == '$') {
@@ -170,8 +185,8 @@ std::vector<Value> split(std::string line) {
}
buf = "";
} else if (chr == '(' || chr == '[') {
brackets += 1;
if (brackets == 1) {
if (brackets == 0) {
bracket_type = chr;
if (!buf.empty()) {
splitvals.push_back(Value(buf));
buf = "";
@@ -179,13 +194,20 @@ std::vector<Value> split(std::string line) {
} else {
buf += chr;
}
brackets++;
} else if (chr == ')' || chr == ']') {
brackets -= 1;
brackets--;
if (brackets == 0) {
if (!buf.empty()) {
splitvals.push_back(Value(Instruction(split(buf))));
if (bracket_type == '(') {
splitvals.push_back(Value(Instruction(split(buf))));
} else {
splitvals.push_back(Value(parse_list_content(buf)));
}
buf = "";
}
} else {
buf += chr;
}
} else if (chr == '"') {
instring = !instring;