From 79a96aea77d29c0c4130222446cc070addad29b0 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Thu, 15 Oct 2026 16:42:52 +0300 Subject: [PATCH] cleanup + minor fixes --- CMakeLists.txt | 2 +- inc/expression.hpp | 5 +- src/parser.cpp | 2 +- src/syntaxer.cpp | 225 ++++++--------------------------------------- 4 files changed, 34 insertions(+), 200 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43d23dd..ba3a455 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.10) -SET(CMAKE_CXX_FLAGS_DEBUG "-g") +SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -Wpedantic") project(pzks) diff --git a/inc/expression.hpp b/inc/expression.hpp index caf4911..b423261 100644 --- a/inc/expression.hpp +++ b/inc/expression.hpp @@ -52,6 +52,7 @@ public: } explicit TokenValue(void) + : s(std::shared_ptr()) { } @@ -144,6 +145,7 @@ public: switch (t) { case VAR: + new(&v.s) std::shared_ptr; v.s = other.v.s; break; case CONST: @@ -174,7 +176,6 @@ public: return v.s; case CONST: { - char buffer[128] = {0}; return std::make_shared((std::ostringstream() << v.d).str()); } case OPERATOR: @@ -186,8 +187,6 @@ public: std::shared_ptr to_math_str(void) const { - char buffer[128] = {0}; - switch (t) { case VAR: diff --git a/src/parser.cpp b/src/parser.cpp index 5be1e66..dcf5af7 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -411,7 +411,7 @@ void print_math_vector(const std::vector& tv, const std::vect if (tv.size()) { - if (highlight_locations[highlight_locations.size()-1] == tv.size() - 1) + if (highlight_locations[highlight_locations.size()-1] == (long) (tv.size() - 1)) { std::cout << "\033[31m" << *tv[tv.size() - 1].to_math_str() << "\033[0m\n"; } diff --git a/src/syntaxer.cpp b/src/syntaxer.cpp index c9c81d5..f655b94 100644 --- a/src/syntaxer.cpp +++ b/src/syntaxer.cpp @@ -38,7 +38,6 @@ static int rank_op(const Expression::Token::Operator o) case Expression::Token::POW: return 2; default: - //pr_warn("Operator is unrankable"); return -1; } } @@ -110,7 +109,7 @@ static int same_lvl_cpa_location(const std::vector& tv, const static void highlight_issue(const std::vector& tv, const int location) { int start = location - 7 < 0 ? 0 : location - 7; - int finish = location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7; + int finish = (size_t) location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7; std::cout << " >>> "; @@ -167,25 +166,6 @@ static int get_right_location(const std::vector& v, int locat return -1; } -static int max_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv) -{ - int m = -1; - - 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 min_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv) { int m = 3; @@ -568,81 +548,6 @@ static void find_select_prio_op_ranges_after_conv(const std::vector& mp, st } } -static void find_select_prio_op_ranges(const std::vector& mp, std::vector& ranges, int target_prio) -{ - for (int i = 0, l = mp.size(), prio = 0, 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, int &p_start, int &p_finish, const int c_start, const int c_finish) -{ - // left side - for (int i = get_mop_location(tv, c_start), p = c_start, lv = 0; i >= 0; i--) - { - if (tv[i].is_opa()) - { - lv--; - - if (lv == -2) - { - p_start = p; - goto right; - } - } - else if (tv[i].is_math_op()) - { - p--; - } - else if (tv[i].is_cpa()) - { - lv++; - } - } - - p_start = 0; - -right: - // right side - int i, p, lv; - for (i = get_mop_location(tv, c_finish), p = c_finish, lv = 0; i < tv.size(); i++) - { - if (tv[i].is_cpa()) - { - lv--; - - if (lv == -2) - { - p_finish = p; - break; - } - } - else if (tv[i].is_math_op()) - { - p++; - } - else if (tv[i].is_opa()) - { - lv++; - } - } - - p_finish = p - 1; -} - static void find_parent_level_range(const std::vector& tv, Range &p, const Range &c) { // left side @@ -673,7 +578,7 @@ static void find_parent_level_range(const std::vector& tv, Ra right: // right side int i, pc, lv; - for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; i < tv.size(); i++) + for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; (size_t) i < tv.size(); i++) { if (tv[i].is_cpa()) { @@ -698,56 +603,6 @@ right: p.finish = pc - 1; } -static void rank_inner_ops_raw(std::vector& inner_op_ranks, const std::vector& mv, const int start, const int finish, const int same_lvl_only = 0) -{ - for (int i = get_mop_location(mv, start), lvl = 0, f = get_mop_location(mv, finish); i <= f; i++) - { - //int idx = get_mop_location(mv, start + i); - const Expression::Token &t = mv[i]; - - if (t.t != Expression::Token::OPERATOR) - { - continue; - } - - switch (t.v.o) - { - case Expression::Token::ADD: - case Expression::Token::SUB: - if (same_lvl_only && lvl) - { - break; - } - inner_op_ranks.push_back(0); - break; - case Expression::Token::MUL: - case Expression::Token::DIV: - if (same_lvl_only && lvl) - { - break; - } - inner_op_ranks.push_back(1); - break; - case Expression::Token::POW: - if (same_lvl_only && lvl) - { - break; - } - inner_op_ranks.push_back(2); - break; - case Expression::Token::OPA: - lvl++; - break; - case Expression::Token::CPA: - lvl--; - break; - default: - pr_warn("Operator is unrankable"); - break; - } - } -} - 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++) @@ -801,26 +656,6 @@ static void weight_operators(std::vector& operator_weights, const std::vect } } -static int same_rank_ops(const std::vector& tv, const std::vector &mp, const Range &r, const int outer_lvl) -{ - int first_op_inner_level = rank_op(tv[get_mop_location(tv, r.start)].v.o); - - for (int i = r.start; i <= r.finish; i++) - { - if (mp[i] != outer_lvl) - { - continue; - } - - if (first_op_inner_level != rank_op(tv[get_mop_location(tv, i)].v.o)) - { - return 1; - } - } - - return 0; -} - static Expression::Token::Operator get_prior_mop(const std::vector& tv, const int location) { for (int i = location - 1, lv = 0; i >= 0; i--) @@ -899,12 +734,7 @@ static int get_max_neighbour_rank(const std::vector& tv, cons { int rank = -1; - // left neighbour - //if (r.start - 1 >= 0) - //if () - //{ int left_bracket = same_lvl_opa_location(tv, get_mop_location(tv, r.start)); - //if (int ro = rank_op(tv[get_mop_location(tv, r.start - 1)].v.o); ro > rank) if (left_bracket - 1 >= 0) { if (int ro = rank_op(tv[left_bracket - 1].v.o); ro > rank) @@ -912,19 +742,9 @@ static int get_max_neighbour_rank(const std::vector& tv, cons rank = ro; } } - //} - - // right neighbour - //if (int rl = get_mop_location(tv, r.finish + 1); rl < tv.size()) - //{ - // if (int ro = rank_op(tv[rl].v.o); ro > rank) - // { - // rank = ro; - // } - //} int right_bracket = same_lvl_cpa_location(tv, get_mop_location(tv, r.finish)); - if (right_bracket + 1 < tv.size()) + if ((size_t) (right_bracket + 1) < tv.size()) { if (int ro = rank_op(tv[right_bracket + 1].v.o); ro > rank) { @@ -941,6 +761,11 @@ static void remove_extra_brackets(std::vector& tv) 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--) { @@ -999,7 +824,7 @@ static void process_remove_empty_brackets(std::vector& tv) while (fix) { fix = 0; - for (int i = 0; i < tv.size() - 1; i++) + for (int i = 0; i < (long) tv.size() - 1; i++) { if (tv[i].t == Expression::Token::OPERATOR && tv[i].v.o == Expression::Token::OPA @@ -1283,11 +1108,25 @@ 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 < tv.size() - 2; i++) + for (int i = 0; i < (long) tv.size() - 2; i++) { if (tv[i].t == Expression::Token::OPERATOR && tv[i].v.o != Expression::Token::CPA @@ -1300,8 +1139,8 @@ static void process_unary_minus(std::vector& tv) { std::vector highlight; - tv.insert(tv.begin() + i + 1, Expression::Token(0.0)); - tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA)); + 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); @@ -1322,7 +1161,7 @@ static int correct_operation_priorities(std::vector& tv, int { int corrections = 0; - for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; i < tv.size(); i++) + for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; (size_t) i < tv.size(); i++) { Expression::Token& tok = tv[i]; @@ -1391,7 +1230,6 @@ static int correct_operation_priorities(std::vector& tv, int { same_lvl_op_cnt = -1; } - } } } @@ -1456,12 +1294,9 @@ int prepare_token_vector(std::vector& tv) return 1; } - //for (int corrs = -1; corrs; ) - //{ - remove_extra_brackets(tv); - (void) post_process(tv); - remove_extra_brackets(tv); - //} + remove_extra_brackets(tv); + (void) post_process(tv); + remove_extra_brackets(tv); return 0; }