void AddLengthToArrayCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { vector<string>& parameters = node.getContentArrayNonConstant(); if (parameters.size() > 3) { string retVal = ""; for (size_t i = 1; i < parameters.size()-1; i++) { if (i != parameters.size() - 2) { retVal += vm.getVariable(parameters.at(i))->getValue() + ";"; } else { retVal += vm.getVariable(parameters.at(i))->getValue(); } } vm.setReturnValue(retVal); } else { vm.setReturnValue(vm.getVariable(parameters[1])->getValue()); } }
void PlusCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { vector<string>& parameters = node.getContentArrayNonConstant(); Variable variable1 = *vm.getVariable(parameters.at(1)); Variable variable2 = *vm.getVariable(parameters.at(2)); if (isUndefined(variable1, variable2, vm)) { return; } if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) { double number1 = atof(variable1.getValue().c_str()); double number2 = atof(variable2.getValue().c_str()); vm.setReturnValue(to_string(number1 + number2)); vm.setReturnToken(variable1.getTokenType()); } else if (variable1.getTokenType() == IToken::TYPE_FACT && variable2.getTokenType() == IToken::TYPE_FACT) { bool bool1 = (variable1.getValue() == "true") ? true : false; bool bool2 = (variable2.getValue() == "true") ? true : false; bool outcome = bool1 + bool2; if (outcome) { vm.setReturnValue("true"); } else { vm.setReturnValue("false"); } vm.setReturnToken(variable1.getTokenType()); } else { string var1 = variable1.getValue(); string var2 = variable2.getValue(); if (variable1.getTokenType() == IToken::TYPE_NUMBER) { var1 = removeUnnecessaryDotsAndZeros(var1); } if (variable1.getTokenType() == IToken::TYPE_NUMBER) { var2 = removeUnnecessaryDotsAndZeros(var2); } vm.setReturnValue(var1 + var2); vm.setReturnToken(IToken::TYPE_TEXT); } }
void CountCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); shared_ptr<Variable> var = vm.getVariable(parameters[1]); if (var->getTokenType() != IToken::TYPE_FACT_ARRAY && var->getTokenType() != IToken::TYPE_NUMBER_ARRAY && var->getTokenType() != IToken::TYPE_TEXT_ARRAY) { throwCustomError("cannot count array " + var->getValue(), vm, supergeheimeToken); return; } shared_ptr<Array> array = vm.getVariableArray(parameters[1]); if (array != nullptr) { if (parameters.size() > 2) { auto var = vm.getVariable(parameters[2]); if (var->getTokenType() == IToken::TYPE_NUMBER && var->getType() == VariableType::number) { int index = atof(var->getValue().c_str()); if (index < 0) { throwCustomError("index is below zero.", vm, supergeheimeToken); } else if (index > (int)array->arraySizes.size() - 1) { throwCustomError("index out of bounds range.", vm, supergeheimeToken); } else { vm.setReturnValue(to_string(array->arraySizes.at(index))); vm.setReturnToken(IToken::TYPE_NUMBER); } } else { throwCustomError("input is not a number.", vm, supergeheimeToken); } } else { vm.setReturnValue(to_string(array->variableArrayDictionary.size())); vm.setReturnToken(IToken::TYPE_NUMBER); } } else { throwCustomError("array is not found.", vm, supergeheimeToken); } }
void MinusMinusCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); Variable variable = *vm.getVariable(parameters[1]); if (variable.getType() == VariableType::nulltype) { variable = *vm.getVariable(parameters[2]); parameters[1] = parameters[2]; } if (variable.getType() != VariableType::nulltype && variable.getTokenType() == IToken::TYPE_NUMBER) { double number1 = atof(variable.getValue().c_str()) - 1; for (string & item : vm.getFunctionParametersByKey(parameters.at(1))) { vm.setVariable(item, to_string(number1), supergeheimeToken, variable.getTokenType()); } vm.setReturnValue(to_string(number1)); vm.setReturnToken(variable.getTokenType()); } else { throwCustomError("cannot decrease an undefined variable.", vm, supergeheimeToken); } }
void MinusCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); Variable variable1 = *vm.getVariable(parameters.at(1)); Variable variable2 = *vm.getVariable(parameters.at(2)); if (isUndefined(variable1, variable2, vm)) { return; } if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) { double number1 = atof(variable1.getValue().c_str()); double number2 = atof(variable2.getValue().c_str()); vm.setReturnValue(to_string(number1 - number2)); vm.setReturnToken(variable1.getTokenType()); } else { // Exception minus requires 2 numbers throwCustomError("cannot subtract " + variable1.getValue() + " by " + variable2.getValue(), vm, supergeheimeToken); return; } }
void ModuloCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); Variable variable1 = *vm.getVariable(parameters.at(1)); Variable variable2 = *vm.getVariable(parameters.at(2)); if (isUndefined(variable1, variable2, vm)) { return; } if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) { int number1 = atoi(variable1.getValue().c_str()); int number2 = atoi(variable2.getValue().c_str()); vm.setReturnValue(to_string(number1 % number2)); vm.setReturnToken(variable1.getTokenType()); } else { throwCustomError("cannot get remainder (modulo) " + variable1.getValue() + " from " + variable2.getValue(), vm,supergeheimeToken); return; } }
void IdentifierToReturnValueCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { vector<string>& parameters = node.getContentArrayNonConstant(); vm.setReturnValue(parameters.at(1)); vm.setReturnToken(node.getToken()->getSubType()); vm.addIdentifer(parameters.at(1)); vm.addArrayTypeToArrayTypes(node.getToken()->getText(), node.getToken()->getSubType()); }
void SmallerEqualsToCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); Variable variable1 = *vm.getVariable(parameters.at(1)); Variable variable2 = *vm.getVariable(parameters.at(2)); if (isUndefined(variable1, variable2, vm)) { return; } if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) { double number1 = atof(variable1.getValue().c_str()); double number2 = atof(variable2.getValue().c_str()); if (number1 <= number2) { vm.setReturnValue("true"); } else { vm.setReturnValue("false"); } vm.setReturnToken(IToken::TYPE_FACT); } else { // Exception "cannot compare different types than numbers" throwCustomError("cannot compare " + variable1.getValue() + " with " + variable2.getValue(), vm, supergeheimeToken); return; } }
void GetFileExtensionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { vector<string>& parameters = node.getContentArrayNonConstant(); shared_ptr<Variable> variable = vm.getVariable(parameters.at(1)); if (variable->getTokenType() == IToken::TYPE_TEXT) { string fileName = variable->getValue(); string::size_type idx; string fileExtension = ""; idx = fileName.rfind('.'); if (idx != string::npos) { fileExtension = "\"" + fileName.substr(idx + 1) + "\""; } vm.setReturnValue(fileExtension); vm.setReturnToken(variable->getTokenType()); } }
void GetFromValueCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); string rValue = vm.getReturnValue(); IToken rToken = vm.getReturnToken(); if (&rValue != nullptr) { vm.setReturnValue(""); vm.setReturnToken(IToken::ANY); if (vm.isAnIdentifier(rValue)) { if (!vm.hasValueInFunctionParameters(parameters[1])) { vector<string> value = vm.getFunctionParametersByValue(rValue); if (value.size() > 0) { vm.setVariable(parameters[1], vm.getVariable(value.back())->getValue(), supergeheimeToken, rToken); } else { // Exception var undefined vm.setVariable(parameters[1], "", supergeheimeToken, rToken); } vm.setFunctionParameter(parameters[1], rValue); } } else { vm.setVariable(parameters[1], rValue, supergeheimeToken, rToken); } } }
void GetAllFilesInDirectoryCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); vector<string>& parameters = node.getContentArrayNonConstant(); auto var = vm.getVariable(parameters[1]); string extension = "*.*"; vector<string> out; DIR* dir = nullptr; struct dirent* dirent = nullptr; string directory = var->getValue(); directory = directory.substr(1, directory.size() - 2); dir = opendir(directory.c_str()); // Target directory if (dir == nullptr) { //throwTypeError(*var, *var, vm); //dir is null dir not found throwCustomError("Directory not found! Cannot get all files in directory..", vm, supergeheimeToken); return; } while (dir) { dirent = readdir(dir); if (!dirent) { break; } string direct = dirent->d_name; direct = directory + "\\" + direct; DIR* temp = opendir(direct.c_str()); if (!temp) { string fileName = dirent->d_name; out.push_back("\"" + fileName + "\""); } closedir(temp); } closedir(dir); string buffer; CompileSingleStatement varGetter = CompileSingleStatement(); string localVariable; string arrayDictionary = varGetter.getNextLocalVariableName(buffer); string arrayIdentifier = varGetter.getNextLocalVariableName(buffer); vm.setVariable(arrayDictionary, "", supergeheimeToken, IToken::TYPE_TEXT_ARRAY); shared_ptr<Variable> arrayVar = vm.getVariable(arrayDictionary); vm.setFunctionParameter(arrayDictionary, arrayIdentifier); int Size = out.size(); vm.addArrayToDictionary(arrayDictionary, vector<int>({ Size })); vm.addIdentifer(arrayIdentifier); for (size_t i = 0; i < out.size(); i++) { localVariable = varGetter.getNextLocalVariableName(buffer); vm.setVariable(localVariable, out.at(i), supergeheimeToken, IToken::TYPE_TEXT); vm.addItemToVariableArrayAt(arrayDictionary, vector<string>({ to_string(i) }), vm.getVariable(localVariable)); } vm.setReturnValue(arrayIdentifier); vm.setReturnToken(IToken::TYPE_TEXT_ARRAY); }
void GetFilesInDirectoryByExtensionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node) { auto supergeheimeToken = node.getToken(); // TODO: DO EXTENSION STUFF vector<string>& parameters = node.getContentArrayNonConstant(); auto var = vm.getVariable(parameters[1]); string extension = ".cpp"; vector<string> out; DIR* dir; struct dirent* de; string directory = var->getValue(); directory = directory.substr(1, directory.size() - 2); dir = opendir(directory.c_str()); /*your directory*/ if (dir == nullptr) { //dir is null dir not found throwCustomError("Directory not found! Cannot get files by extension..", vm, supergeheimeToken); return; } while (dir) { de = readdir(dir); if (!de) { break; } if (getExtension(de->d_name) == extension) { out.push_back(de->d_name); } } closedir(dir); string buffer; CompileSingleStatement varGetter = CompileSingleStatement(); string localVariable; string arrayDictionary = varGetter.getNextLocalVariableName(buffer); string arrayIdentifier = varGetter.getNextLocalVariableName(buffer); vm.setVariable(arrayDictionary, "", supergeheimeToken, IToken::TYPE_TEXT_ARRAY); auto arrayVar = vm.getVariable(arrayDictionary); vm.setFunctionParameter(arrayDictionary, arrayIdentifier); int size = out.size(); vm.addArrayToDictionary(arrayDictionary, vector<int>({size})); vm.addIdentifer(arrayIdentifier); for (size_t i = 0; i < out.size(); i++) { localVariable = varGetter.getNextLocalVariableName(buffer); vm.setVariable(localVariable, out.at(i), supergeheimeToken, IToken::TYPE_TEXT); cout << out.at(i) << endl; vm.addItemToVariableArrayAt(arrayDictionary, vector<string>({ to_string(i) }), vm.getVariable(localVariable)); } vm.setReturnValue(arrayIdentifier); vm.setReturnToken(IToken::TYPE_TEXT_ARRAY); }