lab3 fix: avoid moving items in mixed-rank ranges (leads to discarding operator rank logic)
This commit is contained in:
@@ -54,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 {
|
||||
std::shared_ptr<std::string> s;
|
||||
double d;
|
||||
|
||||
+61
-3
@@ -68,6 +68,55 @@ to_return:
|
||||
return r;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -234,14 +283,22 @@ void lab3_sort_by_weight(std::vector<Expression::Token>& tv)
|
||||
ranges.clear();
|
||||
find_select_prio_op_ranges_after_conv(mp, ranges, current_prio);
|
||||
|
||||
const Range &r = ranges[r_no];
|
||||
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--)
|
||||
{
|
||||
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
|
||||
|
||||
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;
|
||||
@@ -283,6 +340,7 @@ void lab3_sort_by_weight(std::vector<Expression::Token>& tv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user