cleanup + minor fixes
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
|
SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra -Wpedantic")
|
||||||
|
|
||||||
project(pzks)
|
project(pzks)
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -52,6 +52,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
explicit TokenValue(void)
|
explicit TokenValue(void)
|
||||||
|
: s(std::shared_ptr<std::string>())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +145,7 @@ public:
|
|||||||
switch (t)
|
switch (t)
|
||||||
{
|
{
|
||||||
case VAR:
|
case VAR:
|
||||||
|
new(&v.s) std::shared_ptr<std::string>;
|
||||||
v.s = other.v.s;
|
v.s = other.v.s;
|
||||||
break;
|
break;
|
||||||
case CONST:
|
case CONST:
|
||||||
@@ -174,7 +176,6 @@ public:
|
|||||||
return v.s;
|
return v.s;
|
||||||
case CONST:
|
case CONST:
|
||||||
{
|
{
|
||||||
char buffer[128] = {0};
|
|
||||||
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
|
return std::make_shared<std::string>((std::ostringstream() << v.d).str());
|
||||||
}
|
}
|
||||||
case OPERATOR:
|
case OPERATOR:
|
||||||
@@ -186,8 +187,6 @@ public:
|
|||||||
|
|
||||||
std::shared_ptr<std::string> to_math_str(void) const
|
std::shared_ptr<std::string> to_math_str(void) const
|
||||||
{
|
{
|
||||||
char buffer[128] = {0};
|
|
||||||
|
|
||||||
switch (t)
|
switch (t)
|
||||||
{
|
{
|
||||||
case VAR:
|
case VAR:
|
||||||
|
|||||||
+1
-1
@@ -411,7 +411,7 @@ void print_math_vector(const std::vector<Expression::Token>& tv, const std::vect
|
|||||||
|
|
||||||
if (tv.size())
|
if (tv.size())
|
||||||
{
|
{
|
||||||
if (highlight_locations[highlight_locations.size()-1] == tv.size() - 1)
|
if (highlight_locations[highlight_locations.size()-1] == (long) (tv.size() - 1))
|
||||||
{
|
{
|
||||||
std::cout << "\033[31m" << *tv[tv.size() - 1].to_math_str() << "\033[0m\n";
|
std::cout << "\033[31m" << *tv[tv.size() - 1].to_math_str() << "\033[0m\n";
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-192
@@ -38,7 +38,6 @@ static int rank_op(const Expression::Token::Operator o)
|
|||||||
case Expression::Token::POW:
|
case Expression::Token::POW:
|
||||||
return 2;
|
return 2;
|
||||||
default:
|
default:
|
||||||
//pr_warn("Operator is unrankable");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +109,7 @@ static int same_lvl_cpa_location(const std::vector<Expression::Token>& tv, const
|
|||||||
static void highlight_issue(const std::vector<Expression::Token>& tv, const int location)
|
static 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 = location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7;
|
int finish = (size_t) location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7;
|
||||||
|
|
||||||
std::cout << " >>> ";
|
std::cout << " >>> ";
|
||||||
|
|
||||||
@@ -167,25 +166,6 @@ static int get_right_location(const std::vector<Expression::Token>& v, int locat
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int max_rank(const std::vector<Expression::Token> &tv, const std::vector<int> &mp, const Range &r, const int base_lv)
|
|
||||||
{
|
|
||||||
int m = -1;
|
|
||||||
|
|
||||||
for (int i = r.start; i <= r.finish; i++)
|
|
||||||
{
|
|
||||||
if (mp[i] != base_lv)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rank = rank_op(tv[get_mop_location(tv, i)].v.o);
|
|
||||||
|
|
||||||
m = rank > m ? rank : m;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int min_rank(const std::vector<Expression::Token> &tv, const std::vector<int> &mp, const Range &r, const int base_lv)
|
static 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;
|
||||||
@@ -568,81 +548,6 @@ static void find_select_prio_op_ranges_after_conv(const std::vector<int>& mp, st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void find_select_prio_op_ranges(const std::vector<int>& mp, std::vector<Range>& ranges, int target_prio)
|
|
||||||
{
|
|
||||||
for (int i = 0, l = mp.size(), prio = 0, recording_range = 0; i < l; i++)
|
|
||||||
{
|
|
||||||
if (mp[i] == target_prio && !recording_range)
|
|
||||||
{
|
|
||||||
recording_range = 1;
|
|
||||||
ranges.emplace_back(i, i);
|
|
||||||
}
|
|
||||||
else if (mp[i] == target_prio && recording_range)
|
|
||||||
{
|
|
||||||
ranges.back().finish = i;
|
|
||||||
}
|
|
||||||
else if (mp[i] != target_prio && recording_range)
|
|
||||||
{
|
|
||||||
recording_range = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void find_parent_level_range(const std::vector<Expression::Token>& tv, int &p_start, int &p_finish, const int c_start, const int c_finish)
|
|
||||||
{
|
|
||||||
// left side
|
|
||||||
for (int i = get_mop_location(tv, c_start), p = c_start, lv = 0; i >= 0; i--)
|
|
||||||
{
|
|
||||||
if (tv[i].is_opa())
|
|
||||||
{
|
|
||||||
lv--;
|
|
||||||
|
|
||||||
if (lv == -2)
|
|
||||||
{
|
|
||||||
p_start = p;
|
|
||||||
goto right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (tv[i].is_math_op())
|
|
||||||
{
|
|
||||||
p--;
|
|
||||||
}
|
|
||||||
else if (tv[i].is_cpa())
|
|
||||||
{
|
|
||||||
lv++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p_start = 0;
|
|
||||||
|
|
||||||
right:
|
|
||||||
// right side
|
|
||||||
int i, p, lv;
|
|
||||||
for (i = get_mop_location(tv, c_finish), p = c_finish, lv = 0; i < tv.size(); i++)
|
|
||||||
{
|
|
||||||
if (tv[i].is_cpa())
|
|
||||||
{
|
|
||||||
lv--;
|
|
||||||
|
|
||||||
if (lv == -2)
|
|
||||||
{
|
|
||||||
p_finish = p;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (tv[i].is_math_op())
|
|
||||||
{
|
|
||||||
p++;
|
|
||||||
}
|
|
||||||
else if (tv[i].is_opa())
|
|
||||||
{
|
|
||||||
lv++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
p_finish = p - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void find_parent_level_range(const std::vector<Expression::Token>& tv, Range &p, const Range &c)
|
static void find_parent_level_range(const std::vector<Expression::Token>& tv, Range &p, const Range &c)
|
||||||
{
|
{
|
||||||
// left side
|
// left side
|
||||||
@@ -673,7 +578,7 @@ static void find_parent_level_range(const std::vector<Expression::Token>& tv, Ra
|
|||||||
right:
|
right:
|
||||||
// right side
|
// right side
|
||||||
int i, pc, lv;
|
int i, pc, lv;
|
||||||
for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; i < tv.size(); i++)
|
for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; (size_t) i < tv.size(); i++)
|
||||||
{
|
{
|
||||||
if (tv[i].is_cpa())
|
if (tv[i].is_cpa())
|
||||||
{
|
{
|
||||||
@@ -698,56 +603,6 @@ right:
|
|||||||
p.finish = pc - 1;
|
p.finish = pc - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rank_inner_ops_raw(std::vector<int>& inner_op_ranks, const std::vector<Expression::Token>& mv, const int start, const int finish, const int same_lvl_only = 0)
|
|
||||||
{
|
|
||||||
for (int i = get_mop_location(mv, start), lvl = 0, f = get_mop_location(mv, finish); i <= f; i++)
|
|
||||||
{
|
|
||||||
//int idx = get_mop_location(mv, start + i);
|
|
||||||
const Expression::Token &t = mv[i];
|
|
||||||
|
|
||||||
if (t.t != Expression::Token::OPERATOR)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (t.v.o)
|
|
||||||
{
|
|
||||||
case Expression::Token::ADD:
|
|
||||||
case Expression::Token::SUB:
|
|
||||||
if (same_lvl_only && lvl)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
inner_op_ranks.push_back(0);
|
|
||||||
break;
|
|
||||||
case Expression::Token::MUL:
|
|
||||||
case Expression::Token::DIV:
|
|
||||||
if (same_lvl_only && lvl)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
inner_op_ranks.push_back(1);
|
|
||||||
break;
|
|
||||||
case Expression::Token::POW:
|
|
||||||
if (same_lvl_only && lvl)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
inner_op_ranks.push_back(2);
|
|
||||||
break;
|
|
||||||
case Expression::Token::OPA:
|
|
||||||
lvl++;
|
|
||||||
break;
|
|
||||||
case Expression::Token::CPA:
|
|
||||||
lvl--;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
pr_warn("Operator is unrankable");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rank_inner_ops(std::vector<int>& inner_top_ops, const std::vector<Expression::Token>& mv, const int start, const int finish)
|
static 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++)
|
||||||
@@ -801,26 +656,6 @@ static void weight_operators(std::vector<int>& operator_weights, const std::vect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int same_rank_ops(const std::vector<Expression::Token>& tv, const std::vector<int> &mp, const Range &r, const int outer_lvl)
|
|
||||||
{
|
|
||||||
int first_op_inner_level = rank_op(tv[get_mop_location(tv, r.start)].v.o);
|
|
||||||
|
|
||||||
for (int i = r.start; i <= r.finish; i++)
|
|
||||||
{
|
|
||||||
if (mp[i] != outer_lvl)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first_op_inner_level != rank_op(tv[get_mop_location(tv, i)].v.o))
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& tv, const int location)
|
static 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--)
|
||||||
@@ -899,12 +734,7 @@ static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, cons
|
|||||||
{
|
{
|
||||||
int rank = -1;
|
int rank = -1;
|
||||||
|
|
||||||
// left neighbour
|
|
||||||
//if (r.start - 1 >= 0)
|
|
||||||
//if ()
|
|
||||||
//{
|
|
||||||
int left_bracket = same_lvl_opa_location(tv, get_mop_location(tv, r.start));
|
int left_bracket = same_lvl_opa_location(tv, get_mop_location(tv, r.start));
|
||||||
//if (int ro = rank_op(tv[get_mop_location(tv, r.start - 1)].v.o); ro > rank)
|
|
||||||
if (left_bracket - 1 >= 0)
|
if (left_bracket - 1 >= 0)
|
||||||
{
|
{
|
||||||
if (int ro = rank_op(tv[left_bracket - 1].v.o); ro > rank)
|
if (int ro = rank_op(tv[left_bracket - 1].v.o); ro > rank)
|
||||||
@@ -912,19 +742,9 @@ static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, cons
|
|||||||
rank = ro;
|
rank = ro;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
// right neighbour
|
|
||||||
//if (int rl = get_mop_location(tv, r.finish + 1); rl < tv.size())
|
|
||||||
//{
|
|
||||||
// if (int ro = rank_op(tv[rl].v.o); ro > rank)
|
|
||||||
// {
|
|
||||||
// rank = ro;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
int right_bracket = same_lvl_cpa_location(tv, get_mop_location(tv, r.finish));
|
int right_bracket = same_lvl_cpa_location(tv, get_mop_location(tv, r.finish));
|
||||||
if (right_bracket + 1 < tv.size())
|
if ((size_t) (right_bracket + 1) < tv.size())
|
||||||
{
|
{
|
||||||
if (int ro = rank_op(tv[right_bracket + 1].v.o); ro > rank)
|
if (int ro = rank_op(tv[right_bracket + 1].v.o); ro > rank)
|
||||||
{
|
{
|
||||||
@@ -941,6 +761,11 @@ static void remove_extra_brackets(std::vector<Expression::Token>& tv)
|
|||||||
std::vector<int> mp;
|
std::vector<int> mp;
|
||||||
create_prios_vector(tv, mp);
|
create_prios_vector(tv, mp);
|
||||||
|
|
||||||
|
if (mp.size() == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// for everything excluding level 0
|
// for everything excluding level 0
|
||||||
for (int current_prio = mp[get_max_index(mp)]; current_prio > 0; current_prio--)
|
for (int current_prio = mp[get_max_index(mp)]; current_prio > 0; current_prio--)
|
||||||
{
|
{
|
||||||
@@ -999,7 +824,7 @@ static void process_remove_empty_brackets(std::vector<Expression::Token>& tv)
|
|||||||
while (fix) {
|
while (fix) {
|
||||||
fix = 0;
|
fix = 0;
|
||||||
|
|
||||||
for (int i = 0; i < tv.size() - 1; i++)
|
for (int i = 0; i < (long) tv.size() - 1; i++)
|
||||||
{
|
{
|
||||||
if (tv[i].t == Expression::Token::OPERATOR
|
if (tv[i].t == Expression::Token::OPERATOR
|
||||||
&& tv[i].v.o == Expression::Token::OPA
|
&& tv[i].v.o == Expression::Token::OPA
|
||||||
@@ -1283,11 +1108,25 @@ static void process_unary_minus(std::vector<Expression::Token>& tv)
|
|||||||
pr_debug("Pre-process: unary minus");
|
pr_debug("Pre-process: unary minus");
|
||||||
|
|
||||||
int change = 1;
|
int change = 1;
|
||||||
|
|
||||||
|
if (tv.size() >= 2 && tv[0].t == Expression::Token::OPERATOR && tv[0].v.o == Expression::Token::SUB)
|
||||||
|
{
|
||||||
|
std::vector<int> highlight;
|
||||||
|
|
||||||
|
tv.emplace(tv.begin(), 0.0);
|
||||||
|
tv.emplace(tv.begin(), Expression::Token::OPA);
|
||||||
|
highlight.emplace_back(add_bracket_to(tv, 2, 1));
|
||||||
|
|
||||||
|
highlight.emplace(highlight.begin(), 1);
|
||||||
|
highlight.emplace(highlight.begin(), 0);
|
||||||
|
Parser::print_math_vector(tv, highlight);
|
||||||
|
}
|
||||||
|
|
||||||
while (change)
|
while (change)
|
||||||
{
|
{
|
||||||
change = 0;
|
change = 0;
|
||||||
|
|
||||||
for (int i = 0; i < tv.size() - 2; i++)
|
for (int i = 0; i < (long) tv.size() - 2; i++)
|
||||||
{
|
{
|
||||||
if (tv[i].t == Expression::Token::OPERATOR
|
if (tv[i].t == Expression::Token::OPERATOR
|
||||||
&& tv[i].v.o != Expression::Token::CPA
|
&& tv[i].v.o != Expression::Token::CPA
|
||||||
@@ -1300,8 +1139,8 @@ static void process_unary_minus(std::vector<Expression::Token>& tv)
|
|||||||
{
|
{
|
||||||
std::vector<int> highlight;
|
std::vector<int> highlight;
|
||||||
|
|
||||||
tv.insert(tv.begin() + i + 1, Expression::Token(0.0));
|
tv.insert(tv.begin() + i + 1, 0.0);
|
||||||
tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA));
|
tv.insert(tv.begin() + i + 1, Expression::Token::OPA);
|
||||||
highlight.emplace_back(add_bracket_to(tv, i + 3, 1));
|
highlight.emplace_back(add_bracket_to(tv, i + 3, 1));
|
||||||
|
|
||||||
highlight.emplace(highlight.begin(), i+2);
|
highlight.emplace(highlight.begin(), i+2);
|
||||||
@@ -1322,7 +1161,7 @@ static int correct_operation_priorities(std::vector<Expression::Token>& tv, int
|
|||||||
{
|
{
|
||||||
int corrections = 0;
|
int corrections = 0;
|
||||||
|
|
||||||
for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; i < tv.size(); i++)
|
for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; (size_t) i < tv.size(); i++)
|
||||||
{
|
{
|
||||||
Expression::Token& tok = tv[i];
|
Expression::Token& tok = tv[i];
|
||||||
|
|
||||||
@@ -1391,7 +1230,6 @@ static int correct_operation_priorities(std::vector<Expression::Token>& tv, int
|
|||||||
{
|
{
|
||||||
same_lvl_op_cnt = -1;
|
same_lvl_op_cnt = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1456,12 +1294,9 @@ int prepare_token_vector(std::vector<Expression::Token>& tv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//for (int corrs = -1; corrs; )
|
|
||||||
//{
|
|
||||||
remove_extra_brackets(tv);
|
remove_extra_brackets(tv);
|
||||||
(void) post_process(tv);
|
(void) post_process(tv);
|
||||||
remove_extra_brackets(tv);
|
remove_extra_brackets(tv);
|
||||||
//}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user