From 2c6009ae8301c890371b68c03e53caebbc09596c Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Thu, 19 Nov 2026 21:50:09 +0200 Subject: [PATCH] lab3 feature: add negative operation sorting --- CMakeLists.txt | 1 + config.h | 2 + inc/expression.hpp | 9 +- inc/parser.hpp | 2 + inc/syntax-priv.hpp | 46 +++++++ inc/syntaxer.hpp | 22 ++++ src/lab3.cpp | 288 ++++++++++++++++++++++++++++++++++++++++++++ src/parser.cpp | 30 +++++ src/syntaxer.cpp | 87 ++++++------- 9 files changed, 437 insertions(+), 50 deletions(-) create mode 100644 inc/syntax-priv.hpp create mode 100644 src/lab3.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 575c1d8..5814f74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ target_include_directories(parser PRIVATE add_library(syntaxer STATIC) target_sources(syntaxer PRIVATE src/syntaxer.cpp + src/lab3.cpp ) target_include_directories(syntaxer PRIVATE ${GLOBAL_INC} diff --git a/config.h b/config.h index 5acd71e..7c7858c 100644 --- a/config.h +++ b/config.h @@ -9,6 +9,8 @@ #define LAB2_EXCLUDE_COMPTIME_OP_WRITES 1 #define LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO 1 +#define LAB3_EXTENSION 1 + #define DYN_PIPELINE_MEM_SIZE 512 #define DYN_PIPELINE_LAYERS 3 #define DYN_PIPELINE_OUT_OF_ORDER_EXECUTION 1 diff --git a/inc/expression.hpp b/inc/expression.hpp index 5d3d10d..ace390c 100644 --- a/inc/expression.hpp +++ b/inc/expression.hpp @@ -37,7 +37,6 @@ public: 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; @@ -315,7 +314,13 @@ public: return (v.o == SUB || v.o == DIV); } - static int is_negative(Operator o) + // alias + [[nodiscard]] int is_negative(void) const + { + return negative_operator(); + } + + [[nodiscard]] static int is_negative(Operator o) { return (o == SUB || o == DIV); } diff --git a/inc/parser.hpp b/inc/parser.hpp index 4e946da..672bb05 100644 --- a/inc/parser.hpp +++ b/inc/parser.hpp @@ -2,6 +2,7 @@ #include #include "expression.hpp" +#include "syntaxer.hpp" namespace Parser { @@ -10,5 +11,6 @@ int assert_same(const std::vector& a, const std::vector& tv); void print_math_vector(const std::vector& tv); void print_math_vector(const std::vector& tv, const std::vector& highlight_locations); +void print_math_vector(const std::vector& tv, const Syntax::Range& hr); } diff --git a/inc/syntax-priv.hpp b/inc/syntax-priv.hpp new file mode 100644 index 0000000..51bf4a8 --- /dev/null +++ b/inc/syntax-priv.hpp @@ -0,0 +1,46 @@ +#include "expression.hpp" +#include "syntaxer.hpp" +#include + +namespace Syntax { + +int add_bracket_to(std::vector& tv, const int location, const int direction); +int rank_op(const Expression::Token::Operator o); +int same_lvl_opa_location(const std::vector& tv, const int location); +int same_lvl_cpa_location(const std::vector& tv, const int location); +void highlight_issue(const std::vector& tv, const int location); +int get_mop_location(const std::vector& v, int position); +int get_left_location(const std::vector& v, int location); +int get_right_location(const std::vector& v, int location); +int min_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv); +int get_operator_weight(const std::vector& v, int location); +int get_min_index(const std::vector& v); +int get_max_index(const std::vector& v); +int write_operation( + std::vector& ov, + std::vector& cv, + std::vector& mv, + std::vector& mp, + int position); +int add_bracket_to(std::vector& tv, const int location, const int direction); +int add_opa_after_le_io(std::vector &tv, const int location); +int add_cpa_before_le_io(std::vector &tv, const int location); +void create_prios_vector(const std::vector& tv, std::vector& pv); +void find_top_prio_op_range(const std::vector& mp, int& start, int& finish); +void find_select_prio_op_ranges_after_conv(const std::vector& mp, std::vector& ranges, int target_prio); +void find_parent_level_range(const std::vector& tv, Range &p, const Range &c); +void rank_inner_ops(std::vector& inner_top_ops, const std::vector& mv, const int start, const int finish); +void weight_operators(std::vector& operator_weights, const std::vector& mv, const std::vector& inner_top_ops, const int start); +Expression::Token::Operator get_prior_mop(const std::vector& tv, const int location); +void open_brackets(std::vector& tv, const Range &r); +int get_max_neighbour_rank(const std::vector& tv, const Range &r); +void remove_extra_brackets(std::vector& tv); +void process_remove_empty_brackets(std::vector& tv); +int check_bracket_match(const std::vector& tv); +int check_start_finish_validity(const std::vector& tv); +int check_token_type_sequence(const std::vector& tv); +void process_unary_minus(std::vector& tv); +int correct_operation_priorities(std::vector& tv, int op, int ip); +int post_process(std::vector& tv); +void descend_print_tree_node(const Expression::TreeNode& this_node, const int depth, const int leftnesses); +} diff --git a/inc/syntaxer.hpp b/inc/syntaxer.hpp index 4394066..f7247b0 100644 --- a/inc/syntaxer.hpp +++ b/inc/syntaxer.hpp @@ -6,9 +6,31 @@ namespace Syntax { +struct Range +{ + int start, finish; + + Range(void) + { + } + + Range(int start, int finish) + : start(start), finish(finish) + { + } + + int in(int i) const + { + return i >= start && i <= finish; + } +}; + int prepare_token_vector(std::vector& tv); std::vector build_op_vector(const std::vector& tv); Expression::TreeNode ov_to_tree(const std::vector& tv, const std::vector& ov); void print_op_tree(const Expression::TreeNode &node); +// lab3 extension +void lab3_sort_by_weight(std::vector& tv); + } diff --git a/src/lab3.cpp b/src/lab3.cpp new file mode 100644 index 0000000..7be9ff9 --- /dev/null +++ b/src/lab3.cpp @@ -0,0 +1,288 @@ +#include "expression.hpp" +#include "parser.hpp" +#include "print.h" +#include "syntaxer.hpp" +#include "syntax-priv.hpp" +#include + +namespace Syntax { + +static Range expand_range(std::vector& tv, const int location) +{ + Range r; + + int i = location - 1, l = tv.size(), lv = 0; + + for (; i >= 0; i--) + { + if (tv[i].t == Expression::Token::OPERATOR) + { + if (tv[i].v.o == Expression::Token::CPA) + { + lv++; + } + + if (!lv) + { + r.start = i + 1; + goto to_right; + } + + if (tv[i].v.o == Expression::Token::OPA) + { + lv--; + } + } + } + + r.start = 0; + +to_right: + i = location + 1; + + for (; i < l; i++) + { + if (tv[i].t == Expression::Token::OPERATOR) + { + if (tv[i].v.o == Expression::Token::OPA) + { + lv++; + } + + if (!lv) + { + r.finish = i - 1; + goto to_return; + } + + if (tv[i].v.o == Expression::Token::CPA) + { + lv--; + } + } + } + + r.finish = tv.size() - 1; + +to_return: + return r; +} + +static int total_weight(const std::vector& tv, const Range& r) +{ + int total = 0; + + for (int i = r.start; i <= r.finish; i++) + { + switch (const Expression::Token &t = tv[i]; t.t) + { + case Expression::Token::OPERATOR: + total += Expression::Token::weight_op(t.v.o); + break; + case Expression::Token::CV: + total += t.v.cv.w; + break; + default: + break; + } + } + + return total; +} + +static int copy_range(std::vector& dst_tv, const int dst_offset, std::vector& src_tv, const Range& src_r) +{ + const int len = src_r.finish - src_r.start + 1; + + for (int i = 0; i < len; i++) + { + dst_tv[dst_offset + i] = src_tv[src_r.start + i]; + } + + return len; +} + +static void reorder_ranges(std::vector& tv, const std::vector& rs, const std::vector places) +{ + const int global_offset = rs[0].start; + const int global_length = rs[rs.size() - 1].finish + 1 - global_offset; + + // write reordered vector into a temporary buffer + + std::vector rtv; // reordered token vector + rtv.resize(global_length); + + int i = 0, o = 0; + o += copy_range(rtv, o, tv, rs[places[i]]); // first clear copy + i++; + + for ( ; i < (int) rs.size(); i++) + { + Range cr = rs[places[i]]; + cr.start--; // include sign in copy + + o += copy_range(rtv, o, tv, cr); + } + + // then copy the result back into the original vector + copy_range(tv, global_offset, rtv, Range(0, global_length - 1)); +} + +static std::vector split_ranges_by_rank(const std::vector& tv, const Range& r, const int rank) +{ + std::vector rs; + Range cr; + + bool writing_range = 0; + int lvl = 0; + + for (int i = r.start; i <= r.finish; i++) + { + const Expression::Token &t = tv[i]; + + if (t.t == Expression::Token::OPERATOR && writing_range) + { + if (rank_op(t.v.o) > rank) + { + cr.finish = i; + } + else + { + if (t.v.o == Expression::Token::OPA) + { + lvl++; + } + + if (lvl == 0) + { + rs.push_back(cr); + writing_range = false; + } + + if (t.v.o == Expression::Token::CPA) + { + lvl--; + + cr.finish = i; + + if (lvl < 0) + { + pr_warn("Negative lvl; has to be >= 0 here. This is a bug!"); + } + } + } + } + else if (t.t == Expression::Token::OPERATOR && !writing_range) + { + if (t.v.o == Expression::Token::OPA) + { + lvl++; + cr.start = i; + cr.finish = i; + writing_range = true; + } + else + { + pr_warn("Weird operator sequence. This is a bug!"); + } + } + else if (t.t != Expression::Token::OPERATOR && writing_range) + { + cr.finish = i; + } + else if (t.t != Expression::Token::OPERATOR && !writing_range) + { + cr.start = i; + cr.finish = i; + writing_range = true; + } + } + + if (writing_range) + { + rs.push_back(cr); + } + + return rs; +} + +void lab3_sort_by_weight(std::vector& tv) +{ + pr_debug("Processing (lab3): sorting token groups"); + std::vector mp; + create_prios_vector(tv, mp); + + if (mp.size() == 0) + { + return; + } + + 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--) + for (int r_no = 0, l = ranges.size(); r_no < l; 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]; + Range er; // expanded range + + er.start = expand_range(tv, get_mop_location(tv, r.start)).start; + er.finish = expand_range(tv, get_mop_location(tv, r.finish)).finish; + + for (int current_rank = 2; current_rank >= 0; current_rank--) + { + std::vector rs = split_ranges_by_rank(tv, er, current_rank); + /* + std::vector weights; + + for (int i = 0, k = rs.size(); i < k; i++) + { + weights.emplace_back(total_weight(tv, rs[i])); + } + */ + + // apply negative bias + std::vector range_target; // 0 -> positive, 1 -> negative + range_target.emplace_back(0); // first range has no sign, default to positive + + for (int i = 1; i < (int) rs.size(); i++) + { + range_target.emplace_back(tv[rs[i].start - 1].is_negative()); + } + + std::vector places; + + for (int v = 0; v < 2; v++) + { + for (int i = 0; i < (int) range_target.size(); i++) + { + if (v == range_target[i]) + { + places.emplace_back(i); + } + } + } + + reorder_ranges(tv, rs, places); + + pr_debug("Negative bias applied:"); + Parser::print_math_vector(tv, Range(rs[0].start, rs[rs.size()-1].finish)); + + // sorting + } + } + } +} + +} diff --git a/src/parser.cpp b/src/parser.cpp index dcf5af7..5bdd5ed 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1,4 +1,5 @@ #include "expression.hpp" +#include "syntaxer.hpp" #include "print.h" #include #include @@ -422,4 +423,33 @@ void print_math_vector(const std::vector& tv, const std::vect } } +// hr => highlight range +void print_math_vector(const std::vector& tv, const Syntax::Range& hr) +{ + int i, l; + for (i = 0, l = tv.size() - 1; i < l; i++) + { + if (hr.in(i)) + { + std::cout << "\033[31m" << *tv[i].to_math_str() << "\033[0m"; + } + else + { + std::cout << *tv[i].to_math_str();; + } + } + + if (tv.size()) + { + if (hr.in(tv.size() - 1)) + { + std::cout << "\033[31m" << *tv[tv.size() - 1].to_math_str() << "\033[0m\n"; + } + else + { + std::cout << *tv[tv.size() - 1].to_math_str() << "\n"; + } + } +} + } diff --git a/src/syntaxer.cpp b/src/syntaxer.cpp index ea5b9b3..60ce623 100644 --- a/src/syntaxer.cpp +++ b/src/syntaxer.cpp @@ -9,23 +9,9 @@ namespace Syntax { -struct Range -{ - int start, finish; +int add_bracket_to(std::vector& tv, const int location, const int direction); - 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) +int rank_op(const Expression::Token::Operator o) { switch (o) { @@ -42,7 +28,7 @@ static int rank_op(const Expression::Token::Operator o) } } -static int same_lvl_opa_location(const std::vector& tv, const int location) +int same_lvl_opa_location(const std::vector& tv, const int location) { for (int i = location - 1, lv = 0; i >= 0; i--) { @@ -74,7 +60,7 @@ static int same_lvl_opa_location(const std::vector& tv, const return -1; } -static int same_lvl_cpa_location(const std::vector& tv, const int location) +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++) { @@ -106,7 +92,7 @@ static int same_lvl_cpa_location(const std::vector& tv, const return tv.size() - 1; } -static void highlight_issue(const std::vector& tv, const int location) +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; @@ -131,7 +117,7 @@ static void highlight_issue(const std::vector& tv, const int // 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 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 @@ -140,7 +126,7 @@ static int get_mop_location(const std::vector& v, int positio return i; } -static int get_left_location(const std::vector& v, int location) +int get_left_location(const std::vector& v, int location) { for (int i = location - 1; i >= 0; i--) { @@ -153,7 +139,7 @@ static int get_left_location(const std::vector& v, int locati return -1; } -static int get_right_location(const std::vector& v, int location) +int get_right_location(const std::vector& v, int location) { for (int i = location + 1, l = v.size(); i < l; i++) { @@ -166,7 +152,7 @@ static int get_right_location(const std::vector& v, int locat return -1; } -static int min_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv) +int min_rank(const std::vector &tv, const std::vector &mp, const Range &r, const int base_lv) { int m = 3; @@ -185,7 +171,7 @@ static int min_rank(const std::vector &tv, const std::vector< return m; } -static int get_operator_weight(const std::vector& v, int location) +int get_operator_weight(const std::vector& v, int location) { int weight = 0; @@ -218,7 +204,7 @@ static int get_operator_weight(const std::vector& v, int loca return weight; } -static int get_min_index(const std::vector& v) +int get_min_index(const std::vector& v) { int idx = 0; @@ -233,7 +219,7 @@ static int get_min_index(const std::vector& v) return idx; } -static int get_max_index(const std::vector& v) +int get_max_index(const std::vector& v) { int idx = 0; @@ -248,7 +234,7 @@ static int get_max_index(const std::vector& v) return idx; } -static int write_operation( +int write_operation( std::vector& ov, std::vector& cv, std::vector& mv, @@ -352,7 +338,7 @@ static int write_operation( return 0; } -static int add_bracket_to(std::vector& tv, const int location, const int direction) +int add_bracket_to(std::vector& tv, const int location, const int direction) { int i = location + direction, l = tv.size(), lv = 0; @@ -417,7 +403,7 @@ static int add_bracket_to(std::vector& tv, const int location } } -static int add_opa_after_le_io(std::vector &tv, const int location) +int add_opa_after_le_io(std::vector &tv, const int location) { int base_rank = rank_op(tv[location].v.o); @@ -449,7 +435,7 @@ static int add_opa_after_le_io(std::vector &tv, const int loc return 0; } -static int add_cpa_before_le_io(std::vector &tv, const int location) +int add_cpa_before_le_io(std::vector &tv, const int location) { int base_rank = rank_op(tv[location].v.o); @@ -481,7 +467,7 @@ static int add_cpa_before_le_io(std::vector &tv, const int lo return tv.size() - 1; } -static void create_prios_vector(const std::vector& tv, std::vector& pv) +void create_prios_vector(const std::vector& tv, std::vector& pv) { for (int i = 0, cp = 0, l = tv.size(); i < l; i++) { @@ -510,7 +496,7 @@ static void create_prios_vector(const std::vector& tv, std::v } } -static void find_top_prio_op_range(const std::vector& mp, int& start, int& finish) +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++) { @@ -528,7 +514,7 @@ static void find_top_prio_op_range(const std::vector& mp, int& start, int& // 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) +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++) { @@ -548,7 +534,7 @@ static void find_select_prio_op_ranges_after_conv(const std::vector& mp, st } } -static void find_parent_level_range(const std::vector& tv, Range &p, const Range &c) +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--) @@ -603,7 +589,7 @@ right: p.finish = pc - 1; } -static void rank_inner_ops(std::vector& inner_top_ops, const std::vector& mv, const int start, const int finish) +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++) { @@ -647,7 +633,7 @@ static void rank_inner_ops(std::vector& inner_top_ops, const std::vector& operator_weights, const std::vector& mv, const std::vector& inner_top_ops, const int start) +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++) { @@ -656,7 +642,7 @@ static void weight_operators(std::vector& operator_weights, const std::vect } } -static Expression::Token::Operator get_prior_mop(const std::vector& tv, const int location) +Expression::Token::Operator get_prior_mop(const std::vector& tv, const int location) { for (int i = location - 1, lv = 0; i >= 0; i--) { @@ -688,7 +674,7 @@ static Expression::Token::Operator get_prior_mop(const std::vector& tv, const Range &r) +void open_brackets(std::vector& tv, const Range &r) { Range brackets( same_lvl_opa_location(tv, get_mop_location(tv, r.start)), @@ -730,7 +716,7 @@ static void open_brackets(std::vector& tv, const Range &r) tv.erase(tv.begin() + brackets.start); } -static int get_max_neighbour_rank(const std::vector& tv, const Range &r) +int get_max_neighbour_rank(const std::vector& tv, const Range &r) { int rank = -1; @@ -755,7 +741,7 @@ static int get_max_neighbour_rank(const std::vector& tv, cons return rank; } -static void remove_extra_brackets(std::vector& tv) +void remove_extra_brackets(std::vector& tv) { pr_debug("Processing: removing extra brackets"); std::vector mp; @@ -816,7 +802,7 @@ static void remove_extra_brackets(std::vector& tv) } } -static void process_remove_empty_brackets(std::vector& tv) +void process_remove_empty_brackets(std::vector& tv) { pr_debug("Pre-process: remove empty brackets"); @@ -845,7 +831,7 @@ static void process_remove_empty_brackets(std::vector& tv) } } -static int check_bracket_match(const std::vector& tv) +int check_bracket_match(const std::vector& tv) { int problem_cnt = 0; std::stack s; @@ -888,7 +874,7 @@ static int check_bracket_match(const std::vector& tv) return problem_cnt; } -static int check_start_finish_validity(const std::vector& tv) +int check_start_finish_validity(const std::vector& tv) { int problem_cnt = 0; @@ -961,7 +947,7 @@ static int check_start_finish_validity(const std::vector& tv) return problem_cnt; } -static int check_token_type_sequence(const std::vector& tv) +int check_token_type_sequence(const std::vector& tv) { int problem_cnt = 0; @@ -1103,7 +1089,7 @@ static int check_token_type_sequence(const std::vector& tv) return problem_cnt; } -static void process_unary_minus(std::vector& tv) +void process_unary_minus(std::vector& tv) { pr_debug("Pre-process: unary minus"); @@ -1157,7 +1143,7 @@ static void process_unary_minus(std::vector& tv) // rl - relative level (outer priority) // ip - inner priority -static int correct_operation_priorities(std::vector& tv, int op, int ip) +int correct_operation_priorities(std::vector& tv, int op, int ip) { int corrections = 0; @@ -1237,7 +1223,7 @@ static int correct_operation_priorities(std::vector& tv, int return corrections; } -static int post_process(std::vector& tv) +int post_process(std::vector& tv) { pr_debug("Post processing:"); Parser::print_math_vector(tv); @@ -1295,6 +1281,11 @@ int prepare_token_vector(std::vector& tv) } remove_extra_brackets(tv); + +#if LAB3_EXTENSION == 1 + Syntax::lab3_sort_by_weight(tv); +#endif + (void) post_process(tv); remove_extra_brackets(tv); @@ -1432,7 +1423,7 @@ Expression::TreeNode ov_to_tree(const std::vector& tv, const return root; } -static void descend_print_tree_node(const Expression::TreeNode& this_node, const int depth, const int leftnesses) +void descend_print_tree_node(const Expression::TreeNode& this_node, const int depth, const int leftnesses) { prs_info("");