diff --git a/inc/parser.hpp b/inc/parser.hpp index f7a8ec9..4e946da 100644 --- a/inc/parser.hpp +++ b/inc/parser.hpp @@ -9,5 +9,6 @@ std::vector tokenize_input(const char * const input_string); int assert_same(const std::vector& a, const std::vector& b); void print_token_vector(const std::vector& tv); void print_math_vector(const std::vector& tv); +void print_math_vector(const std::vector& tv, const std::vector& highlight_locations); } diff --git a/src/parser.cpp b/src/parser.cpp index 7ec7573..e5bbd5e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -397,4 +397,33 @@ void print_math_vector(const std::vector& tv) } } +void print_math_vector(const std::vector& tv, const std::vector& 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"; + } + } +} + } diff --git a/src/syntaxer.cpp b/src/syntaxer.cpp index adfc197..95ffa70 100644 --- a/src/syntaxer.cpp +++ b/src/syntaxer.cpp @@ -420,7 +420,7 @@ static void write_operation( mp.erase(mp.begin() + position, mp.begin() + position + 1); } -static void add_bracket_to(std::vector& tv, const int location, const int direction) +static int add_bracket_to(std::vector& tv, const int location, const int direction) { int i = location + direction, l = tv.size(), lv = 0; @@ -438,7 +438,7 @@ static void add_bracket_to(std::vector& tv, const int locatio if (!lv) { tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA)); - return; + return i + 1; } if (tv[i].v.o == Expression::Token::OPA) @@ -449,6 +449,7 @@ static void add_bracket_to(std::vector& tv, const int locatio } tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA)); + return 0; } else if (direction == 1) { @@ -464,7 +465,7 @@ static void add_bracket_to(std::vector& tv, const int locatio if (!lv) { tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA)); - return; + return i; } if (tv[i].v.o == Expression::Token::CPA) @@ -475,6 +476,12 @@ static void add_bracket_to(std::vector& tv, const int locatio } 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& tv) } // inverse order to keep location valid between calls - add_bracket_to(tv, i, 1); - add_bracket_to(tv, i, -1); + std::vector 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++; @@ -709,7 +718,7 @@ void apply_corrections(std::vector& tv) relative_level++; pr_debug("Updated vector:"); - Parser::print_math_vector(tv); + Parser::print_math_vector(tv, highlight_locations); } } }