lab3 feature: add negative operation sorting
This commit is contained in:
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
+7
-2
@@ -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);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include "expression.hpp"
|
||||
#include "syntaxer.hpp"
|
||||
|
||||
namespace Parser {
|
||||
|
||||
@@ -10,5 +11,6 @@ int assert_same(const std::vector<Expression::Token>& a, const std::vector<Expre
|
||||
void print_token_vector(const std::vector<Expression::Token>& tv);
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv);
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv, const std::vector<int>& highlight_locations);
|
||||
void print_math_vector(const std::vector<Expression::Token>& tv, const Syntax::Range& hr);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "expression.hpp"
|
||||
#include "syntaxer.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace Syntax {
|
||||
|
||||
int add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction);
|
||||
int rank_op(const Expression::Token::Operator o);
|
||||
int same_lvl_opa_location(const std::vector<Expression::Token>& tv, const int location);
|
||||
int same_lvl_cpa_location(const std::vector<Expression::Token>& tv, const int location);
|
||||
void highlight_issue(const std::vector<Expression::Token>& tv, const int location);
|
||||
int get_mop_location(const std::vector<Expression::Token>& v, int position);
|
||||
int get_left_location(const std::vector<Expression::Token>& v, int location);
|
||||
int get_right_location(const std::vector<Expression::Token>& v, int location);
|
||||
int min_rank(const std::vector<Expression::Token> &tv, const std::vector<int> &mp, const Range &r, const int base_lv);
|
||||
int get_operator_weight(const std::vector<Expression::Token>& v, int location);
|
||||
int get_min_index(const std::vector<int>& v);
|
||||
int get_max_index(const std::vector<int>& v);
|
||||
int write_operation(
|
||||
std::vector<Expression::Operation>& ov,
|
||||
std::vector<Expression::Token>& cv,
|
||||
std::vector<Expression::Token>& mv,
|
||||
std::vector<int>& mp,
|
||||
int position);
|
||||
int add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction);
|
||||
int add_opa_after_le_io(std::vector<Expression::Token> &tv, const int location);
|
||||
int add_cpa_before_le_io(std::vector<Expression::Token> &tv, const int location);
|
||||
void create_prios_vector(const std::vector<Expression::Token>& tv, std::vector<int>& pv);
|
||||
void find_top_prio_op_range(const std::vector<int>& mp, int& start, int& finish);
|
||||
void find_select_prio_op_ranges_after_conv(const std::vector<int>& mp, std::vector<Range>& ranges, int target_prio);
|
||||
void find_parent_level_range(const std::vector<Expression::Token>& tv, Range &p, const Range &c);
|
||||
void rank_inner_ops(std::vector<int>& inner_top_ops, const std::vector<Expression::Token>& mv, const int start, const int finish);
|
||||
void weight_operators(std::vector<int>& operator_weights, const std::vector<Expression::Token>& mv, const std::vector<int>& inner_top_ops, const int start);
|
||||
Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& tv, const int location);
|
||||
void open_brackets(std::vector<Expression::Token>& tv, const Range &r);
|
||||
int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, const Range &r);
|
||||
void remove_extra_brackets(std::vector<Expression::Token>& tv);
|
||||
void process_remove_empty_brackets(std::vector<Expression::Token>& tv);
|
||||
int check_bracket_match(const std::vector<Expression::Token>& tv);
|
||||
int check_start_finish_validity(const std::vector<Expression::Token>& tv);
|
||||
int check_token_type_sequence(const std::vector<Expression::Token>& tv);
|
||||
void process_unary_minus(std::vector<Expression::Token>& tv);
|
||||
int correct_operation_priorities(std::vector<Expression::Token>& tv, int op, int ip);
|
||||
int post_process(std::vector<Expression::Token>& tv);
|
||||
void descend_print_tree_node(const Expression::TreeNode& this_node, const int depth, const int leftnesses);
|
||||
}
|
||||
@@ -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<Expression::Token>& tv);
|
||||
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv);
|
||||
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token>& tv, const std::vector<Expression::Operation>& ov);
|
||||
void print_op_tree(const Expression::TreeNode &node);
|
||||
|
||||
// lab3 extension
|
||||
void lab3_sort_by_weight(std::vector<Expression::Token>& tv);
|
||||
|
||||
}
|
||||
|
||||
+288
@@ -0,0 +1,288 @@
|
||||
#include "expression.hpp"
|
||||
#include "parser.hpp"
|
||||
#include "print.h"
|
||||
#include "syntaxer.hpp"
|
||||
#include "syntax-priv.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace Syntax {
|
||||
|
||||
static Range expand_range(std::vector<Expression::Token>& 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<Expression::Token>& 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<Expression::Token>& dst_tv, const int dst_offset, std::vector<Expression::Token>& 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<Expression::Token>& tv, const std::vector<Range>& rs, const std::vector<int> 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<Expression::Token> 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<Range> split_ranges_by_rank(const std::vector<Expression::Token>& tv, const Range& r, const int rank)
|
||||
{
|
||||
std::vector<Range> 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<Expression::Token>& tv)
|
||||
{
|
||||
pr_debug("Processing (lab3): sorting token groups");
|
||||
std::vector<int> 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<Range> 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<Range> rs = split_ranges_by_rank(tv, er, current_rank);
|
||||
/*
|
||||
std::vector<int> weights;
|
||||
|
||||
for (int i = 0, k = rs.size(); i < k; i++)
|
||||
{
|
||||
weights.emplace_back(total_weight(tv, rs[i]));
|
||||
}
|
||||
*/
|
||||
|
||||
// apply negative bias
|
||||
std::vector<int> 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<int> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "expression.hpp"
|
||||
#include "syntaxer.hpp"
|
||||
#include "print.h"
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
@@ -422,4 +423,33 @@ void print_math_vector(const std::vector<Expression::Token>& tv, const std::vect
|
||||
}
|
||||
}
|
||||
|
||||
// hr => highlight range
|
||||
void print_math_vector(const std::vector<Expression::Token>& 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+39
-48
@@ -9,23 +9,9 @@
|
||||
|
||||
namespace Syntax {
|
||||
|
||||
struct Range
|
||||
{
|
||||
int start, finish;
|
||||
int add_bracket_to(std::vector<Expression::Token>& 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<Expression::Token>& 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<Expression::Token>& tv, const int location)
|
||||
int same_lvl_opa_location(const std::vector<Expression::Token>& 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<Expression::Token>& tv, const
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int same_lvl_cpa_location(const std::vector<Expression::Token>& tv, const int location)
|
||||
int same_lvl_cpa_location(const std::vector<Expression::Token>& 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<Expression::Token>& tv, const
|
||||
return tv.size() - 1;
|
||||
}
|
||||
|
||||
static void highlight_issue(const std::vector<Expression::Token>& tv, const int location)
|
||||
void highlight_issue(const std::vector<Expression::Token>& 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<Expression::Token>& tv, const int
|
||||
|
||||
// get mop = Math OPerator location (=absolute index in std::vector<Expression::Token>)
|
||||
// mapper of 'priority vector index' -> 'token vector index'
|
||||
static int get_mop_location(const std::vector<Expression::Token>& v, int position)
|
||||
int get_mop_location(const std::vector<Expression::Token>& 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<Expression::Token>& v, int positio
|
||||
return i;
|
||||
}
|
||||
|
||||
static int get_left_location(const std::vector<Expression::Token>& v, int location)
|
||||
int get_left_location(const std::vector<Expression::Token>& v, int location)
|
||||
{
|
||||
for (int i = location - 1; i >= 0; i--)
|
||||
{
|
||||
@@ -153,7 +139,7 @@ static int get_left_location(const std::vector<Expression::Token>& v, int locati
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int get_right_location(const std::vector<Expression::Token>& v, int location)
|
||||
int get_right_location(const std::vector<Expression::Token>& 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<Expression::Token>& v, int locat
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int min_rank(const std::vector<Expression::Token> &tv, const std::vector<int> &mp, const Range &r, const int base_lv)
|
||||
int min_rank(const std::vector<Expression::Token> &tv, const std::vector<int> &mp, const Range &r, const int base_lv)
|
||||
{
|
||||
int m = 3;
|
||||
|
||||
@@ -185,7 +171,7 @@ static int min_rank(const std::vector<Expression::Token> &tv, const std::vector<
|
||||
return m;
|
||||
}
|
||||
|
||||
static int get_operator_weight(const std::vector<Expression::Token>& v, int location)
|
||||
int get_operator_weight(const std::vector<Expression::Token>& v, int location)
|
||||
{
|
||||
int weight = 0;
|
||||
|
||||
@@ -218,7 +204,7 @@ static int get_operator_weight(const std::vector<Expression::Token>& v, int loca
|
||||
return weight;
|
||||
}
|
||||
|
||||
static int get_min_index(const std::vector<int>& v)
|
||||
int get_min_index(const std::vector<int>& v)
|
||||
{
|
||||
int idx = 0;
|
||||
|
||||
@@ -233,7 +219,7 @@ static int get_min_index(const std::vector<int>& v)
|
||||
return idx;
|
||||
}
|
||||
|
||||
static int get_max_index(const std::vector<int>& v)
|
||||
int get_max_index(const std::vector<int>& v)
|
||||
{
|
||||
int idx = 0;
|
||||
|
||||
@@ -248,7 +234,7 @@ static int get_max_index(const std::vector<int>& v)
|
||||
return idx;
|
||||
}
|
||||
|
||||
static int write_operation(
|
||||
int write_operation(
|
||||
std::vector<Expression::Operation>& ov,
|
||||
std::vector<Expression::Token>& cv,
|
||||
std::vector<Expression::Token>& mv,
|
||||
@@ -352,7 +338,7 @@ static int write_operation(
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction)
|
||||
int add_bracket_to(std::vector<Expression::Token>& 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<Expression::Token>& tv, const int location
|
||||
}
|
||||
}
|
||||
|
||||
static int add_opa_after_le_io(std::vector<Expression::Token> &tv, const int location)
|
||||
int add_opa_after_le_io(std::vector<Expression::Token> &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<Expression::Token> &tv, const int loc
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int add_cpa_before_le_io(std::vector<Expression::Token> &tv, const int location)
|
||||
int add_cpa_before_le_io(std::vector<Expression::Token> &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<Expression::Token> &tv, const int lo
|
||||
return tv.size() - 1;
|
||||
}
|
||||
|
||||
static void create_prios_vector(const std::vector<Expression::Token>& tv, std::vector<int>& pv)
|
||||
void create_prios_vector(const std::vector<Expression::Token>& tv, std::vector<int>& 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<Expression::Token>& tv, std::v
|
||||
}
|
||||
}
|
||||
|
||||
static void find_top_prio_op_range(const std::vector<int>& mp, int& start, int& finish)
|
||||
void find_top_prio_op_range(const std::vector<int>& 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<int>& 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<int>& mp, std::vector<Range>& ranges, int target_prio)
|
||||
void find_select_prio_op_ranges_after_conv(const std::vector<int>& mp, std::vector<Range>& 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<int>& mp, st
|
||||
}
|
||||
}
|
||||
|
||||
static void find_parent_level_range(const std::vector<Expression::Token>& tv, Range &p, const Range &c)
|
||||
void find_parent_level_range(const std::vector<Expression::Token>& 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<int>& inner_top_ops, const std::vector<Expression::Token>& mv, const int start, const int finish)
|
||||
void rank_inner_ops(std::vector<int>& inner_top_ops, const std::vector<Expression::Token>& 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<int>& inner_top_ops, const std::vector<Ex
|
||||
}
|
||||
}
|
||||
|
||||
static void weight_operators(std::vector<int>& operator_weights, const std::vector<Expression::Token>& mv, const std::vector<int>& inner_top_ops, const int start)
|
||||
void weight_operators(std::vector<int>& operator_weights, const std::vector<Expression::Token>& mv, const std::vector<int>& 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<int>& operator_weights, const std::vect
|
||||
}
|
||||
}
|
||||
|
||||
static Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& tv, const int location)
|
||||
Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& 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<Expression::T
|
||||
return Expression::Token::ADD;
|
||||
}
|
||||
|
||||
static void open_brackets(std::vector<Expression::Token>& tv, const Range &r)
|
||||
void open_brackets(std::vector<Expression::Token>& 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<Expression::Token>& tv, const Range &r)
|
||||
tv.erase(tv.begin() + brackets.start);
|
||||
}
|
||||
|
||||
static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, const Range &r)
|
||||
int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, const Range &r)
|
||||
{
|
||||
int rank = -1;
|
||||
|
||||
@@ -755,7 +741,7 @@ static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, cons
|
||||
return rank;
|
||||
}
|
||||
|
||||
static void remove_extra_brackets(std::vector<Expression::Token>& tv)
|
||||
void remove_extra_brackets(std::vector<Expression::Token>& tv)
|
||||
{
|
||||
pr_debug("Processing: removing extra brackets");
|
||||
std::vector<int> mp;
|
||||
@@ -816,7 +802,7 @@ static void remove_extra_brackets(std::vector<Expression::Token>& tv)
|
||||
}
|
||||
}
|
||||
|
||||
static void process_remove_empty_brackets(std::vector<Expression::Token>& tv)
|
||||
void process_remove_empty_brackets(std::vector<Expression::Token>& tv)
|
||||
{
|
||||
pr_debug("Pre-process: remove empty brackets");
|
||||
|
||||
@@ -845,7 +831,7 @@ static void process_remove_empty_brackets(std::vector<Expression::Token>& tv)
|
||||
}
|
||||
}
|
||||
|
||||
static int check_bracket_match(const std::vector<Expression::Token>& tv)
|
||||
int check_bracket_match(const std::vector<Expression::Token>& tv)
|
||||
{
|
||||
int problem_cnt = 0;
|
||||
std::stack<int> s;
|
||||
@@ -888,7 +874,7 @@ static int check_bracket_match(const std::vector<Expression::Token>& tv)
|
||||
return problem_cnt;
|
||||
}
|
||||
|
||||
static int check_start_finish_validity(const std::vector<Expression::Token>& tv)
|
||||
int check_start_finish_validity(const std::vector<Expression::Token>& tv)
|
||||
{
|
||||
int problem_cnt = 0;
|
||||
|
||||
@@ -961,7 +947,7 @@ static int check_start_finish_validity(const std::vector<Expression::Token>& tv)
|
||||
return problem_cnt;
|
||||
}
|
||||
|
||||
static int check_token_type_sequence(const std::vector<Expression::Token>& tv)
|
||||
int check_token_type_sequence(const std::vector<Expression::Token>& tv)
|
||||
{
|
||||
int problem_cnt = 0;
|
||||
|
||||
@@ -1103,7 +1089,7 @@ static int check_token_type_sequence(const std::vector<Expression::Token>& tv)
|
||||
return problem_cnt;
|
||||
}
|
||||
|
||||
static void process_unary_minus(std::vector<Expression::Token>& tv)
|
||||
void process_unary_minus(std::vector<Expression::Token>& tv)
|
||||
{
|
||||
pr_debug("Pre-process: unary minus");
|
||||
|
||||
@@ -1157,7 +1143,7 @@ static void process_unary_minus(std::vector<Expression::Token>& tv)
|
||||
|
||||
// rl - relative level (outer priority)
|
||||
// ip - inner priority
|
||||
static int correct_operation_priorities(std::vector<Expression::Token>& tv, int op, int ip)
|
||||
int correct_operation_priorities(std::vector<Expression::Token>& tv, int op, int ip)
|
||||
{
|
||||
int corrections = 0;
|
||||
|
||||
@@ -1237,7 +1223,7 @@ static int correct_operation_priorities(std::vector<Expression::Token>& tv, int
|
||||
return corrections;
|
||||
}
|
||||
|
||||
static int post_process(std::vector<Expression::Token>& tv)
|
||||
int post_process(std::vector<Expression::Token>& tv)
|
||||
{
|
||||
pr_debug("Post processing:");
|
||||
Parser::print_math_vector(tv);
|
||||
@@ -1295,6 +1281,11 @@ int prepare_token_vector(std::vector<Expression::Token>& 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<Expression::Token>& 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("");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user