#include "expression.hpp" #include "parser.hpp" #include "print.h" #include "syntaxer.hpp" #include #include #include #include namespace Syntax { struct Range { int start, finish; Range(void) { } Range(int start, int finish) : start(start), finish(finish) { } }; static int add_bracket_to(std::vector& tv, const int location, const int direction); static int rank_op(const Expression::Token::Operator o) { switch (o) { case Expression::Token::ADD: case Expression::Token::SUB: return 0; case Expression::Token::MUL: case Expression::Token::DIV: return 1; case Expression::Token::POW: return 2; default: return -1; } } static int same_lvl_opa_location(const std::vector& tv, const int location) { for (int i = location - 1, lv = 0; i >= 0; i--) { const Expression::Token &t = tv[i]; if (t.t != Expression::Token::OPERATOR) { continue; } if (t.v.o == Expression::Token::CPA) { lv++; } if (t.v.o == Expression::Token::OPA) { if (!lv) { return i; } else { lv--; } } } return -1; } static int same_lvl_cpa_location(const std::vector& tv, const int location) { for (int i = location + 1, lv = 0, l = tv.size(); i < l; i++) { const Expression::Token &t = tv[i]; if (t.t != Expression::Token::OPERATOR) { continue; } if (t.v.o == Expression::Token::OPA) { lv++; } if (t.v.o == Expression::Token::CPA) { if (!lv) { return i; } else { lv--; } } } return tv.size() - 1; } static void highlight_issue(const std::vector& tv, const int location) { int start = location - 7 < 0 ? 0 : location - 7; int finish = (size_t) location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7; std::cout << " >>> "; for (int i = start; i <= finish; i++) { if (i == location) { std::cout << "\033[31m" << *tv[i].to_math_str() << "\033[0m "; } else { std::cout << *tv[i].to_math_str() << " "; } } std::cout << "\n"; } // get mop = Math OPerator location (=absolute index in std::vector) // mapper of 'priority vector index' -> 'token vector index' static int get_mop_location(const std::vector& v, int position) { int i = 0, l = v.size(), p = -1; for (; ({if (!(i < l)) return i;}), p += v[i].t == Expression::Token::OPERATOR && v[i].v.o != Expression::Token::OPA && v[i].v.o != Expression::Token::CPA, p != position; i++); return i; } static int get_left_location(const std::vector& v, int location) { for (int i = location - 1; i >= 0; i--) { if (v[i].t != Expression::Token::OPERATOR && v[i].t != Expression::Token::NAT) { return i; } } return -1; } static int get_right_location(const std::vector& v, int location) { for (int i = location + 1, l = v.size(); i < l; i++) { if (v[i].t != Expression::Token::OPERATOR && v[i].t != Expression::Token::NAT) { return i; } } return -1; } static int min_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv) { int m = 3; for (int i = r.start; i <= r.finish; i++) { if (mp[i] != base_lv) { continue; } int rank = rank_op(tv[get_mop_location(tv, i)].v.o); m = rank < m ? rank : m; } return m; } static int get_operator_weight(const std::vector& v, int location) { int weight = 0; switch (const Expression::Token &p = v[get_left_location(v, location)]; p.t) { case Expression::Token::VAR: case Expression::Token::CONST: break; case Expression::Token::CV: weight += p.v.cv.w; break; default: pr_warn("Invalid token found to the left of operator"); break; } switch (const Expression::Token &p = v[get_right_location(v, location)]; p.t) { case Expression::Token::VAR: case Expression::Token::CONST: break; case Expression::Token::CV: weight += p.v.cv.w; break; default: pr_warn("Invalid token found to the left of operator"); break; } return weight; } static int get_min_index(const std::vector& v) { int idx = 0; for (int i = 1, l = v.size(); i < l; i++) { if (v[i] < v[idx]) { idx = i; } } return idx; } static int get_max_index(const std::vector& v) { int idx = 0; for (int i = 1, l = v.size(); i < l; i++) { if (v[i] > v[idx]) { idx = i; } } return idx; } static int op_weight(enum Expression::Token::Operator o) { switch (o) { case Expression::Token::OPA: case Expression::Token::CPA: case Expression::Token::NOP: pr_warn("Unweightable operator - this is probably a bug!"); return 0; case Expression::Token::ADD: return 1; case Expression::Token::SUB: return 1; case Expression::Token::MUL: return 2; case Expression::Token::DIV: return 2; case Expression::Token::POW: return 3; default: pr_warn("Impossible operator - this is a bug!"); return 0; } } int op_weight(DynamicPipeline::CommandType o) { return op_weight((enum Expression::Token::Operator) (o + 2)); } static int write_operation( std::vector& ov, std::vector& cv, std::vector& mv, std::vector& mp, int position) { int location = get_mop_location(mv, position); int left_location = get_left_location(mv, location); int right_location = get_right_location(mv, location); std::vector hl; hl.emplace_back(left_location); hl.emplace_back(location); hl.emplace_back(right_location); Parser::print_math_vector(mv, hl); Expression::Token &o = mv[location], &l = mv[left_location], &r = mv[right_location]; #if LAB2_EXCLUDE_COMPTIME_OP_WRITES == 1 if (l.t == Expression::Token::CONST && r.t == Expression::Token::CONST) { pr_debug("CONST folding"); switch (o.v.o) { case Expression::Token::ADD: o.v.d = l.v.d + r.v.d; break; case Expression::Token::SUB: o.v.d = l.v.d - r.v.d; break; case Expression::Token::MUL: o.v.d = l.v.d * r.v.d; break; case Expression::Token::DIV: if (r.v.d != 0.0) { o.v.d = l.v.d / r.v.d; } else { pr_warn("Right operand is zero, invalid operation"); return 1; } break; case Expression::Token::POW: o.v.d = std::pow(l.v.d, r.v.d); break; default: pr_warn("Impossible operator, this is a bug!"); break; } l.t = Expression::Token::NAT; r.t = Expression::Token::NAT; o.t = Expression::Token::CONST; // sync &cv operator cv[location].t = Expression::Token::CONST; cv[location].v.d = o.v.d; } else { #endif #if LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO == 1 if (r.t == Expression::Token::CONST && r.v.d == 0.0 && o.v.o == Expression::Token::DIV) { pr_warn("Right operand is zero, operation will raise exception during runtime"); return 1; } #endif int this_op_weight = get_operator_weight(mv, location) + op_weight(o.v.o); int il = l.t == Expression::Token::CV ? l.v.cv.i : left_location; int ir = r.t == Expression::Token::CV ? r.v.cv.i : right_location; // write new operation int this_ov_index = ov.size(); ov.emplace_back(o.v.o, l.t == Expression::Token::CV ? Expression::Operation::DERIVED : Expression::Operation::NORMAL, il, r.t == Expression::Token::CV ? Expression::Operation::DERIVED : Expression::Operation::NORMAL, ir, this_op_weight); // update mv l.t = Expression::Token::NAT; r.t = Expression::Token::NAT; o.t = Expression::Token::CV; o.v.cv.w = this_op_weight; o.v.cv.i = this_ov_index; #if LAB2_EXCLUDE_COMPTIME_OP_WRITES == 1 } #endif // update mp mp.erase(mp.begin() + position, mp.begin() + position + 1); return 0; } static int add_bracket_to(std::vector& tv, const int location, const int direction) { int i = location + direction, l = tv.size(), lv = 0; if (direction == -1) { for (; i >= 0; i--) { if (tv[i].t == Expression::Token::OPERATOR) { if (tv[i].v.o == Expression::Token::CPA) { lv++; } if (!lv) { tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA)); return i + 1; } if (tv[i].v.o == Expression::Token::OPA) { lv--; } } } tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA)); return 0; } else if (direction == 1) { for (; i < l; i++) { if (tv[i].t == Expression::Token::OPERATOR) { if (tv[i].v.o == Expression::Token::OPA) { lv++; } if (!lv) { tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA)); return i; } if (tv[i].v.o == Expression::Token::CPA) { lv--; } } } tv.emplace_back(Expression::Token::CPA); return i; } else { pr_warn("Invalid direction given, this is a big bug!"); return -1; } } static int add_opa_after_le_io(std::vector &tv, const int location) { int base_rank = rank_op(tv[location].v.o); for (int i = location - 1, lv = 0; i >= 0; i--) { Expression::Token &t = tv[i]; if (t.t == Expression::Token::OPERATOR) { if (t.v.o == Expression::Token::CPA) { lv++; } if (lv <= 0 && (base_rank - rank_op(t.v.o)) >= 0) { tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA)); return i + 1; } if (t.v.o == Expression::Token::OPA) { lv--; } } } tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA)); return 0; } static int add_cpa_before_le_io(std::vector &tv, const int location) { int base_rank = rank_op(tv[location].v.o); for (int i = location + 1, lv = 0, l = tv.size(); i < l; i++) { Expression::Token &t = tv[i]; if (t.t == Expression::Token::OPERATOR) { if (t.v.o == Expression::Token::OPA) { lv++; } if (lv <= 0 && (rank_op(t.v.o) - base_rank) <= 0) { tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA)); return i; } if (t.v.o == Expression::Token::CPA) { lv--; } } } tv.push_back(Expression::Token(Expression::Token::CPA)); return tv.size() - 1; } static void create_prios_vector(const std::vector& tv, std::vector& pv) { for (int i = 0, cp = 0, l = tv.size(); i < l; i++) { if (tv[i].t == Expression::Token::OPERATOR) { switch (tv[i].v.o) { case Expression::Token::OPA: cp++; break; case Expression::Token::CPA: cp--; break; case Expression::Token::ADD: case Expression::Token::SUB: case Expression::Token::MUL: case Expression::Token::DIV: case Expression::Token::POW: pv.emplace_back(cp); break; default: pr_warn("Token vector has broken operator at " << i); break; } } } } static void find_top_prio_op_range(const std::vector& mp, int& start, int& finish) { for (int i = 0, l = mp.size(), prio = -1; i < l; i++) { if (mp[i] > prio) { prio = mp[i]; start = finish = i; } else if (mp[i] == prio && finish == i - 1) { finish = i; } } } // find operation ranges that are of selected priority (target_prio) // accounting for future token convolution into compound values static void find_select_prio_op_ranges_after_conv(const std::vector& mp, std::vector& ranges, int target_prio) { for (int i = 0, l = mp.size(), recording_range = 0; i < l; i++) { if (mp[i] == target_prio && !recording_range) { recording_range = 1; ranges.emplace_back(i, i); } else if (mp[i] == target_prio && recording_range) { ranges.back().finish = i; } else if (mp[i] < target_prio && recording_range) { recording_range = 0; } } } static void find_parent_level_range(const std::vector& tv, Range &p, const Range &c) { // left side for (int i = get_mop_location(tv, c.start), pc = c.start, lv = 0; i >= 0; i--) { if (tv[i].is_opa()) { lv--; if (lv == -2) { p.start = pc; goto right; } } else if (tv[i].is_math_op()) { pc--; } else if (tv[i].is_cpa()) { lv++; } } p.start = 0; right: // right side int i, pc, lv; for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; (size_t) i < tv.size(); i++) { if (tv[i].is_cpa()) { lv--; if (lv == -2) { p.finish = pc; break; } } else if (tv[i].is_math_op()) { pc++; } else if (tv[i].is_opa()) { lv++; } } p.finish = pc - 1; } static void rank_inner_ops(std::vector& inner_top_ops, const std::vector& mv, const int start, const int finish) { for (int i = 0, lvl = 0; i < finish - start + 1; i++) { int idx = get_mop_location(mv, start + i); switch (mv[idx].v.o) { case Expression::Token::ADD: case Expression::Token::SUB: if (lvl == 0) inner_top_ops.push_back(i); break; case Expression::Token::MUL: case Expression::Token::DIV: if (lvl < 1) { lvl = 1; inner_top_ops.clear(); } if (lvl == 1) inner_top_ops.push_back(i); break; case Expression::Token::POW: if (lvl < 2) { lvl = 2; inner_top_ops.clear(); } if (lvl == 2) inner_top_ops.push_back(i); break; default: pr_warn("Operator is unrankable"); break; } } } static void weight_operators(std::vector& operator_weights, const std::vector& mv, const std::vector& inner_top_ops, const int start) { for (int i = 0, l = inner_top_ops.size(); i < l; i++) { int operator_location = get_mop_location(mv, start + inner_top_ops[i]); operator_weights.push_back(get_operator_weight(mv, operator_location)); } } static Expression::Token::Operator get_prior_mop(const std::vector& tv, const int location) { for (int i = location - 1, lv = 0; i >= 0; i--) { const Expression::Token &t = tv[i]; if (t.t == Expression::Token::OPERATOR) { if (t.v.o == Expression::Token::CPA) { lv++; } else if (t.v.o == Expression::Token::OPA) { lv--; if (lv == -2) { // enclosed in another pair of brackets, safe to assume ADD return Expression::Token::ADD; } } else if (lv < 0) { return t.v.o; } } } return Expression::Token::ADD; } static void open_brackets(std::vector& tv, const Range &r) { Range brackets( same_lvl_opa_location(tv, get_mop_location(tv, r.start)), same_lvl_cpa_location(tv, get_mop_location(tv, r.finish)) ); std::vector hl; hl.emplace_back(brackets.start); hl.emplace_back(brackets.finish); Parser::print_math_vector(tv, hl); if (Expression::Token::Operator po = get_prior_mop(tv, get_mop_location(tv, r.start)); po == Expression::Token::SUB || po == Expression::Token::DIV) { for (int i = r.start; i <= r.finish; i++) { Expression::Token &t = tv[get_mop_location(tv, i)]; switch (t.v.o) { case Expression::Token::ADD: t.v.o = Expression::Token::SUB; break; case Expression::Token::SUB: t.v.o = Expression::Token::ADD; break; case Expression::Token::MUL: t.v.o = Expression::Token::DIV; break; case Expression::Token::DIV: t.v.o = Expression::Token::MUL; break; default: break; } } } tv.erase(tv.begin() + brackets.finish); tv.erase(tv.begin() + brackets.start); } static int get_max_neighbour_rank(const std::vector& tv, const Range &r) { int rank = -1; int left_bracket = same_lvl_opa_location(tv, get_mop_location(tv, r.start)); if (left_bracket - 1 >= 0) { if (int ro = rank_op(tv[left_bracket - 1].v.o); ro > rank) { rank = ro; } } int right_bracket = same_lvl_cpa_location(tv, get_mop_location(tv, r.finish)); if ((size_t) (right_bracket + 1) < tv.size()) { if (int ro = rank_op(tv[right_bracket + 1].v.o); ro > rank) { rank = ro; } } return rank; } static void remove_extra_brackets(std::vector& tv) { pr_debug("Processing: removing extra brackets"); std::vector mp; create_prios_vector(tv, mp); if (mp.size() == 0) { return; } // for everything excluding level 0 for (int current_prio = mp[get_max_index(mp)]; current_prio > 0; current_prio--) { mp.clear(); create_prios_vector(tv, mp); std::vector ranges; find_select_prio_op_ranges_after_conv(mp, ranges, current_prio); for (int r_no = ranges.size() - 1; r_no >= 0; r_no--) { mp.clear(); create_prios_vector(tv, mp); ranges.clear(); find_select_prio_op_ranges_after_conv(mp, ranges, current_prio); const Range &r = ranges[r_no]; { // extra logging pr_debug("Considering option:"); std::vector hl; hl.emplace_back(get_mop_location(tv, r.start)); hl.emplace_back(get_mop_location(tv, r.finish)); Parser::print_math_vector(tv, hl); } Range parent_r; find_parent_level_range(tv, parent_r, r); int min_child_rank = min_rank(tv, mp, r, current_prio); if (int nr = get_max_neighbour_rank(tv, r); nr > min_child_rank) { pr_debug("Fails in neighbour rank: max_neighbour=" << nr << " > min_child=" << min_child_rank); continue; } // check specifically if the opening undoes applied fixes if (Expression::Token::is_negative(get_prior_mop(tv, get_mop_location(tv, r.start)))) { pr_debug("Fails in prior mop is_negative"); continue; } open_brackets(tv, r); } } } static void process_remove_empty_brackets(std::vector& tv) { pr_debug("Pre-process: remove empty brackets"); int fix = -1; while (fix) { fix = 0; for (int i = 0; i < (long) tv.size() - 1; i++) { if (tv[i].t == Expression::Token::OPERATOR && tv[i].v.o == Expression::Token::OPA && tv[i+1].t == Expression::Token::OPERATOR && tv[i+1].v.o == Expression::Token::CPA) { std::vector highlight; highlight.emplace_back(i); highlight.emplace_back(i+1); Parser::print_math_vector(tv, highlight); tv.erase(tv.begin() + i, tv.begin() + i + 2); i--; fix++; } } } } static int check_bracket_match(const std::vector& tv) { int problem_cnt = 0; std::stack s; for (int i = 0, l = tv.size(); i < l; i++) { if (tv[i].t == Expression::Token::OPERATOR) { switch (tv[i].v.o) { case Expression::Token::OPA: s.push(i); break; case Expression::Token::CPA: if (s.size()) { s.pop(); } else { pr_warn("Missing opening bracket at token " << i); problem_cnt++; highlight_issue(tv, i); } break; default: break; } } } for (int i = 0, l = s.size(); i < l; i++) { problem_cnt++; pr_warn("Opening bracket at token " << s.top() << " expected a closing bracket, but none found"); highlight_issue(tv, s.top()); s.pop(); } return problem_cnt; } static int check_start_finish_validity(const std::vector& tv) { int problem_cnt = 0; if (tv.size()) { int pc = problem_cnt; switch (tv[0].t) { case Expression::Token::VAR: case Expression::Token::CONST: break; case Expression::Token::OPERATOR: switch (tv[0].v.o) { case Expression::Token::OPA: break; case Expression::Token::CPA: pr_warn("Expected value or opening bracket at token 0, got closing bracket"); problem_cnt++; break; default: pr_warn("Expected value or opening bracket at token 0, got operator"); problem_cnt++; break; } break; default: pr_warn("Impossible token type, this is a bug!"); break; } if (pc != problem_cnt) { highlight_issue(tv, 0); pc = problem_cnt; } switch (tv.back().t) { case Expression::Token::VAR: case Expression::Token::CONST: break; case Expression::Token::OPERATOR: switch (tv.back().v.o) { case Expression::Token::CPA: break; case Expression::Token::OPA: pr_warn("Expected value or closing bracket at token " << tv.size() - 1 << ", got opening bracket"); problem_cnt++; break; default: pr_warn("Expected value or closing bracket at token " << tv.size() - 1 << ", got operator"); problem_cnt++; break; } break; default: pr_warn("Impossible token type, this is a bug!"); break; } if (pc != problem_cnt) { highlight_issue(tv, tv.size()-1); } } return problem_cnt; } static int check_token_type_sequence(const std::vector& tv) { int problem_cnt = 0; if (tv.size() > 1) { for (int i = 0, l = tv.size() - 1, pc; pc = problem_cnt, i < l; i++) { switch (tv[i].t) { case Expression::Token::VAR: case Expression::Token::CONST: switch (tv[i+1].t) { case Expression::Token::OPERATOR: switch (tv[i+1].v.o) { case Expression::Token::OPA: pr_warn("Expected operator at token " << i + 1 << ", got opening bracket"); problem_cnt++; break; case Expression::Token::CPA: break; default: break; } break; case Expression::Token::VAR: pr_warn("Expected operator at token " << i + 1 << ", got variable"); problem_cnt++; break; case Expression::Token::CONST: pr_warn("Expected operator at token " << i + 1 << ", got constant"); problem_cnt++; break; default: pr_warn("Impossible token type, this is a bug!"); break; } break; case Expression::Token::OPERATOR: switch (tv[i].v.o) { case Expression::Token::OPA: switch (tv[i+1].t) { case Expression::Token::OPERATOR: switch (tv[i+1].v.o) { case Expression::Token::OPA: break; case Expression::Token::CPA: pr_warn("Expected value at token " << i + 1 << ", got closing bracket"); problem_cnt++; break; default: pr_warn("Expected value at token " << i + 1 << ", got operator"); problem_cnt++; break; } break; case Expression::Token::CONST: case Expression::Token::VAR: break; default: pr_warn("Impossible token type, this is a bug!"); break; } break; case Expression::Token::CPA: switch (tv[i+1].t) { case Expression::Token::OPERATOR: switch (tv[i+1].v.o) { case Expression::Token::CPA: break; case Expression::Token::OPA: pr_warn("Expected operator at token " << i + 1 << ", got opening bracket"); problem_cnt++; break; default: break; } break; case Expression::Token::CONST: pr_warn("Expected operator at token " << i + 1 << ", got constant"); problem_cnt++; break; case Expression::Token::VAR: pr_warn("Expected operator at token " << i + 1 << ", got variable"); problem_cnt++; break; default: pr_warn("Impossible token type, this is a bug!"); break; } break; default: switch (tv[i+1].t) { case Expression::Token::OPERATOR: switch (tv[i+1].v.o) { case Expression::Token::CPA: pr_warn("Expected value at token " << i + 1 << ", got closing bracket"); problem_cnt++; break; case Expression::Token::OPA: break; default: pr_warn("Expected value at token " << i + 1 << ", got operator"); problem_cnt++; break; } break; case Expression::Token::CONST: break; case Expression::Token::VAR: break; default: pr_warn("Impossible token type, this is a bug!"); break; } break; } break; default: pr_warn("Impossible token type, this is a bug!"); break; } if (pc != problem_cnt) { highlight_issue(tv, i + 1); } } } return problem_cnt; } static void process_unary_minus(std::vector& tv) { pr_debug("Pre-process: unary minus"); int change = 1; if (tv.size() >= 2 && tv[0].t == Expression::Token::OPERATOR && tv[0].v.o == Expression::Token::SUB) { std::vector highlight; tv.emplace(tv.begin(), 0.0); tv.emplace(tv.begin(), Expression::Token::OPA); highlight.emplace_back(add_bracket_to(tv, 2, 1)); highlight.emplace(highlight.begin(), 1); highlight.emplace(highlight.begin(), 0); Parser::print_math_vector(tv, highlight); } while (change) { change = 0; for (int i = 0; i < (long) tv.size() - 2; i++) { if (tv[i].t == Expression::Token::OPERATOR && tv[i].v.o != Expression::Token::CPA && tv[i+1].t == Expression::Token::OPERATOR && tv[i+1].v.o == Expression::Token::SUB && (tv[i+2].t == Expression::Token::CONST || tv[i+2].t == Expression::Token::VAR || (tv[i+2].t == Expression::Token::OPERATOR && tv[i+2].v.o == Expression::Token::OPA))) { std::vector highlight; tv.insert(tv.begin() + i + 1, 0.0); tv.insert(tv.begin() + i + 1, Expression::Token::OPA); highlight.emplace_back(add_bracket_to(tv, i + 3, 1)); highlight.emplace(highlight.begin(), i+2); highlight.emplace(highlight.begin(), i+1); Parser::print_math_vector(tv, highlight); i += 4; change++; } } } } // rl - relative level (outer priority) // ip - inner priority static int correct_operation_priorities(std::vector& tv, int op, int ip) { int corrections = 0; for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; (size_t) i < tv.size(); i++) { Expression::Token& tok = tv[i]; if (tok.t == Expression::Token::OPERATOR) { if (tok.v.o == Expression::Token::OPA) { relative_level++; } else if (tok.v.o == Expression::Token::CPA) { relative_level--; if (relative_level < 0) { same_lvl_op_cnt = -1; } } else if (relative_level == 0) { int target_op; switch (ip) { case 0: target_op = tok.v.o == Expression::Token::SUB; break; case 1: target_op = tok.v.o == Expression::Token::DIV; break; } if (rank_op(tok.v.o) == ip && target_op) { same_lvl_op_cnt++; if (same_lvl_op_cnt >= 1) { same_lvl_op_cnt = 0; corrections++; switch (ip) { case 0: tok.v.o = Expression::Token::ADD; break; case 1: tok.v.o = Expression::Token::MUL; break; } // inverse order to keep location valid between calls std::vector highlight_locations; highlight_locations.emplace_back(i+1); highlight_locations.emplace_back(add_cpa_before_le_io(tv, i) + 1); highlight_locations.emplace(highlight_locations.begin(), add_opa_after_le_io(tv, i)); Parser::print_math_vector(tv, highlight_locations); // advance one step to compensate for inserted left bracket i++; // elevate one level to compensate for new brackets relative_level++; } } else if (rank_op(tok.v.o) < ip) { same_lvl_op_cnt = -1; } } } } return corrections; } static int post_process(std::vector& tv) { pr_debug("Post processing:"); Parser::print_math_vector(tv); int total_corrections = 0; // global prio evaluation std::vector mp; create_prios_vector(tv, mp); if (mp.size() == 0) { return total_corrections; } for (int current_prio_level = mp[get_max_index(mp)]; current_prio_level >= 0; current_prio_level--) { int corrections = -1; while (corrections) { total_corrections += corrections = correct_operation_priorities(tv, current_prio_level, 1); } corrections = -1; while (corrections) { total_corrections += corrections = correct_operation_priorities(tv, current_prio_level, 0); } } return total_corrections; } int prepare_token_vector(std::vector& tv) { process_remove_empty_brackets(tv); if (check_bracket_match(tv)) { return 1; } process_unary_minus(tv); if (check_start_finish_validity(tv)) { return 1; } if (check_token_type_sequence(tv)) { return 1; } remove_extra_brackets(tv); (void) post_process(tv); remove_extra_brackets(tv); return 0; } std::vector build_op_vector(const std::vector& tv) { // define operator precedency std::vector g_prios; create_prios_vector(tv, g_prios); pr_debug("Building operation vector"); std::vector cv = tv; // Constant sync Vector // build operation vector std::vector ov; { std::vector mv = tv; // Modifiable Vector std::vector mp = g_prios; while (mp.size()) { int start, finish; find_top_prio_op_range(mp, start, finish); // --- process current range --- while (start != finish) { std::vector inner_top_ops; rank_inner_ops(inner_top_ops, mv, start, finish); std::vector operator_weights; weight_operators(operator_weights, mv, inner_top_ops, start); if (write_operation(ov, cv, mv, mp, start + inner_top_ops[get_min_index(operator_weights)])) { pr_warn("Stopping"); goto finish; } finish--; } // process final operation w/out running selection algorithm if (write_operation(ov, cv, mv, mp, start)) { pr_warn("Stopping"); goto finish; } } pr_debug("Final value:"); Parser::print_math_vector(mv); } pr_debug("Resulting operation vector:"); for (int i = 0, l = ov.size(); i < l; i++) { const Expression::Operation &o = ov[i]; prs_debug("OP (w=" << o.w << "): "); if (o.tl == Expression::Operation::NORMAL) { const Expression::Token &real_value = cv[o.il]; pri_debug(*real_value.to_math_str()); } else { pri_debug("t" << o.il); } pri_debug(" " << o.o.op_math_str() << " "); if (o.tr == Expression::Operation::NORMAL) { const Expression::Token &real_value = cv[o.ir]; pri_debug(*real_value.to_math_str()); } else { pri_debug("t" << o.ir); } pri_debug(" = t" << i << "\n"); } finish: return ov; } Expression::TreeNode ov_to_tree(const std::vector& tv, const std::vector& ov) { Expression::TreeNode root; std::queue> queued_ops; if (ov.size()) { queued_ops.emplace(&root, ov.size() - 1); } while (queued_ops.size()) { const std::pair item = queued_ops.front(); queued_ops.pop(); const Expression::Operation &proc_op = ov[item.second]; Expression::TreeNode &node = *item.first; node.o = proc_op.o; switch (proc_op.tl) { case Expression::Operation::DERIVED: queued_ops.emplace(node.l = new Expression::TreeNode(), proc_op.il); break; case Expression::Operation::NORMAL: node.l = new Expression::TreeNode(tv[proc_op.il]); break; } switch (proc_op.tr) { case Expression::Operation::DERIVED: queued_ops.emplace(node.r = new Expression::TreeNode(), proc_op.ir); break; case Expression::Operation::NORMAL: node.r = new Expression::TreeNode(tv[proc_op.ir]); break; } } return root; } void print_op_tree(const Expression::TreeNode& root) { (void) root; std::vector> items_to_display; } }