Basic list support
This commit is contained in:
@@ -17,6 +17,71 @@ std::map<std::string, Object> Executor::getVariables() {
|
||||
|
||||
std::map<std::string, Object> globalVariables;
|
||||
|
||||
void Executor::printValue(const ASTValue& arg) {
|
||||
if (arg.type == ValueType::String) {
|
||||
std::optional<std::string> argString = arg.getString();
|
||||
if (argString.has_value()) {
|
||||
std::cout << argString.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string but got something else";
|
||||
}
|
||||
} else if (arg.type == ValueType::Int) {
|
||||
std::optional<int> argInt = arg.getInt();
|
||||
if (argInt.has_value()) {
|
||||
std::cout << argInt.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting int but got something else";
|
||||
}
|
||||
} else if (arg.type == ValueType::Float) {
|
||||
std::optional<double> argFloat = arg.getFloat();
|
||||
if (argFloat.has_value()) {
|
||||
std::cout << argFloat.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting float but got something else";
|
||||
}
|
||||
} else if (arg.type == ValueType::Bool) {
|
||||
std::optional<bool> argBool = arg.getBool();
|
||||
if (argBool.has_value()) {
|
||||
std::cout << (argBool.value() ? "true" : "false");
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting bool but got something else";
|
||||
}
|
||||
} else if (arg.type == ValueType::List) {
|
||||
std::optional<ASTList> list = arg.getList();
|
||||
if (list.has_value()) {
|
||||
std::cout << "[";
|
||||
auto elements = list.value().getElements();
|
||||
for (size_t i = 0; i < elements.size(); ++i) {
|
||||
auto& node = elements[i];
|
||||
if (std::holds_alternative<std::shared_ptr<ASTValue>>(node)) {
|
||||
printValue(*std::get<std::shared_ptr<ASTValue>>(node));
|
||||
} else if (std::holds_alternative<std::shared_ptr<ASTIdentifier>>(node)) {
|
||||
auto name = std::get<std::shared_ptr<ASTIdentifier>>(node)->name;
|
||||
if (variables.count(name)) {
|
||||
Object obj = variables[name];
|
||||
if (!obj.isCustomObject && std::holds_alternative<ASTValue>(obj.value)) {
|
||||
printValue(std::get<ASTValue>(obj.value));
|
||||
} else {
|
||||
std::cout << "<function>";
|
||||
}
|
||||
} else {
|
||||
std::cout << "<undefined variable: " << name << ">";
|
||||
}
|
||||
} else {
|
||||
std::cout << "<unprintable>";
|
||||
}
|
||||
|
||||
if (i < elements.size() - 1) {
|
||||
std::cout << ", ";
|
||||
}
|
||||
}
|
||||
std::cout << "]";
|
||||
}
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string, int, float, bool or list but got something else";
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<ASTNode> Executor::consume() {
|
||||
if (iterator < code.nodes.size()) {
|
||||
return code.nodes[iterator++];
|
||||
@@ -184,40 +249,9 @@ Executor::Executor(ASTCodeBlock in, bool isInitCall, std::map<std::string, Objec
|
||||
ASTValue arg;
|
||||
if (object.isCustomObject == false && std::holds_alternative<ASTValue>(object.value)) {
|
||||
arg = std::get<ASTValue>(object.value);
|
||||
printValue(arg);
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string, int, float, or bool but got something else" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
if (arg.type == ValueType::String) {
|
||||
std::optional<std::string> argString = arg.getString();
|
||||
if (argString.has_value()) {
|
||||
std::cout << argString.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Int) {
|
||||
std::optional<int> argInt = arg.getInt();
|
||||
if (argInt.has_value()) {
|
||||
std::cout << argInt.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting int but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Float) {
|
||||
std::optional<double> argFloat = arg.getFloat();
|
||||
if (argFloat.has_value()) {
|
||||
std::cout << argFloat.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting float but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Bool) {
|
||||
std::optional<bool> argBool = arg.getBool();
|
||||
if (argBool.has_value()) {
|
||||
std::cout << argBool.value();
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting bool but got something else" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string, int, float, or bool but got something else" << std::endl;
|
||||
std::cout << "Type mismatch - expecting printable value but got something else";
|
||||
}
|
||||
}
|
||||
} else if (fnName == "println") {
|
||||
@@ -225,40 +259,10 @@ Executor::Executor(ASTCodeBlock in, bool isInitCall, std::map<std::string, Objec
|
||||
ASTValue arg;
|
||||
if (object.isCustomObject == false && std::holds_alternative<ASTValue>(object.value)) {
|
||||
arg = std::get<ASTValue>(object.value);
|
||||
printValue(arg);
|
||||
std::cout << std::endl;
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string, int, float, or bool but got something else" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
if (arg.type == ValueType::String) {
|
||||
std::optional<std::string> argString = arg.getString();
|
||||
if (argString.has_value()) {
|
||||
std::cout << argString.value() << std::endl;
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Int) {
|
||||
std::optional<int> argInt = arg.getInt();
|
||||
if (argInt.has_value()) {
|
||||
std::cout << argInt.value() << std::endl;
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting int but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Float) {
|
||||
std::optional<double> argFloat = arg.getFloat();
|
||||
if (argFloat.has_value()) {
|
||||
std::cout << argFloat.value() << std::endl;
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting float but got something else" << std::endl;
|
||||
}
|
||||
} else if (arg.type == ValueType::Bool) {
|
||||
std::optional<bool> argBool = arg.getBool();
|
||||
if (argBool.has_value()) {
|
||||
std::cout << (argBool.value() ? "true" : "false") << std::endl;
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting bool but got something else" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "Type mismatch - expecting string, int, float, or bool but got something else" << std::endl;
|
||||
std::cout << "Type mismatch - expecting printable value but got something else" << std::endl;
|
||||
}
|
||||
}
|
||||
} else if (fnName == "if") {
|
||||
|
||||
@@ -35,6 +35,7 @@ class Executor {
|
||||
size_t iterator = 0;
|
||||
std::optional<ASTNode> consume();
|
||||
std::optional<ASTNode> peek(int ahead = 1);
|
||||
void printValue(const ASTValue& arg);
|
||||
public:
|
||||
explicit Executor(ASTCodeBlock in, bool isInitCall = false, std::map<std::string, Object> scopeVals = {}, std::vector<Object> args = {});
|
||||
std::map<std::string, Object> getVariables();
|
||||
|
||||
Reference in New Issue
Block a user