7 Commits
11 changed files with 673 additions and 54 deletions
+1
View File
@@ -0,0 +1 @@
build/
+1
View File
@@ -21,6 +21,7 @@ target_include_directories(parser PRIVATE
add_library(syntaxer STATIC) add_library(syntaxer STATIC)
target_sources(syntaxer PRIVATE target_sources(syntaxer PRIVATE
src/syntaxer.cpp src/syntaxer.cpp
src/lab3.cpp
) )
target_include_directories(syntaxer PRIVATE target_include_directories(syntaxer PRIVATE
${GLOBAL_INC} ${GLOBAL_INC}
+3
View File
@@ -9,6 +9,9 @@
#define LAB2_EXCLUDE_COMPTIME_OP_WRITES 1 #define LAB2_EXCLUDE_COMPTIME_OP_WRITES 1
#define LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO 1 #define LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO 1
#define LAB3_EXTENSION 1
#define LAB3_WEIGHT_SORT 1
#define DYN_PIPELINE_MEM_SIZE 512 #define DYN_PIPELINE_MEM_SIZE 512
#define DYN_PIPELINE_LAYERS 3 #define DYN_PIPELINE_LAYERS 3
#define DYN_PIPELINE_OUT_OF_ORDER_EXECUTION 1 #define DYN_PIPELINE_OUT_OF_ORDER_EXECUTION 1
+49 -6
View File
@@ -37,7 +37,6 @@ public:
case Expression::Token::OPA: case Expression::Token::OPA:
case Expression::Token::CPA: case Expression::Token::CPA:
case Expression::Token::NOP: case Expression::Token::NOP:
pr_warn("Unweightable operator - this is probably a bug!");
return 0; return 0;
case Expression::Token::ADD: case Expression::Token::ADD:
return 1; return 1;
@@ -46,7 +45,7 @@ public:
case Expression::Token::MUL: case Expression::Token::MUL:
return 2; return 2;
case Expression::Token::DIV: case Expression::Token::DIV:
return 2; return 3;
case Expression::Token::POW: case Expression::Token::POW:
return 3; return 3;
default: default:
@@ -55,6 +54,34 @@ public:
} }
} }
[[nodiscard]] int rank(void) const
{
if (t != Expression::Token::OPERATOR)
{
pr_warn("Not an operator. This is a bug!");
return -1;
}
switch (v.o)
{
case Expression::Token::OPA:
case Expression::Token::CPA:
case Expression::Token::NOP:
return -1;
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:
pr_warn("Impossible operator - this is a bug!");
return -1;
}
}
union TokenValue { union TokenValue {
std::shared_ptr<std::string> s; std::shared_ptr<std::string> s;
double d; double d;
@@ -315,12 +342,18 @@ public:
return (v.o == SUB || v.o == DIV); 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); return (o == SUB || o == DIV);
} }
int is_math_op(void) const [[nodiscard]] int is_math_op(void) const
{ {
if (t != OPERATOR) if (t != OPERATOR)
{ {
@@ -337,7 +370,12 @@ public:
} }
} }
int is_opa(void) const [[nodiscard]] int is_mop(void) const
{
return is_math_op();
}
[[nodiscard]] int is_opa(void) const
{ {
if (t != OPERATOR) if (t != OPERATOR)
{ {
@@ -347,7 +385,7 @@ public:
return v.o == OPA; return v.o == OPA;
} }
int is_cpa(void) const [[nodiscard]] int is_cpa(void) const
{ {
if (t != OPERATOR) if (t != OPERATOR)
{ {
@@ -356,6 +394,11 @@ public:
return v.o == CPA; return v.o == CPA;
} }
[[nodiscard]] int is_bracket(void) const
{
return is_opa() || is_cpa();
}
}; };
struct Operation struct Operation
+2
View File
@@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include "expression.hpp" #include "expression.hpp"
#include "syntaxer.hpp"
namespace Parser { 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_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);
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 std::vector<int>& highlight_locations);
void print_math_vector(const std::vector<Expression::Token>& tv, const Syntax::Range& hr);
} }
+46
View File
@@ -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);
}
+22
View File
@@ -6,9 +6,31 @@
namespace Syntax 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); int prepare_token_vector(std::vector<Expression::Token>& tv);
std::vector<Expression::Operation> build_op_vector(const 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); 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); void print_op_tree(const Expression::TreeNode &node);
// lab3 extension
void lab3_sort_by_weight(std::vector<Expression::Token>& tv);
} }
View File
+480
View File
@@ -0,0 +1,480 @@
#include "expression.hpp"
#include "parser.hpp"
#include "print.h"
#include "syntaxer.hpp"
#include "syntax-priv.hpp"
#include <cstring>
#include <vector>
namespace Syntax {
template<int N>
struct Count
{
int counters[N];
Count(void)
{
memset(counters, 0, sizeof(int) * N);
}
int operator[](const int index)
{
counters[index]++;
return index;
}
int read(const int index) const
{
return counters[index];
}
};
template<typename T>
static int in_vector(const std::vector<T>& v, const int value)
{
for (int i : v)
{
if (i == value)
{
return 1;
}
}
return 0;
}
static int first_available(const std::vector<int> v, const int start = 0)
{
int i = start;
for ( ; ; i++)
{
if (!in_vector(v, i))
{
return i;
}
}
}
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 ranked_op_present(std::vector<Expression::Token>& tv, const Range& mr, const int current_rank)
{
for (int i = mr.start; i <= mr.finish; i++)
{
if (tv[get_mop_location(tv, i)].rank() == current_rank)
{
return 1;
}
}
return 0;
}
static void split_into_ranked_ranges(std::vector<Range>& rr, std::vector<Expression::Token>& tv, const std::vector<int>& mp, const Range& mr, const int current_rank, const int current_prio)
{
Range cr;
bool writing_range = false;
for (int i = mr.start; i <= mr.finish; i++)
{
const int p = mp[i];
const Expression::Token &t = tv[get_mop_location(tv, i)];
if (p == current_prio && writing_range)
{
if (t.rank() >= current_rank)
{
cr.finish = i;
}
else
{
rr.push_back(cr);
writing_range = false;
}
}
else if (p == current_prio && !writing_range)
{
if (t.rank() >= current_rank)
{
cr.start = i;
cr.finish = i;
writing_range = true;
}
}
else if (p > current_prio && writing_range)
{
cr.finish = i;
}
else if (p > current_prio && !writing_range)
{
cr.start = i;
cr.finish = i;
writing_range = true;
}
}
if (writing_range)
{
rr.push_back(cr);
}
}
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]];
//Parser::print_math_vector(tv, cr);
// if the first term is moved into diferent place,
// no sign info is available; assume ADD and place it in manually
if (places[i] == 0)
{
rtv[o++] = Expression::Token::ADD;
o += copy_range(rtv, o, tv, cr);
}
else
{
cr.start--; // include prior math operator 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 &mr = ranges[r_no]; // range of same-level neighbour mixed-rank operators
// split range and iterate over every same-rank chunk
for (int current_rank = 2; current_rank >= 0; current_rank--)
{
if (!ranked_op_present(tv, mr, current_rank))
{
continue;
}
std::vector<Range> rr; // ranked ranges
split_into_ranked_ranges(rr, tv, mp, mr, current_rank, current_prio);
for (int rri = 0; rri < (int) rr.size(); rri++)
{
const Range &r = rr[rri];
Range er; // expanded range
{
Range ers[2];
ers[0] = expand_range(tv, get_mop_location(tv, r.start));
ers[1] = expand_range(tv, get_mop_location(tv, r.finish));
er.start = ers[0].start < ers[1].start ? ers[0].start : ers[1].start;
er.finish = ers[0].finish > ers[1].finish ? ers[0].finish : ers[1].finish;
}
//er.start = expand_range(tv, get_mop_location(tv, r.start)).start;
//er.finish = expand_range(tv, get_mop_location(tv, r.finish)).finish;
std::vector<Range> rs = split_ranges_by_rank(tv, er, current_rank);
// apply negative bias
Count<2> cnts; // save ranges to sort within
std::vector<int> range_target; // 0 -> positive, 1 -> negative
range_target.emplace_back(cnts[0]); // first range has no sign, default to positive
for (int i = 1; i < (int) rs.size(); i++)
{
range_target.emplace_back(cnts[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));
#if LAB3_WEIGHT_SORT == 1
// sorting
rs = split_ranges_by_rank(tv, er, current_rank); // update ranges after reordering
// get all weights first
std::vector<int> weights;
for (int i = 0, k = rs.size(); i < k; i++)
{
weights.emplace_back(total_weight(tv, rs[i]));
}
// sort positive items
places.clear();
for (int i = 0, k = cnts.read(0); i < k; i++)
{
int smallest = first_available(places);
int j;
for (j = 1; j < cnts.read(0); j++)
{
if ((weights[j] < weights[smallest]) && !in_vector(places, j))
{
smallest = j;
}
}
places.emplace_back(smallest);
}
// sort negative items
for (int i = cnts.read(0), k = rs.size(); i < k; i++)
{
int smallest = first_available(places, cnts.read(0));
int j;
for (j = cnts.read(0) + 1; j < (int) rs.size(); j++)
{
if ((weights[j] < weights[smallest]) && !in_vector(places, j))
{
smallest = j;
}
}
places.emplace_back(smallest);
}
reorder_ranges(tv, rs, places);
pr_debug("Range weight-sorted:");
Parser::print_math_vector(tv, Range(rs[0].start, rs[rs.size()-1].finish));
#endif
}
}
}
}
}
}
+30
View File
@@ -1,4 +1,5 @@
#include "expression.hpp" #include "expression.hpp"
#include "syntaxer.hpp"
#include "print.h" #include "print.h"
#include <cctype> #include <cctype>
#include <cstring> #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
View File
@@ -9,23 +9,9 @@
namespace Syntax { namespace Syntax {
struct Range int add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction);
{
int start, finish;
Range(void) int rank_op(const Expression::Token::Operator o)
{
}
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)
{ {
switch (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--) 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; 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++) 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; 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 start = location - 7 < 0 ? 0 : location - 7;
int finish = (size_t) location + 7 > tv.size() - 1 ? tv.size() - 1 : 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>) // get mop = Math OPerator location (=absolute index in std::vector<Expression::Token>)
// mapper of 'priority vector index' -> 'token vector index' // 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; int i = 0, l = v.size(), p = -1;
for (; ({if (!(i < l)) return i;}), p += v[i].t == Expression::Token::OPERATOR 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; 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--) 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; 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++) 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; 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; int m = 3;
@@ -185,7 +171,7 @@ static int min_rank(const std::vector<Expression::Token> &tv, const std::vector<
return m; 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; int weight = 0;
@@ -218,7 +204,7 @@ static int get_operator_weight(const std::vector<Expression::Token>& v, int loca
return weight; return weight;
} }
static int get_min_index(const std::vector<int>& v) int get_min_index(const std::vector<int>& v)
{ {
int idx = 0; int idx = 0;
@@ -233,7 +219,7 @@ static int get_min_index(const std::vector<int>& v)
return idx; return idx;
} }
static int get_max_index(const std::vector<int>& v) int get_max_index(const std::vector<int>& v)
{ {
int idx = 0; int idx = 0;
@@ -248,7 +234,7 @@ static int get_max_index(const std::vector<int>& v)
return idx; return idx;
} }
static int write_operation( int write_operation(
std::vector<Expression::Operation>& ov, std::vector<Expression::Operation>& ov,
std::vector<Expression::Token>& cv, std::vector<Expression::Token>& cv,
std::vector<Expression::Token>& mv, std::vector<Expression::Token>& mv,
@@ -352,7 +338,7 @@ static int write_operation(
return 0; 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; 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); 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; 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); 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; 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++) 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++) 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) // find operation ranges that are of selected priority (target_prio)
// accounting for future token convolution into compound values // 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++) 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 // left side
for (int i = get_mop_location(tv, c.start), pc = c.start, lv = 0; i >= 0; i--) 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; 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++) 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++) 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--) 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; 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( Range brackets(
same_lvl_opa_location(tv, get_mop_location(tv, r.start)), 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); 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; int rank = -1;
@@ -755,7 +741,7 @@ static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, cons
return rank; 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"); pr_debug("Processing: removing extra brackets");
std::vector<int> mp; 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"); 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; int problem_cnt = 0;
std::stack<int> s; std::stack<int> s;
@@ -888,7 +874,7 @@ static int check_bracket_match(const std::vector<Expression::Token>& tv)
return problem_cnt; 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; int problem_cnt = 0;
@@ -961,7 +947,7 @@ static int check_start_finish_validity(const std::vector<Expression::Token>& tv)
return problem_cnt; 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; int problem_cnt = 0;
@@ -1103,7 +1089,7 @@ static int check_token_type_sequence(const std::vector<Expression::Token>& tv)
return problem_cnt; 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"); 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) // rl - relative level (outer priority)
// ip - inner 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; int corrections = 0;
@@ -1237,7 +1223,7 @@ static int correct_operation_priorities(std::vector<Expression::Token>& tv, int
return corrections; return corrections;
} }
static int post_process(std::vector<Expression::Token>& tv) int post_process(std::vector<Expression::Token>& tv)
{ {
pr_debug("Post processing:"); pr_debug("Post processing:");
Parser::print_math_vector(tv); Parser::print_math_vector(tv);
@@ -1295,6 +1281,11 @@ int prepare_token_vector(std::vector<Expression::Token>& tv)
} }
remove_extra_brackets(tv); remove_extra_brackets(tv);
#if LAB3_EXTENSION == 1
Syntax::lab3_sort_by_weight(tv);
#endif
(void) post_process(tv); (void) post_process(tv);
remove_extra_brackets(tv); remove_extra_brackets(tv);
@@ -1432,7 +1423,7 @@ Expression::TreeNode ov_to_tree(const std::vector<Expression::Token>& tv, const
return root; 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(""); prs_info("");