Simple list implementation
This commit is contained in:
		@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -6,11 +6,11 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
enum class InstructionType {
 | 
			
		||||
    None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Return
 | 
			
		||||
    None, Print, Println, Math, Let, Variable, Exit, If, While, Input, Compare, Function, Return, Concat
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum class ValueType {
 | 
			
		||||
    Real, Processed, Variable, InstructionGroup
 | 
			
		||||
    Real, Processed, Variable, InstructionGroup, List, Map
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct Instruction;
 | 
			
		||||
@@ -33,12 +33,14 @@ struct Value {
 | 
			
		||||
    std::unique_ptr<Instruction> processed;
 | 
			
		||||
    std::string real = "";
 | 
			
		||||
    InstructionGroup instructionGroup;
 | 
			
		||||
    std::vector<Value> list;
 | 
			
		||||
    bool is_return = false;
 | 
			
		||||
    std::string toString() const;
 | 
			
		||||
    Varname varName = Varname();
 | 
			
		||||
    Value(std::string stringval);
 | 
			
		||||
    Value(Instruction instval);
 | 
			
		||||
    Value(InstructionGroup instgroup);
 | 
			
		||||
    Value(std::vector<Value> listval);
 | 
			
		||||
    Value(Varname var);
 | 
			
		||||
    Value();
 | 
			
		||||
    Value(const Value& other);
 | 
			
		||||
@@ -58,4 +60,5 @@ struct Instruction {
 | 
			
		||||
 | 
			
		||||
std::vector<Value> split(std::string in);
 | 
			
		||||
InstructionType strToInstructionType(std::string in);
 | 
			
		||||
std::string trim(const std::string& str);
 | 
			
		||||
extern bool inReplMode;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user