3 Commits
3 changed files with 113 additions and 77 deletions
+1
View File
@@ -9,5 +9,6 @@ std::vector<Expression::Token> tokenize_input(const char * const input_string);
int assert_same(const std::vector<Expression::Token>& a, const std::vector<Expression::Token>& b); int assert_same(const std::vector<Expression::Token>& a, const std::vector<Expression::Token>& b);
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);
} }
+29
View File
@@ -397,4 +397,33 @@ 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)
{
int i, l, h, hl;
for (i = 0, l = tv.size() - 1, h = 0, hl = highlight_locations.size(); i < l; i++)
{
if (h < hl && highlight_locations[h] == i)
{
std::cout << "\033[31m" << *tv[i].to_math_str() << "\033[0m";
h++;
}
else
{
std::cout << *tv[i].to_math_str();;
}
}
if (tv.size())
{
if (highlight_locations[highlight_locations.size()-1] == 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";
}
}
}
} }
+83 -77
View File
@@ -420,7 +420,7 @@ static void write_operation(
mp.erase(mp.begin() + position, mp.begin() + position + 1); mp.erase(mp.begin() + position, mp.begin() + position + 1);
} }
static void add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction) static 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;
@@ -430,26 +430,26 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
{ {
if (tv[i].t == Expression::Token::OPERATOR) if (tv[i].t == Expression::Token::OPERATOR)
{ {
switch (tv[i].v.o) if (tv[i].v.o == Expression::Token::CPA)
{ {
case Expression::Token::OPA:
lv--;
break;
case Expression::Token::CPA:
lv++; lv++;
break; }
default:
if (!lv) if (!lv)
{ {
tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA)); tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA));
return; return i + 1;
} }
break;
if (tv[i].v.o == Expression::Token::OPA)
{
lv--;
} }
} }
} }
tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA)); tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA));
return 0;
} }
else if (direction == 1) else if (direction == 1)
{ {
@@ -457,26 +457,31 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
{ {
if (tv[i].t == Expression::Token::OPERATOR) if (tv[i].t == Expression::Token::OPERATOR)
{ {
switch (tv[i].v.o) if (tv[i].v.o == Expression::Token::OPA)
{ {
case Expression::Token::OPA:
lv++; lv++;
break; }
case Expression::Token::CPA:
if (!lv)
{
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
return i;
}
if (tv[i].v.o == Expression::Token::CPA)
{
lv--; lv--;
break;
default:
if (!lv)
{
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
return;
}
break;
} }
} }
} }
tv.emplace_back(Expression::Token::CPA); tv.emplace_back(Expression::Token::CPA);
return i;
}
else
{
pr_warn("Invalid direction given, this is a big bug!");
return -1;
} }
} }
@@ -636,7 +641,7 @@ static void weight_operators(std::vector<int>& operator_weights, const std::vect
void apply_corrections(std::vector<Expression::Token>& tv) void apply_corrections(std::vector<Expression::Token>& tv)
{ {
pr_debug("Starting with vector:"); pr_debug("Post processing:");
Parser::print_math_vector(tv); Parser::print_math_vector(tv);
// global prio evaluation // global prio evaluation
@@ -650,68 +655,69 @@ void apply_corrections(std::vector<Expression::Token>& tv)
for (int current_prio_level = mp[get_max_index(mp)]; current_prio_level >= 0; current_prio_level--) for (int current_prio_level = mp[get_max_index(mp)]; current_prio_level >= 0; current_prio_level--)
{ {
// calculate how many ranges need to be processed at this level int corrections = 1;
std::vector<Range> rv; int relative_level = -current_prio_level;
find_select_prio_op_ranges(mp, rv, current_prio_level);
for (int current_range = rv.size() - 1; current_range >= 0; current_range--) while (corrections)
{ {
Range &outer_range = rv[current_range]; corrections = 0;
std::vector<int> inner_prios; int same_op_cnt = -1;
rank_inner_ops_raw(inner_prios, tv, outer_range.start, outer_range.finish); for (int i = 0; i < tv.size(); i++)
for (int inner_prio = inner_prios[get_max_index(inner_prios)]; inner_prio >= 0; inner_prio--)
{ {
std::vector<Range> inner_ranges; Expression::Token& tok = tv[i];
find_select_prio_op_ranges(inner_prios, inner_ranges, inner_prio);
for (int inner_range = inner_ranges.size() - 1; inner_range >= 0; inner_range--) if (tok.t == Expression::Token::OPERATOR)
{ {
int corrections = 1; if (tok.v.o == Expression::Token::OPA)
Range &range = inner_ranges[inner_range];
while (corrections)
{ {
corrections = 0; relative_level++;
}
else if (tok.v.o == Expression::Token::CPA)
{
relative_level--;
}
int same_op_cnt = -1; if (relative_level < 0)
for (int i = outer_range.start + range.start; i < outer_range.start + range.finish + 1; i++) {
same_op_cnt = -1;
}
else if ((tok.v.o == Expression::Token::SUB || tok.v.o == Expression::Token::DIV) && !relative_level)
{
same_op_cnt++;
if (same_op_cnt == 2)
{ {
int loc = get_mop_location(tv, i); same_op_cnt = 0;
Expression::Token& tok = tv[loc];
if (tok.v.o == Expression::Token::SUB || tok.v.o == Expression::Token::DIV) corrections++;
switch (tok.v.o)
{ {
same_op_cnt++; case Expression::Token::SUB:
tok.v.o = Expression::Token::ADD;
if (same_op_cnt == 2) break;
{ case Expression::Token::DIV:
same_op_cnt = 0; tok.v.o = Expression::Token::MUL;
break;
corrections++; default:
pr_warn("Impossible operator in this branch, this is a bug!");
switch (tok.v.o) break;
{
case Expression::Token::SUB:
tok.v.o = Expression::Token::ADD;
break;
case Expression::Token::DIV:
tok.v.o = Expression::Token::MUL;
break;
default:
pr_warn("Impossible operator in this branch, this is a bug!");
break;
}
// inverse order to keep location valid between calls
add_bracket_to(tv, loc, 1);
add_bracket_to(tv, loc, -1);
pr_debug("Updated vector:");
Parser::print_math_vector(tv);
}
} }
// inverse order to keep location valid between calls
std::vector<int> highlight_locations;
highlight_locations.emplace_back(i+1);
highlight_locations.emplace_back(add_bracket_to(tv, i, 1) + 1);
highlight_locations.emplace(highlight_locations.begin(), add_bracket_to(tv, i, -1));
// advance one step to compensate for inserted left bracket
i++;
// elevate one level to compensate for new brackets
relative_level++;
Parser::print_math_vector(tv, highlight_locations);
} }
} }
} }