meridiandsl.cpp
来自「件主要用于帮助计算机爱好者学习蚁群算法时做有关蚁群算法的试验。蚁群算法作为一种优」· C++ 代码 · 共 2,208 行 · 第 1/5 页
CPP
2,208 行
ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_int(ps, (int)ceil(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case FLOOR: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_int(ps, (int)floor(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case SIN: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, sin(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case COS: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, cos(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case TAN: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, tan(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case ASIN: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, asin(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case ACOS: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, acos(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case ATAN: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, atan(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case LOG_OP: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, log(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case EXP: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, exp(nextNode->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case POW: { ASTNode* nextNode_1 = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); ASTNode* nextNode_2 = eval(ps, cur_node->val.n_val.n_param_2, recurse_count + 1); if (nextNode_1->type != DOUBLE_TYPE || nextNode_2->type != DOUBLE_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, pow(nextNode_1->val.d_val, nextNode_2->val.d_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case DBL: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != INT_TYPE) { DSL_ERROR("Integer type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_double(ps, (double)(nextNode->val.i_val));/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case ARRAY_SIZE: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } ASTNode* ret = mk_int(ps, nextNode->val.a_val.a_vector->size());/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case DNS_LOOKUP: { return handleDNSLookup(ps, cur_node, recurse_count); } case DNS_ADDR: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != INT_TYPE) { DSL_ERROR("Double type expected\n"); return ps->empty_token(); } uint32_t tmpAddr = htonl(nextNode->val.i_val); char* addrStr = inet_ntoa(*((struct in_addr*)&tmpAddr)); if (addrStr == NULL) { DSL_ERROR("inet_ntoa failed\n"); return ps->empty_token(); } string tmpString = addrStr; // mk_string creates a copy of the string; ASTNode* ret = mk_string(ps, &tmpString);/* if (ret == NULL) { DSL_ERROR("Out of memory\n"); ret = ps->empty_token(); }*/ return ret; } case PUSH_BACK: { ASTNode* nextNode_1 = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); ASTNode* nextNode_2 = eval(ps, cur_node->val.n_val.n_param_2, recurse_count + 1); if (nextNode_1->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } if (nextNode_1->val.a_val.a_type == ADT_TYPE) { if (*(nextNode_1->val.a_val.a_adt_name) != *(nextNode_2->val.adt_val.adt_type_name)) { DSL_ERROR("Wrong array type encountered\n"); return ps->empty_token(); } } else if (nextNode_1->val.a_val.a_type != nextNode_2->type) { DSL_ERROR("Wrong array type encountered\n"); return ps->empty_token(); } ASTNode* newASTNode = nextNode_1->val.a_val.a_var_table->ASTCreate( nextNode_1->val.a_val.a_type, nextNode_1->val.a_val.a_adt_name, EMPTY_TYPE, 0); if (newASTNode == NULL) { return ps->empty_token(); } if (ps->get_var_table()->updateADT(newASTNode, nextNode_2) == -1) { DSL_ERROR("Array entry assign failed\n"); return ps->empty_token(); } nextNode_1->val.a_val.a_vector->push_back(newASTNode); return ps->empty_token(); } case POP_BACK: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } if (!(nextNode->val.a_val.a_vector->empty())) { nextNode->val.a_val.a_vector->pop_back(); } return ps->empty_token(); } case GET_SELF: { return handleGetSelf(ps); } case RING_GT: { return handleRingGT(ps, cur_node, recurse_count); } case RING_GE: { return handleRingGE(ps, cur_node, recurse_count); } case RING_LT: { return handleRingLT(ps, cur_node, recurse_count); } case RING_LE: { return handleRingLE(ps, cur_node, recurse_count); } case GET_DISTANCE_TCP: { return handleGetDistTCP(ps, cur_node, recurse_count); } case GET_DISTANCE_DNS: { return handleGetDistDNS(ps, cur_node, recurse_count); } case GET_DISTANCE_PING: { return handleGetDistPing(ps, cur_node, recurse_count); } case GET_DISTANCE_ICMP: {#ifdef PLANET_LAB_SUPPORT return handleGetDistICMP(ps, cur_node, recurse_count);#else DSL_ERROR("ICMP not supported\n"); return ps->empty_token();#endif } case ARRAY_INTERSECT: { ASTNode* nextNode_1 = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); ASTNode* nextNode_2 = eval(ps, cur_node->val.n_val.n_param_2, recurse_count + 1); if (nextNode_1->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } if (nextNode_1->type != nextNode_2->type) { DSL_ERROR("Arrays must be of the same type\n"); return ps->empty_token(); } if (nextNode_1->val.a_val.a_type == ADT_TYPE) { if (*(nextNode_1->val.a_val.a_adt_name) != *(nextNode_2->val.a_val.a_adt_name)) { DSL_ERROR("Arrays must be of the same type\n"); return ps->empty_token(); } } // Create return array ASTNode* retVar = ASTCreate(ps, ARRAY_TYPE, nextNode_1->val.a_val.a_adt_name, nextNode_1->val.a_val.a_type, 0); if (retVar->type == EMPTY_TYPE) { return ps->empty_token(); } vector<ASTNode*>* vect_1 = nextNode_1->val.a_val.a_vector; vector<ASTNode*>* vect_2 = nextNode_2->val.a_val.a_vector; for (u_int i = 0; i < vect_1->size(); i++) { for (u_int j = 0; j < vect_2->size(); j++) { if (ADTEqual((*vect_1)[i], (*vect_2)[j])) { if (uniqueAdd(ps, retVar->val.a_val.a_vector, (*vect_1)[i]) == -1) { return ps->empty_token(); } break; } } } return retVar; } case ARRAY_MAX_OFFSET: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } vector<ASTNode*>* curVect = nextNode->val.a_val.a_vector; int offset = 0; if (arrayLargestOffset( nextNode->val.a_val.a_type, curVect, &offset) == -1) { return ps->empty_token(); } return mk_int(ps, offset); } case ARRAY_MIN_OFFSET: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } vector<ASTNode*>* curVect = nextNode->val.a_val.a_vector; int offset = 0; if (arraySmallestOffset( nextNode->val.a_val.a_type, curVect, &offset) == -1) { return ps->empty_token(); } return mk_int(ps, offset); } case ARRAY_AVG: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } vector<ASTNode*>* curVect = nextNode->val.a_val.a_vector; if (curVect->size() == 0) { return mk_double(ps, 0.0); } if (nextNode->val.a_val.a_type == INT_TYPE) { double total = 0.0; for (u_int i = 0; i < curVect->size(); i++) { ASTNode* curVectNode = (*curVect)[i]; if (curVectNode->type != INT_TYPE) { DSL_ERROR("Ill-formed array\n"); return ps->empty_token(); } total += (double)(curVectNode->val.i_val); } return mk_double(ps, total / (double)(curVect->size())); } else if (nextNode->val.a_val.a_type == DOUBLE_TYPE) { double total = 0.0; for (u_int i = 0; i < curVect->size(); i++) { ASTNode* curVectNode = (*curVect)[i]; if (curVectNode->type != DOUBLE_TYPE) { DSL_ERROR("Ill-formed array\n"); return ps->empty_token(); } total += curVectNode->val.d_val; } return mk_double(ps, total / (double)(curVect->size())); } DSL_ERROR("Must be int or double array\n"); return ps->empty_token(); } case ARRAY_MAX: { ASTNode* nextNode = eval(ps, cur_node->val.n_val.n_param_1, recurse_count + 1); if (nextNode->type != ARRAY_TYPE) { DSL_ERROR("Array type expected\n"); return ps->empty_token(); } vector<ASTNode*>* curVect = nextNode->val.a_val.a_vector; int offset = 0; if (arrayLargestOffset( nextNode->val.a_val.a_type, curVect, &offset) == -1) { return ps->empty_token(); } // This should not be possible if (offset >= (int)(curVect->size())) { return ps->empty_token(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?