/** * @brief Load a variable into memory. * * Checks to see if this variable is loaded, and if not, checks that it exists in the file. * Use this method when the variable to load is of type float * * @param variable Variable to load into memory. * @return status of the operation */ long Model::loadVariable(const std::string& variable) { //first, check to determine whether variable is already loaded if (variableData.find(variable) != variableData.end()) return FileReader::OK; //first check if the variable exists in the file!! if (!this->doesVariableExist(variable)) { std::cerr <<"Problem: "<< variable << " does not exist" << std::endl; return FileReader::VARIABLE_DOES_NOT_EXIST; } std::vector<float> * data = getVariable(variable); long id = getVariableID(variable); //std::cout << BOOST_CURRENT_FUNCTION << " id: " << id << std::endl; if (data->size() > 0) { //std::cout << "adding " << variable << " to maps" << std::endl; variableData[variable] = data; variableDataByID[id] = data; } //else return false; else { //std::cout << "not adding " << variable << " to maps" << std::endl; delete data; } return FileReader::OK; }
/** * @brief Unload a variable from memory. * This will return FileReader::OK if the variable is removed from memory, and FileReader::VARIABLE_NOT_IN_MEMORY * if the variable was not already in memory. * * @param variable Variable to unload from memory. * @return status of the operation */ long Model::unloadVariable(const std::string& variable) { //bool success = false; //first, check to determine whether variable is already loaded if (variableData.find(variable) != variableData.end()) { std::vector<float> * data = variableData[variable]; long id = getVariableID(variable); delete data; variableData.erase(variable); variableDataByID.erase(id); return FileReader::OK; } return FileReader::VARIABLE_NOT_IN_MEMORY; }
void Expression::allocateVariables(OperationCode *opcode, Parser* parser) { for (list<ExpressionTerm*>::iterator it=postfix.begin(); it!=postfix.end(); it++) { Token *token = (*it)->token; if (token && (token->aType & Token::OPERATOR) == 0) { expressionVars[*it] = setVariable(parser, ""); } } for (map<ExpressionTerm*,uint>::iterator it=expressionVars.begin(); it != expressionVars.end(); it++) { if (!it->first->token) continue; Token *token = it->first->token; allocateVariable(opcode, it->second); if (token->aType == Token::VARIABLE_FUNCTION) { // Copy the original variable into our new one opcode->addInterop(new ByteOperation(OP_MOV)); uint src = getVariableID(parser, token->token); opcode->addInterop(new DwordOperation(&it->second)); opcode->addInterop(new DwordOperation(&src)); } else { // The value to be copied is a numerical value. byte operation = 0; void *dword = malloc(4); if (it->first->token->aType == Token::VARIABLE_INT) { operation = OP_MOVI; *(int*)dword = atoi(token->token.c_str()); } else if (token->aType == Token::VARIABLE_FLOAT) { operation = OP_MOVF; *(float*)dword = (float)atof(token->token.c_str()); } else { throw InvalidTokenException("Invalid token expression of type"); } opcode->addInterop(new ByteOperation(operation)); opcode->addInterop(new DwordOperation(&it->second)); opcode->addInterop(new DwordOperation(dword)); free(dword); } } }
/** * @brief Loads a variable into memory. * * Checks to see if this variable is loaded, and if not, checks that it exists in the file * Use this method when the variable to load is of type int * * @param variable * @return status of the operation */ long Model::loadVariableInt(const std::string& variable) { //first, check to determine whether variable is already loaded if (variableDataInt.find(variable) != variableDataInt.end()) return true; //first check if the variable exists in the file!! if (!this->doesVariableExist(variable)) { return FileReader::VARIABLE_DOES_NOT_EXIST; } std::vector<int> * data = getVariableInt(variable); long id = getVariableID(variable); if (data->size() > 0) { variableDataInt[variable] = data; variableDataIntByID[id] = data; } //else return false; return FileReader::OK; }