CItemSelector::CSymbolInterval CItemSelector::GetSelectionInfo() const { if( start->GetParent() == 0 ) { int startIdx = -1; while( &content[++startIdx] != start->CurrentLine ) { } int endIdx = -1; while ( &content[++endIdx] != end->CurrentLine ) { } CSymbolPosition tmpStart( *start ); CSymbolPosition tmpEnd( *end ); if( startIdx > endIdx || tmpStart.Index > tmpEnd.Index ) { std::swap( tmpStart, tmpEnd ); std::swap( startIdx, endIdx ); } if( tmpStart.CurrentLine == &content[content.size() - 1] && tmpStart.Index == tmpStart.CurrentLine->Length() ) { return std::make_pair( CSymbolPosition( -1, tmpStart ), CSymbolPosition( -1, tmpEnd ) ); } else { goToNextSymbol( startIdx, tmpStart ); goToPrevSymbol( endIdx, tmpEnd ); if( startIdx > endIdx ) { return std::make_pair( CSymbolPosition( -1, tmpStart ), CSymbolPosition( -1, tmpEnd ) ); } else { return std::make_pair( tmpStart, tmpEnd ); } } } else { if( start->Index > end->Index ) { return std::make_pair( *end, *start ); } else { return std::make_pair( *start, *end ); } } }
bool Interpreter::isConstMod() { goToNextSymbol(); if (!(*iter < s->size())) return false; if ((*s)[*iter] != '$') return false; ++(*iter); return true; }
bool Interpreter::isConstName() { int it = *iter; goToNextSymbol(); if (!(it < s->size())) return true; if ((*s)[it] != '(') return true; it++; goToNextSymbol(); if (!(it < s->size())) return false; if ((*s)[it] == ')') { it++; *iter = it; return true; } else return false; }
void Interpreter::makePolandList(EvalList *list) { bool is_function = (v) ? true : false; goToNextSymbol(); if (*iter >= s->size()) throw_(invalid_expression); Interpreter::HistoryStack h_stack(this); Interpreter::PolStack p_stack(this); for (; (*iter) < s->size(); goToNextSymbol()) { goToNextSymbol(); if (!(*iter < s->size())) break; switch (h_stack.getLastType()) { case val: switch ((*s)[*iter]) { case '(': { { string* pr = new string("*"); p_stack.push(Drobot::global_tree.find(pr), list); delete pr; p_stack.push(Drobot::ope_br, list); h_stack.pop(); h_stack.push(bin_op); h_stack.incLastNumberOfArgumets(); h_stack.push(op_br); ++(*iter); continue; } } case ')': p_stack.push(')', list); h_stack.pop(); if (h_stack.getLastType() != op_br) throw_(invalid_expression); h_stack.pop(); switch (h_stack.getLastType()) { case bin_op: h_stack.pop(); h_stack.push(val); ++(*iter); continue; case nothing: h_stack.push(val); ++(*iter); continue; case val: throw(you_are_fool, p); continue; case func: { h_stack.check(); h_stack.incLastNumberOfArgumets(); short num = h_stack.getNumOfArguments(); Quantum* tmp = Drobot::global_tree.find(h_stack.getName(), num); if (!tmp) { if (num == 1) { if (find(h_stack.getName()) && !h_stack.isUnchang()) { list->add(new Variable(h_stack.getName()), h_stack.getNode()); string* pr = new string("*"); p_stack.push(Drobot::global_tree.find(pr), list); delete pr; } else { tmp = Drobot::global_tree.find(h_stack.getName(), 0); if (!tmp) throw_(unknown_name); if (h_stack.isUnchang()) tmp = new Const(dynamic_cast<Const*>(tmp)); list->add(tmp, h_stack.getNode()); string* pr = new string("*"); p_stack.push(Drobot::global_tree.find(pr), list); delete pr; } } else throw_(unknown_name); } else { if (h_stack.isUnchang()) tmp = new Function(dynamic_cast<Function*>(tmp)); p_stack.push(tmp, list); } h_stack.pop(); if (h_stack.getLastType() == bin_op) h_stack.pop(); h_stack.push(val); ++(*iter); continue; } case op_br: h_stack.push(val); ++(*iter); continue; } continue; case ',': p_stack.push(',', list); h_stack.pop(); if (h_stack.getLastType() != op_br) throw_(invalid_expression); if (!h_stack.isFuncPre()) throw_(invalid_expression); h_stack.pop(); h_stack.incLastNumberOfArgumets(); h_stack.push(op_br); ++(*iter); continue; case '$': throw_(bad_position_of_reserved_symbol); continue; default: { char c = (*s)[*iter]; if (c <= '9' && c >= '0') { TInternalData* tmp = new TInternalData(0.0); getValue(tmp); string* pr = new string("*"); p_stack.push(Drobot::global_tree.find(pr), list); list->add(new Value(tmp)); delete pr; continue; } bool found_in_ar_operators = false; for (int i = 0; i < strlen(ar_operators) && !found_in_ar_operators; ++i) { if (c == ar_operators[i]) { found_in_ar_operators = true; string* st = new string; st->push_back(ar_operators[i]); p_stack.push(Drobot::global_tree.find(st), list); delete st; h_stack.pop(); h_stack.push(bin_op); h_stack.incLastNumberOfArgumets(); ++(*iter); continue; } } if (!found_in_ar_operators) { string* st = new string; getName(st); bool uncheng = isConstMod(); goToNextSymbol(); string* pr = new string("*"); p_stack.push(Drobot::global_tree.find(pr), list); delete pr; if (!(*iter < s->size()) || isConstName()) { if (find(st)) { if (uncheng) { Quantum* f = Drobot::global_tree.find(st, 0); if (!f) { delete st; throw_(invalid_expression); } else { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } } else { list->add(new Variable(st)); delete st; } } else { Quantum* f = Drobot::global_tree.find(st, 0); if (uncheng) { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } else { delete st; list->add(f); } } } else { h_stack.pop(); h_stack.push(bin_op); h_stack.push(func, st, list->_tail, uncheng); p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); } } continue; } } continue; case bin_op: switch ((*s)[*iter]) { case '(': p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); continue; case ')': throw_(invalid_expression); continue; case ',': throw_(invalid_expression); continue; case '$': throw_(bad_position_of_reserved_symbol); continue; default: { char c = (*s)[*iter]; if (c <= '9' && c >= '0') { TInternalData* tmp = new TInternalData(0.0); getValue(tmp); list->add(new Value(tmp)); h_stack.pop(); h_stack.push(val); continue; } for (int i = 0; i < strlen(ar_operators); ++i) if (c == ar_operators[i]) throw_(invalid_expression); string* st = new string; getName(st); bool uncheng = isConstMod(); goToNextSymbol(); if (!(*iter < s->size()) || isConstName()) { h_stack.pop(); h_stack.push(val); if (find(st)) { if (uncheng) { Quantum* f = Drobot::global_tree.find(st, 0); if (!f) { delete st; throw_(invalid_expression); } else { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } } else { list->add(new Variable(st)); delete st; } } else { Quantum* f = Drobot::global_tree.find(st, 0); if (uncheng) { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } else { delete st; list->add(f); } } } else { h_stack.push(func, st, list->_tail, uncheng); p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); } } } continue; case nothing: switch ((*s)[*iter]) { case '(': p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); continue; case ')': throw_(invalid_bracket_position); continue; case ',': throw_(invalid_expression); continue; case '$': throw_(bad_position_of_reserved_symbol); continue; default: { char c = (*s)[*iter]; if (c <= '9' && c >= '0') { TInternalData* tmp = new TInternalData(0.0); getValue(tmp); list->add(new Value(tmp)); h_stack.push(val); continue; } if (c == '-') { string* pr = new string("*"); TInternalData* ch = new TInternalData(-1); list->add(new Value(ch)); p_stack.push(Drobot::global_tree.find(pr), list); h_stack.push(bin_op); delete pr; ++(*iter); continue; } if (c == '+') { ++(*iter); continue; } for (int i = 0; i < strlen(ar_operators); ++i) if (c == ar_operators[i]) throw_(invalid_expression); string* st = new string; getName(st); bool uncheng = isConstMod(); goToNextSymbol(); if (!(*iter < s->size()) || isConstName()) { h_stack.push(val); if (find(st)) { if (uncheng) { Quantum* f = Drobot::global_tree.find(st, 0); if (!f) { delete st; throw_(invalid_expression); } else { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } } else { list->add(new Variable(st)); delete st; } } else { Quantum* f = Drobot::global_tree.find(st, 0); if (uncheng) { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } else { delete st; list->add(f); } } } else { h_stack.push(func, st, list->_tail, uncheng); p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); } } } continue; case op_br: switch ((*s)[*iter]) { case '(': p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); (*iter)++; continue; case ')': p_stack.push(')', list); h_stack.pop(); ++(*iter); continue; case ',': throw_(invalid_expression); continue; case '$': throw_(bad_position_of_reserved_symbol); continue; default: { char c = (*s)[*iter]; if (c <= '9' && c >= '0') { TInternalData* tmp = new TInternalData(0.0); getValue(tmp); list->add(new Value(tmp)); h_stack.push(val); continue; } if (c == '-') { string* pr = new string("*"); TInternalData* ch = new TInternalData(-1); list->add(new Value(ch)); p_stack.push(Drobot::global_tree.find(pr), list); h_stack.push(bin_op); delete pr; ++(*iter); continue; } if (c == '+') { ++(*iter); continue; } for (int i = 0; i < strlen(ar_operators); ++i) if (c == ar_operators[i]) throw_(invalid_expression); string* st = new string; getName(st); bool uncheng = isConstMod(); goToNextSymbol(); if (!(*iter < s->size()) || isConstName()) { h_stack.push(val); if (find(st)) { if (uncheng) { Quantum* f = Drobot::global_tree.find(st, 0); if (!f) { delete st; throw_(invalid_expression); } else { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } } else { list->add(new Variable(st)); delete st; } } else { Quantum* f = Drobot::global_tree.find(st, 0); if (uncheng) { delete st; list->add(new Const(dynamic_cast<Const*>(f))); } else { delete st; list->add(f); } } } else { h_stack.push(func, st, list->_tail, uncheng); p_stack.push(Drobot::ope_br, list); h_stack.push(op_br); ++(*iter); } /*Quantum* f = Drobot::global_tree.find(st); if (!f) { if (!is_function) throw_(unknown_name, p); if (!find(v, st)) throw_(unknown_name, p); list->add(new Variable(st)); h_stack.push(val); delete st; continue; } if (f->GetType() == con) { if (isConstMod(s, iter)) list->add(new Const(dynamic_cast<Const*>(f))); else list->add(f); delete st; h_stack.push(val); continue; } bool uncheng = isConstMod(s, iter); goToNextSymbol(s, iter); if (!(*iter < s->size())) throw_(wrong_number_of_argument, p); if ((*s)[*iter] != '(') throw_(invalid_operand, p); if (uncheng) p_stack.push(new Function(dynamic_cast<Function*>(f)), list); else p_stack.push(f, list); p_stack.push(Drobot::ope_br, list); h_stack.push(func, f); h_stack.push(op_br); (*iter)++; continue;*/ } } } } if (!h_stack.isCorrect()) throw_(invalid_expression); for (; !p_stack.isEmpty();) if (p_stack.isOpenedBr()) throw_(invalid_expression); else list->add(p_stack.pop()); }