add color highlighting during token post-processing
This commit is contained in:
@@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-6
@@ -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;
|
||||||
|
|
||||||
@@ -438,7 +438,7 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tv[i].v.o == Expression::Token::OPA)
|
if (tv[i].v.o == Expression::Token::OPA)
|
||||||
@@ -449,6 +449,7 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -464,7 +465,7 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
|
|||||||
if (!lv)
|
if (!lv)
|
||||||
{
|
{
|
||||||
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
|
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
|
||||||
return;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tv[i].v.o == Expression::Token::CPA)
|
if (tv[i].v.o == Expression::Token::CPA)
|
||||||
@@ -475,6 +476,12 @@ static void add_bracket_to(std::vector<Expression::Token>& tv, const int locatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -699,8 +706,10 @@ void apply_corrections(std::vector<Expression::Token>& tv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// inverse order to keep location valid between calls
|
// inverse order to keep location valid between calls
|
||||||
add_bracket_to(tv, i, 1);
|
std::vector<int> highlight_locations;
|
||||||
add_bracket_to(tv, i, -1);
|
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
|
// advance one step to compensate for inserted left bracket
|
||||||
i++;
|
i++;
|
||||||
@@ -709,7 +718,7 @@ void apply_corrections(std::vector<Expression::Token>& tv)
|
|||||||
relative_level++;
|
relative_level++;
|
||||||
|
|
||||||
pr_debug("Updated vector:");
|
pr_debug("Updated vector:");
|
||||||
Parser::print_math_vector(tv);
|
Parser::print_math_vector(tv, highlight_locations);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user