2026-10-04 14:13:14 +03:00
|
|
|
#include "expression.hpp"
|
2026-10-06 21:02:40 +03:00
|
|
|
#include "parser.hpp"
|
2026-10-04 14:13:14 +03:00
|
|
|
#include "print.h"
|
2026-10-06 21:02:40 +03:00
|
|
|
#include "syntaxer.hpp"
|
2026-10-14 20:07:28 +03:00
|
|
|
#include <cmath>
|
2026-10-07 16:15:42 +03:00
|
|
|
#include <queue>
|
2026-10-06 21:02:40 +03:00
|
|
|
#include <stack>
|
|
|
|
|
#include <vector>
|
2026-10-04 14:13:14 +03:00
|
|
|
|
|
|
|
|
namespace Syntax {
|
|
|
|
|
|
2026-10-15 09:03:54 +03:00
|
|
|
struct Range
|
|
|
|
|
{
|
|
|
|
|
int start, finish;
|
|
|
|
|
|
|
|
|
|
Range(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Range(int start, int finish)
|
|
|
|
|
: start(start), finish(finish)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-14 12:00:41 +03:00
|
|
|
static int add_bracket_to(std::vector<Expression::Token>& tv, const int location, const int direction);
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static int rank_op(const Expression::Token::Operator o)
|
|
|
|
|
{
|
|
|
|
|
switch (o)
|
|
|
|
|
{
|
|
|
|
|
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:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 21:17:32 +03:00
|
|
|
static int same_lvl_opa_location(const std::vector<Expression::Token>& tv, const int location)
|
|
|
|
|
{
|
|
|
|
|
for (int i = location - 1, lv = 0; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
const Expression::Token &t = tv[i];
|
|
|
|
|
|
|
|
|
|
if (t.t != Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
if (!lv)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int same_lvl_cpa_location(const std::vector<Expression::Token>& tv, const int location)
|
|
|
|
|
{
|
|
|
|
|
for (int i = location + 1, lv = 0, l = tv.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
const Expression::Token &t = tv[i];
|
|
|
|
|
|
|
|
|
|
if (t.t != Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
if (!lv)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tv.size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-06 21:02:40 +03:00
|
|
|
static void highlight_issue(const std::vector<Expression::Token>& tv, const int location)
|
2026-10-04 14:13:14 +03:00
|
|
|
{
|
2026-10-06 21:02:40 +03:00
|
|
|
int start = location - 7 < 0 ? 0 : location - 7;
|
2026-10-15 16:42:52 +03:00
|
|
|
int finish = (size_t) location + 7 > tv.size() - 1 ? tv.size() - 1 : location + 7;
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-07 17:35:04 +03:00
|
|
|
std::cout << " >>> ";
|
2026-10-06 21:02:40 +03:00
|
|
|
|
|
|
|
|
for (int i = start; i <= finish; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i == location)
|
|
|
|
|
{
|
|
|
|
|
std::cout << "\033[31m" << *tv[i].to_math_str() << "\033[0m ";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
std::cout << *tv[i].to_math_str() << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << "\n";
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
// get mop = Math OPerator location (=absolute index in std::vector<Expression::Token>)
|
|
|
|
|
// mapper of 'priority vector index' -> 'token vector index'
|
|
|
|
|
static int get_mop_location(const std::vector<Expression::Token>& v, int position)
|
|
|
|
|
{
|
|
|
|
|
int i = 0, l = v.size(), p = -1;
|
2026-10-15 09:03:54 +03:00
|
|
|
for (; ({if (!(i < l)) return i;}), p += v[i].t == Expression::Token::OPERATOR
|
2026-10-14 19:57:02 +03:00
|
|
|
&& v[i].v.o != Expression::Token::OPA
|
|
|
|
|
&& v[i].v.o != Expression::Token::CPA, p != position; i++);
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_left_location(const std::vector<Expression::Token>& v, int location)
|
|
|
|
|
{
|
|
|
|
|
for (int i = location - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (v[i].t != Expression::Token::OPERATOR && v[i].t != Expression::Token::NAT)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_right_location(const std::vector<Expression::Token>& v, int location)
|
|
|
|
|
{
|
|
|
|
|
for (int i = location + 1, l = v.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (v[i].t != Expression::Token::OPERATOR && v[i].t != Expression::Token::NAT)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 09:03:54 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static int get_operator_weight(const std::vector<Expression::Token>& v, int location)
|
|
|
|
|
{
|
|
|
|
|
int weight = 0;
|
|
|
|
|
|
|
|
|
|
switch (const Expression::Token &p = v[get_left_location(v, location)]; p.t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CV:
|
|
|
|
|
weight += p.v.cv.w;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Invalid token found to the left of operator");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (const Expression::Token &p = v[get_right_location(v, location)]; p.t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CV:
|
|
|
|
|
weight += p.v.cv.w;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Invalid token found to the left of operator");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_min_index(const std::vector<int>& v)
|
|
|
|
|
{
|
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 1, l = v.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (v[i] < v[idx])
|
|
|
|
|
{
|
|
|
|
|
idx = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int get_max_index(const std::vector<int>& v)
|
|
|
|
|
{
|
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 1, l = v.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (v[i] > v[idx])
|
|
|
|
|
{
|
|
|
|
|
idx = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int write_operation(
|
|
|
|
|
std::vector<Expression::Operation>& ov,
|
|
|
|
|
std::vector<Expression::Token>& cv,
|
|
|
|
|
std::vector<Expression::Token>& mv,
|
|
|
|
|
std::vector<int>& mp,
|
|
|
|
|
int position)
|
|
|
|
|
{
|
|
|
|
|
int location = get_mop_location(mv, position);
|
|
|
|
|
int left_location = get_left_location(mv, location);
|
|
|
|
|
int right_location = get_right_location(mv, location);
|
|
|
|
|
|
|
|
|
|
std::vector<int> hl;
|
|
|
|
|
hl.emplace_back(left_location);
|
|
|
|
|
hl.emplace_back(location);
|
|
|
|
|
hl.emplace_back(right_location);
|
|
|
|
|
Parser::print_math_vector(mv, hl);
|
|
|
|
|
|
|
|
|
|
Expression::Token &o = mv[location],
|
|
|
|
|
&l = mv[left_location],
|
|
|
|
|
&r = mv[right_location];
|
|
|
|
|
|
|
|
|
|
#if LAB2_EXCLUDE_COMPTIME_OP_WRITES == 1
|
|
|
|
|
if (l.t == Expression::Token::CONST && r.t == Expression::Token::CONST)
|
|
|
|
|
{
|
|
|
|
|
pr_debug("CONST folding");
|
|
|
|
|
|
|
|
|
|
switch (o.v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::ADD:
|
|
|
|
|
o.v.d = l.v.d + r.v.d;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::SUB:
|
|
|
|
|
o.v.d = l.v.d - r.v.d;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::MUL:
|
|
|
|
|
o.v.d = l.v.d * r.v.d;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::DIV:
|
|
|
|
|
if (r.v.d != 0.0)
|
|
|
|
|
{
|
|
|
|
|
o.v.d = l.v.d / r.v.d;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Right operand is zero, invalid operation");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::POW:
|
|
|
|
|
o.v.d = std::pow(l.v.d, r.v.d);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible operator, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
l.t = Expression::Token::NAT;
|
|
|
|
|
r.t = Expression::Token::NAT;
|
|
|
|
|
o.t = Expression::Token::CONST;
|
|
|
|
|
|
|
|
|
|
// sync &cv operator
|
|
|
|
|
cv[location].t = Expression::Token::CONST;
|
|
|
|
|
cv[location].v.d = o.v.d;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-10-15 10:16:01 +03:00
|
|
|
#endif
|
|
|
|
|
#if LAB2_PREVENT_NON_FOLDED_DIV_BY_ZERO == 1
|
|
|
|
|
if (r.t == Expression::Token::CONST && r.v.d == 0.0 && o.v.o == Expression::Token::DIV)
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Right operand is zero, operation will raise exception during runtime");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
#endif
|
|
|
|
|
int this_op_weight = get_operator_weight(mv, location) + 1;
|
|
|
|
|
|
|
|
|
|
int il = l.t == Expression::Token::CV ? l.v.cv.i : left_location;
|
|
|
|
|
int ir = r.t == Expression::Token::CV ? r.v.cv.i : right_location;
|
|
|
|
|
|
|
|
|
|
// write new operation
|
|
|
|
|
int this_ov_index = ov.size();
|
|
|
|
|
ov.emplace_back(o.v.o,
|
|
|
|
|
l.t == Expression::Token::CV ? Expression::Operation::DERIVED : Expression::Operation::NORMAL,
|
|
|
|
|
il,
|
|
|
|
|
r.t == Expression::Token::CV ? Expression::Operation::DERIVED : Expression::Operation::NORMAL,
|
|
|
|
|
ir,
|
|
|
|
|
this_op_weight);
|
|
|
|
|
|
|
|
|
|
// update mv
|
|
|
|
|
l.t = Expression::Token::NAT;
|
|
|
|
|
r.t = Expression::Token::NAT;
|
|
|
|
|
o.t = Expression::Token::CV;
|
|
|
|
|
o.v.cv.w = this_op_weight;
|
|
|
|
|
o.v.cv.i = this_ov_index;
|
|
|
|
|
#if LAB2_EXCLUDE_COMPTIME_OP_WRITES == 1
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// update mp
|
|
|
|
|
mp.erase(mp.begin() + position, mp.begin() + position + 1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (direction == -1)
|
|
|
|
|
{
|
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].t == Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lv)
|
|
|
|
|
{
|
|
|
|
|
tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA));
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tv[i].v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (direction == 1)
|
|
|
|
|
{
|
|
|
|
|
for (; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].t == Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lv)
|
|
|
|
|
{
|
|
|
|
|
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tv[i].v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv.emplace_back(Expression::Token::CPA);
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Invalid direction given, this is a big bug!");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int add_opa_after_le_io(std::vector<Expression::Token> &tv, const int location)
|
|
|
|
|
{
|
|
|
|
|
int base_rank = rank_op(tv[location].v.o);
|
|
|
|
|
|
|
|
|
|
for (int i = location - 1, lv = 0; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
Expression::Token &t = tv[i];
|
|
|
|
|
|
|
|
|
|
if (t.t == Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
if (t.v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lv <= 0 && (base_rank - rank_op(t.v.o)) >= 0)
|
|
|
|
|
{
|
|
|
|
|
tv.insert(tv.begin() + i + 1, Expression::Token(Expression::Token::OPA));
|
|
|
|
|
return i + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv.insert(tv.begin(), Expression::Token(Expression::Token::OPA));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int add_cpa_before_le_io(std::vector<Expression::Token> &tv, const int location)
|
|
|
|
|
{
|
|
|
|
|
int base_rank = rank_op(tv[location].v.o);
|
|
|
|
|
|
|
|
|
|
for (int i = location + 1, lv = 0, l = tv.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
Expression::Token &t = tv[i];
|
|
|
|
|
|
|
|
|
|
if (t.t == Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
if (t.v.o == Expression::Token::OPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lv <= 0 && (rank_op(t.v.o) - base_rank) <= 0)
|
|
|
|
|
{
|
|
|
|
|
tv.insert(tv.begin() + i, Expression::Token(Expression::Token::CPA));
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (t.v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tv.push_back(Expression::Token(Expression::Token::CPA));
|
|
|
|
|
return tv.size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void create_prios_vector(const std::vector<Expression::Token>& tv, std::vector<int>& pv)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, cp = 0, l = tv.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].t == Expression::Token::OPERATOR)
|
|
|
|
|
{
|
|
|
|
|
switch (tv[i].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
cp++;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
cp--;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::ADD:
|
|
|
|
|
case Expression::Token::SUB:
|
|
|
|
|
case Expression::Token::MUL:
|
|
|
|
|
case Expression::Token::DIV:
|
|
|
|
|
case Expression::Token::POW:
|
|
|
|
|
pv.emplace_back(cp);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Token vector has broken operator at " << i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void find_top_prio_op_range(const std::vector<int>& mp, int& start, int& finish)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, l = mp.size(), prio = -1; i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
if (mp[i] > prio)
|
|
|
|
|
{
|
|
|
|
|
prio = mp[i];
|
|
|
|
|
start = finish = i;
|
|
|
|
|
}
|
|
|
|
|
else if (mp[i] == prio && finish == i - 1)
|
|
|
|
|
{
|
|
|
|
|
finish = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 09:58:31 +03:00
|
|
|
// find operation ranges that are of selected priority (target_prio)
|
|
|
|
|
// accounting for future token convolution into compound values
|
|
|
|
|
static void find_select_prio_op_ranges_after_conv(const std::vector<int>& mp, std::vector<Range>& ranges, int target_prio)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, l = mp.size(), 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 09:03:54 +03:00
|
|
|
static void find_parent_level_range(const std::vector<Expression::Token>& tv, Range &p, const Range &c)
|
|
|
|
|
{
|
|
|
|
|
// left side
|
|
|
|
|
for (int i = get_mop_location(tv, c.start), pc = c.start, lv = 0; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
if (tv[i].is_opa())
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
|
|
|
|
|
if (lv == -2)
|
|
|
|
|
{
|
|
|
|
|
p.start = pc;
|
|
|
|
|
goto right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (tv[i].is_math_op())
|
|
|
|
|
{
|
|
|
|
|
pc--;
|
|
|
|
|
}
|
|
|
|
|
else if (tv[i].is_cpa())
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.start = 0;
|
|
|
|
|
|
|
|
|
|
right:
|
|
|
|
|
// right side
|
|
|
|
|
int i, pc, lv;
|
2026-10-15 16:42:52 +03:00
|
|
|
for (i = get_mop_location(tv, c.finish), pc = c.finish, lv = 0; (size_t) i < tv.size(); i++)
|
2026-10-15 09:03:54 +03:00
|
|
|
{
|
|
|
|
|
if (tv[i].is_cpa())
|
|
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
|
|
|
|
|
if (lv == -2)
|
|
|
|
|
{
|
|
|
|
|
p.finish = pc;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (tv[i].is_math_op())
|
|
|
|
|
{
|
|
|
|
|
pc++;
|
|
|
|
|
}
|
|
|
|
|
else if (tv[i].is_opa())
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.finish = pc - 1;
|
2026-10-14 19:57:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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++)
|
|
|
|
|
{
|
|
|
|
|
int idx = get_mop_location(mv, start + i);
|
|
|
|
|
|
|
|
|
|
switch (mv[idx].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::ADD:
|
|
|
|
|
case Expression::Token::SUB:
|
|
|
|
|
if (lvl == 0)
|
|
|
|
|
inner_top_ops.push_back(i);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::MUL:
|
|
|
|
|
case Expression::Token::DIV:
|
|
|
|
|
if (lvl < 1)
|
|
|
|
|
{
|
|
|
|
|
lvl = 1;
|
|
|
|
|
inner_top_ops.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lvl == 1)
|
|
|
|
|
inner_top_ops.push_back(i);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::POW:
|
|
|
|
|
if (lvl < 2)
|
|
|
|
|
{
|
|
|
|
|
lvl = 2;
|
|
|
|
|
inner_top_ops.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lvl == 2)
|
|
|
|
|
inner_top_ops.push_back(i);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Operator is unrankable");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void weight_operators(std::vector<int>& operator_weights, const std::vector<Expression::Token>& mv, const std::vector<int>& inner_top_ops, const int start)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0, l = inner_top_ops.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
int operator_location = get_mop_location(mv, start + inner_top_ops[i]);
|
|
|
|
|
operator_weights.push_back(get_operator_weight(mv, operator_location));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& tv, const int location)
|
|
|
|
|
{
|
2026-10-14 20:07:28 +03:00
|
|
|
for (int i = location - 1, lv = 0; i >= 0; i--)
|
2026-10-14 19:57:02 +03:00
|
|
|
{
|
2026-10-14 20:07:28 +03:00
|
|
|
const Expression::Token &t = tv[i];
|
|
|
|
|
|
|
|
|
|
if (t.t == Expression::Token::OPERATOR)
|
2026-10-14 19:57:02 +03:00
|
|
|
{
|
2026-10-15 09:33:07 +03:00
|
|
|
if (t.v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
lv++;
|
|
|
|
|
}
|
|
|
|
|
else if (t.v.o == Expression::Token::OPA)
|
2026-10-14 20:07:28 +03:00
|
|
|
{
|
|
|
|
|
lv--;
|
|
|
|
|
|
|
|
|
|
if (lv == -2)
|
|
|
|
|
{
|
|
|
|
|
// enclosed in another pair of brackets, safe to assume ADD
|
|
|
|
|
return Expression::Token::ADD;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-15 09:33:07 +03:00
|
|
|
else if (lv < 0)
|
2026-10-14 20:07:28 +03:00
|
|
|
{
|
|
|
|
|
return t.v.o;
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Expression::Token::ADD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void open_brackets(std::vector<Expression::Token>& tv, const Range &r)
|
|
|
|
|
{
|
2026-10-14 21:17:32 +03:00
|
|
|
Range brackets(
|
|
|
|
|
same_lvl_opa_location(tv, get_mop_location(tv, r.start)),
|
|
|
|
|
same_lvl_cpa_location(tv, get_mop_location(tv, r.finish))
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-14 20:31:10 +03:00
|
|
|
std::vector<int> hl;
|
2026-10-14 21:17:32 +03:00
|
|
|
hl.emplace_back(brackets.start);
|
|
|
|
|
hl.emplace_back(brackets.finish);
|
2026-10-14 20:31:10 +03:00
|
|
|
Parser::print_math_vector(tv, hl);
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
if (Expression::Token::Operator po = get_prior_mop(tv, get_mop_location(tv, r.start)); po == Expression::Token::SUB || po == Expression::Token::DIV)
|
|
|
|
|
{
|
|
|
|
|
for (int i = r.start; i <= r.finish; i++)
|
|
|
|
|
{
|
|
|
|
|
Expression::Token &t = tv[get_mop_location(tv, i)];
|
|
|
|
|
|
|
|
|
|
switch (t.v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::ADD:
|
|
|
|
|
t.v.o = Expression::Token::SUB;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::SUB:
|
|
|
|
|
t.v.o = Expression::Token::ADD;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::MUL:
|
|
|
|
|
t.v.o = Expression::Token::DIV;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::DIV:
|
|
|
|
|
t.v.o = Expression::Token::MUL;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 21:17:32 +03:00
|
|
|
tv.erase(tv.begin() + brackets.finish);
|
|
|
|
|
tv.erase(tv.begin() + brackets.start);
|
2026-10-14 19:57:02 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-15 09:03:54 +03:00
|
|
|
static int get_max_neighbour_rank(const std::vector<Expression::Token>& tv, const Range &r)
|
|
|
|
|
{
|
|
|
|
|
int rank = -1;
|
|
|
|
|
|
|
|
|
|
int left_bracket = same_lvl_opa_location(tv, get_mop_location(tv, r.start));
|
|
|
|
|
if (left_bracket - 1 >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (int ro = rank_op(tv[left_bracket - 1].v.o); ro > rank)
|
|
|
|
|
{
|
|
|
|
|
rank = ro;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int right_bracket = same_lvl_cpa_location(tv, get_mop_location(tv, r.finish));
|
2026-10-15 16:42:52 +03:00
|
|
|
if ((size_t) (right_bracket + 1) < tv.size())
|
2026-10-15 09:03:54 +03:00
|
|
|
{
|
|
|
|
|
if (int ro = rank_op(tv[right_bracket + 1].v.o); ro > rank)
|
|
|
|
|
{
|
|
|
|
|
rank = ro;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rank;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static void remove_extra_brackets(std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
2026-10-14 20:31:10 +03:00
|
|
|
pr_debug("Processing: removing extra brackets");
|
2026-10-14 19:57:02 +03:00
|
|
|
std::vector<int> mp;
|
|
|
|
|
create_prios_vector(tv, mp);
|
|
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
if (mp.size() == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
// for everything excluding level 0
|
|
|
|
|
for (int current_prio = mp[get_max_index(mp)]; current_prio > 0; current_prio--)
|
|
|
|
|
{
|
|
|
|
|
mp.clear();
|
|
|
|
|
create_prios_vector(tv, mp);
|
|
|
|
|
|
|
|
|
|
std::vector<Range> ranges;
|
2026-10-15 09:58:31 +03:00
|
|
|
find_select_prio_op_ranges_after_conv(mp, ranges, current_prio);
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
for (int r_no = ranges.size() - 1; r_no >= 0; r_no--)
|
|
|
|
|
{
|
|
|
|
|
mp.clear();
|
|
|
|
|
create_prios_vector(tv, mp);
|
|
|
|
|
|
|
|
|
|
ranges.clear();
|
2026-10-15 09:58:31 +03:00
|
|
|
find_select_prio_op_ranges_after_conv(mp, ranges, current_prio);
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
const Range &r = ranges[r_no];
|
|
|
|
|
|
|
|
|
|
{
|
2026-10-15 09:03:54 +03:00
|
|
|
// extra logging
|
|
|
|
|
pr_debug("Considering option:");
|
|
|
|
|
std::vector<int> hl;
|
|
|
|
|
hl.emplace_back(get_mop_location(tv, r.start));
|
|
|
|
|
hl.emplace_back(get_mop_location(tv, r.finish));
|
|
|
|
|
Parser::print_math_vector(tv, hl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Range parent_r;
|
|
|
|
|
find_parent_level_range(tv, parent_r, r);
|
2026-10-14 19:57:02 +03:00
|
|
|
|
2026-10-15 09:03:54 +03:00
|
|
|
int min_child_rank = min_rank(tv, mp, r, current_prio);
|
|
|
|
|
if (int nr = get_max_neighbour_rank(tv, r); nr > min_child_rank)
|
2026-10-14 19:57:02 +03:00
|
|
|
{
|
2026-10-15 09:03:54 +03:00
|
|
|
pr_debug("Fails in neighbour rank: max_neighbour=" << nr << " > min_child=" << min_child_rank);
|
2026-10-14 19:57:02 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 20:44:20 +03:00
|
|
|
// check specifically if the opening undoes applied fixes
|
|
|
|
|
if (Expression::Token::is_negative(get_prior_mop(tv, get_mop_location(tv, r.start))))
|
|
|
|
|
{
|
2026-10-15 09:03:54 +03:00
|
|
|
pr_debug("Fails in prior mop is_negative");
|
2026-10-14 20:44:20 +03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
open_brackets(tv, r);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void process_remove_empty_brackets(std::vector<Expression::Token>& tv)
|
2026-10-14 12:00:41 +03:00
|
|
|
{
|
|
|
|
|
pr_debug("Pre-process: remove empty brackets");
|
|
|
|
|
|
2026-10-14 21:18:27 +03:00
|
|
|
int fix = -1;
|
|
|
|
|
while (fix) {
|
|
|
|
|
fix = 0;
|
|
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
for (int i = 0; i < (long) tv.size() - 1; i++)
|
2026-10-14 21:18:27 +03:00
|
|
|
{
|
|
|
|
|
if (tv[i].t == Expression::Token::OPERATOR
|
|
|
|
|
&& tv[i].v.o == Expression::Token::OPA
|
|
|
|
|
&& tv[i+1].t == Expression::Token::OPERATOR
|
|
|
|
|
&& tv[i+1].v.o == Expression::Token::CPA)
|
|
|
|
|
{
|
|
|
|
|
std::vector<int> highlight;
|
|
|
|
|
highlight.emplace_back(i);
|
|
|
|
|
highlight.emplace_back(i+1);
|
|
|
|
|
Parser::print_math_vector(tv, highlight);
|
|
|
|
|
|
|
|
|
|
tv.erase(tv.begin() + i, tv.begin() + i + 2);
|
|
|
|
|
i--;
|
|
|
|
|
|
|
|
|
|
fix++;
|
|
|
|
|
}
|
2026-10-14 12:00:41 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
}
|
2026-10-14 12:00:41 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static int check_bracket_match(const std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
|
|
|
|
int problem_cnt = 0;
|
|
|
|
|
std::stack<int> s;
|
|
|
|
|
|
|
|
|
|
for (int i = 0, l = tv.size(); i < l; i++)
|
2026-10-14 12:00:41 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
if (tv[i].t == Expression::Token::OPERATOR)
|
2026-10-14 12:00:41 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
switch (tv[i].v.o)
|
2026-10-14 12:00:41 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
s.push(i);
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
if (s.size())
|
|
|
|
|
{
|
|
|
|
|
s.pop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Missing opening bracket at token " << i);
|
|
|
|
|
problem_cnt++;
|
|
|
|
|
highlight_issue(tv, i);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2026-10-14 12:00:41 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
for (int i = 0, l = s.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
problem_cnt++;
|
|
|
|
|
pr_warn("Opening bracket at token " << s.top() << " expected a closing bracket, but none found");
|
|
|
|
|
highlight_issue(tv, s.top());
|
|
|
|
|
s.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return problem_cnt;
|
2026-10-14 12:00:41 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static int check_start_finish_validity(const std::vector<Expression::Token>& tv)
|
2026-10-06 21:02:40 +03:00
|
|
|
{
|
|
|
|
|
int problem_cnt = 0;
|
|
|
|
|
|
2026-10-07 18:11:39 +03:00
|
|
|
if (tv.size())
|
2026-10-07 16:15:42 +03:00
|
|
|
{
|
|
|
|
|
int pc = problem_cnt;
|
|
|
|
|
|
|
|
|
|
switch (tv[0].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[0].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
pr_warn("Expected value or opening bracket at token 0, got closing bracket");
|
|
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Expected value or opening bracket at token 0, got operator");
|
|
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pc != problem_cnt)
|
|
|
|
|
{
|
|
|
|
|
highlight_issue(tv, 0);
|
|
|
|
|
pc = problem_cnt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (tv.back().t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv.back().v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPA:
|
2026-10-07 17:35:04 +03:00
|
|
|
pr_warn("Expected value or closing bracket at token " << tv.size() - 1 << ", got opening bracket");
|
2026-10-07 16:15:42 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-10-07 17:35:04 +03:00
|
|
|
pr_warn("Expected value or closing bracket at token " << tv.size() - 1 << ", got operator");
|
2026-10-07 16:15:42 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pc != problem_cnt)
|
|
|
|
|
{
|
|
|
|
|
highlight_issue(tv, tv.size()-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
return problem_cnt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int check_token_type_sequence(const std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
|
|
|
|
int problem_cnt = 0;
|
|
|
|
|
|
2026-10-07 18:11:39 +03:00
|
|
|
if (tv.size() > 1)
|
2026-10-06 21:02:40 +03:00
|
|
|
{
|
|
|
|
|
for (int i = 0, l = tv.size() - 1, pc; pc = problem_cnt, i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
switch (tv[i].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
switch (tv[i+1].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[i+1].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPA:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got opening bracket");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::VAR:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got variable");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CONST:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got constant");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[i].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
switch (tv[i+1].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[i+1].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected value at token " << i + 1 << ", got closing bracket");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected value at token " << i + 1 << ", got operator");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
switch (tv[i+1].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[i+1].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::CPA:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPA:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got opening bracket");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CONST:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got constant");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::VAR:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected operator at token " << i + 1 << ", got variable");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
switch (tv[i+1].t)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::OPERATOR:
|
|
|
|
|
switch (tv[i+1].v.o)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Token::CPA:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected value at token " << i + 1 << ", got closing bracket");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::OPA:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2026-10-07 16:15:42 +03:00
|
|
|
pr_warn("Expected value at token " << i + 1 << ", got operator");
|
2026-10-06 21:02:40 +03:00
|
|
|
problem_cnt++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::CONST:
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Token::VAR:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Impossible token type, this is a bug!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pc != problem_cnt)
|
|
|
|
|
{
|
|
|
|
|
highlight_issue(tv, i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return problem_cnt;
|
|
|
|
|
}
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static void process_unary_minus(std::vector<Expression::Token>& tv)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
pr_debug("Pre-process: unary minus");
|
2026-10-05 11:17:48 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
int change = 1;
|
2026-10-15 16:42:52 +03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
while (change)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
change = 0;
|
|
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
for (int i = 0; i < (long) tv.size() - 2; i++)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
if (tv[i].t == Expression::Token::OPERATOR
|
|
|
|
|
&& tv[i].v.o != Expression::Token::CPA
|
|
|
|
|
&& tv[i+1].t == Expression::Token::OPERATOR
|
|
|
|
|
&& tv[i+1].v.o == Expression::Token::SUB
|
|
|
|
|
&& (tv[i+2].t == Expression::Token::CONST
|
|
|
|
|
|| tv[i+2].t == Expression::Token::VAR
|
|
|
|
|
|| (tv[i+2].t == Expression::Token::OPERATOR
|
|
|
|
|
&& tv[i+2].v.o == Expression::Token::OPA)))
|
2026-10-05 20:24:09 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
std::vector<int> highlight;
|
2026-10-12 21:29:02 +03:00
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
tv.insert(tv.begin() + i + 1, 0.0);
|
|
|
|
|
tv.insert(tv.begin() + i + 1, Expression::Token::OPA);
|
2026-10-14 19:57:02 +03:00
|
|
|
highlight.emplace_back(add_bracket_to(tv, i + 3, 1));
|
2026-10-12 21:29:02 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
highlight.emplace(highlight.begin(), i+2);
|
|
|
|
|
highlight.emplace(highlight.begin(), i+1);
|
|
|
|
|
Parser::print_math_vector(tv, highlight);
|
2026-10-05 20:24:09 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
i += 4;
|
2026-10-12 21:29:02 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
change++;
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
// rl - relative level (outer priority)
|
|
|
|
|
// ip - inner priority
|
|
|
|
|
static int correct_operation_priorities(std::vector<Expression::Token>& tv, int op, int ip)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
int corrections = 0;
|
|
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
for (int i = 0, same_lvl_op_cnt = -1, relative_level = -op; (size_t) i < tv.size(); i++)
|
2026-10-04 14:13:14 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
Expression::Token& tok = tv[i];
|
|
|
|
|
|
|
|
|
|
if (tok.t == Expression::Token::OPERATOR)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
if (tok.v.o == Expression::Token::OPA)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
relative_level++;
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
else if (tok.v.o == Expression::Token::CPA)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
relative_level--;
|
|
|
|
|
|
|
|
|
|
if (relative_level < 0)
|
|
|
|
|
{
|
|
|
|
|
same_lvl_op_cnt = -1;
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
else if (relative_level == 0)
|
|
|
|
|
{
|
|
|
|
|
int target_op;
|
|
|
|
|
switch (ip)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
target_op = tok.v.o == Expression::Token::SUB;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
target_op = tok.v.o == Expression::Token::DIV;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
|
2026-10-14 21:49:59 +03:00
|
|
|
if (rank_op(tok.v.o) == ip && target_op)
|
2026-10-14 19:57:02 +03:00
|
|
|
{
|
|
|
|
|
same_lvl_op_cnt++;
|
|
|
|
|
|
2026-10-14 21:49:59 +03:00
|
|
|
if (same_lvl_op_cnt >= 1)
|
2026-10-14 19:57:02 +03:00
|
|
|
{
|
|
|
|
|
same_lvl_op_cnt = 0;
|
|
|
|
|
corrections++;
|
|
|
|
|
|
|
|
|
|
switch (ip)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
tok.v.o = Expression::Token::ADD;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
tok.v.o = Expression::Token::MUL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// inverse order to keep location valid between calls
|
|
|
|
|
std::vector<int> highlight_locations;
|
|
|
|
|
highlight_locations.emplace_back(i+1);
|
|
|
|
|
highlight_locations.emplace_back(add_cpa_before_le_io(tv, i) + 1);
|
|
|
|
|
highlight_locations.emplace(highlight_locations.begin(), add_opa_after_le_io(tv, i));
|
|
|
|
|
Parser::print_math_vector(tv, highlight_locations);
|
|
|
|
|
|
|
|
|
|
// advance one step to compensate for inserted left bracket
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
// elevate one level to compensate for new brackets
|
|
|
|
|
relative_level++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 21:49:59 +03:00
|
|
|
else if (rank_op(tok.v.o) < ip)
|
2026-10-14 21:45:17 +03:00
|
|
|
{
|
2026-10-14 21:49:59 +03:00
|
|
|
same_lvl_op_cnt = -1;
|
2026-10-14 21:45:17 +03:00
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
return corrections;
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
static int post_process(std::vector<Expression::Token>& tv)
|
2026-10-05 11:17:48 +03:00
|
|
|
{
|
2026-10-12 23:55:16 +03:00
|
|
|
pr_debug("Post processing:");
|
2026-10-05 20:24:09 +03:00
|
|
|
Parser::print_math_vector(tv);
|
2026-10-05 11:17:48 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
int total_corrections = 0;
|
|
|
|
|
|
2026-10-05 20:24:09 +03:00
|
|
|
// global prio evaluation
|
|
|
|
|
std::vector<int> mp;
|
|
|
|
|
create_prios_vector(tv, mp);
|
|
|
|
|
|
2026-10-06 21:02:40 +03:00
|
|
|
if (mp.size() == 0)
|
|
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
return total_corrections;
|
2026-10-06 21:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 20:24:09 +03:00
|
|
|
for (int current_prio_level = mp[get_max_index(mp)]; current_prio_level >= 0; current_prio_level--)
|
|
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
int corrections = -1;
|
2026-10-05 20:24:09 +03:00
|
|
|
|
2026-10-12 21:29:02 +03:00
|
|
|
while (corrections)
|
2026-10-05 20:24:09 +03:00
|
|
|
{
|
2026-10-14 19:57:02 +03:00
|
|
|
total_corrections += corrections = correct_operation_priorities(tv, current_prio_level, 1);
|
|
|
|
|
}
|
2026-10-05 20:24:09 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
corrections = -1;
|
2026-10-05 20:24:09 +03:00
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
while (corrections)
|
|
|
|
|
{
|
|
|
|
|
total_corrections += corrections = correct_operation_priorities(tv, current_prio_level, 0);
|
2026-10-05 20:24:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
return total_corrections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int prepare_token_vector(std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
|
|
|
|
process_remove_empty_brackets(tv);
|
|
|
|
|
|
|
|
|
|
if (check_bracket_match(tv))
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process_unary_minus(tv);
|
|
|
|
|
|
|
|
|
|
if (check_start_finish_validity(tv))
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (check_token_type_sequence(tv))
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-15 16:42:52 +03:00
|
|
|
remove_extra_brackets(tv);
|
|
|
|
|
(void) post_process(tv);
|
|
|
|
|
remove_extra_brackets(tv);
|
2026-10-14 19:57:02 +03:00
|
|
|
|
|
|
|
|
return 0;
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Expression::Operation> build_op_vector(const std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
|
|
|
|
// define operator precedency
|
|
|
|
|
std::vector<int> g_prios;
|
|
|
|
|
create_prios_vector(tv, g_prios);
|
|
|
|
|
|
2026-10-14 15:16:08 +03:00
|
|
|
pr_debug("Building operation vector");
|
|
|
|
|
|
|
|
|
|
std::vector<Expression::Token> cv = tv; // Constant sync Vector
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-05 11:17:48 +03:00
|
|
|
// build operation vector
|
|
|
|
|
std::vector<Expression::Operation> ov;
|
2026-10-04 14:13:14 +03:00
|
|
|
{
|
2026-10-14 15:16:08 +03:00
|
|
|
std::vector<Expression::Token> mv = tv; // Modifiable Vector
|
2026-10-05 11:17:48 +03:00
|
|
|
std::vector<int> mp = g_prios;
|
|
|
|
|
|
|
|
|
|
while (mp.size())
|
|
|
|
|
{
|
|
|
|
|
int start, finish;
|
|
|
|
|
find_top_prio_op_range(mp, start, finish);
|
|
|
|
|
|
|
|
|
|
// --- process current range ---
|
|
|
|
|
while (start != finish)
|
|
|
|
|
{
|
|
|
|
|
std::vector<int> inner_top_ops;
|
|
|
|
|
rank_inner_ops(inner_top_ops, mv, start, finish);
|
|
|
|
|
|
|
|
|
|
std::vector<int> operator_weights;
|
|
|
|
|
weight_operators(operator_weights, mv, inner_top_ops, start);
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
if (write_operation(ov, cv, mv, mp, start + inner_top_ops[get_min_index(operator_weights)]))
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Stopping");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
finish--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process final operation w/out running selection algorithm
|
2026-10-14 19:57:02 +03:00
|
|
|
if (write_operation(ov, cv, mv, mp, start))
|
|
|
|
|
{
|
|
|
|
|
pr_warn("Stopping");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
2026-10-14 15:16:08 +03:00
|
|
|
|
|
|
|
|
pr_debug("Final value:");
|
|
|
|
|
Parser::print_math_vector(mv);
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-14 15:16:08 +03:00
|
|
|
pr_debug("Resulting operation vector:");
|
|
|
|
|
|
2026-10-05 11:17:48 +03:00
|
|
|
for (int i = 0, l = ov.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
const Expression::Operation &o = ov[i];
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-07 16:15:42 +03:00
|
|
|
prs_debug("OP (w=" << o.w << "): ");
|
2026-10-05 11:17:48 +03:00
|
|
|
|
|
|
|
|
if (o.tl == Expression::Operation::NORMAL)
|
|
|
|
|
{
|
2026-10-14 15:16:08 +03:00
|
|
|
const Expression::Token &real_value = cv[o.il];
|
|
|
|
|
pri_debug(*real_value.to_math_str());
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug("t" << o.il);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pri_debug(" " << o.o.op_math_str() << " ");
|
|
|
|
|
|
|
|
|
|
if (o.tr == Expression::Operation::NORMAL)
|
|
|
|
|
{
|
2026-10-14 15:16:08 +03:00
|
|
|
const Expression::Token &real_value = cv[o.ir];
|
|
|
|
|
pri_debug(*real_value.to_math_str());
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug("t" << o.ir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pri_debug(" = t" << i << "\n");
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-14 19:57:02 +03:00
|
|
|
finish:
|
2026-10-05 11:17:48 +03:00
|
|
|
return ov;
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
|
2026-10-07 16:15:42 +03:00
|
|
|
Expression::TreeNode ov_to_tree(const std::vector<Expression::Token> tv, const std::vector<Expression::Operation>& ov)
|
|
|
|
|
{
|
|
|
|
|
Expression::TreeNode root;
|
|
|
|
|
std::queue<std::pair<Expression::TreeNode *, int>> queued_ops;
|
2026-10-07 18:11:39 +03:00
|
|
|
|
|
|
|
|
if (ov.size())
|
|
|
|
|
{
|
|
|
|
|
queued_ops.emplace(&root, ov.size() - 1);
|
|
|
|
|
}
|
2026-10-07 16:15:42 +03:00
|
|
|
|
|
|
|
|
while (queued_ops.size())
|
|
|
|
|
{
|
|
|
|
|
const std::pair<Expression::TreeNode *, int> item = queued_ops.front();
|
|
|
|
|
queued_ops.pop();
|
|
|
|
|
|
|
|
|
|
const Expression::Operation &proc_op = ov[item.second];
|
|
|
|
|
Expression::TreeNode &node = *item.first;
|
|
|
|
|
|
|
|
|
|
node.o = proc_op.o;
|
|
|
|
|
|
|
|
|
|
switch (proc_op.tl)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Operation::DERIVED:
|
|
|
|
|
queued_ops.emplace(node.l = new Expression::TreeNode(), proc_op.il);
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Operation::NORMAL:
|
|
|
|
|
node.l = new Expression::TreeNode(tv[proc_op.il]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (proc_op.tr)
|
|
|
|
|
{
|
|
|
|
|
case Expression::Operation::DERIVED:
|
|
|
|
|
queued_ops.emplace(node.r = new Expression::TreeNode(), proc_op.ir);
|
|
|
|
|
break;
|
|
|
|
|
case Expression::Operation::NORMAL:
|
|
|
|
|
node.r = new Expression::TreeNode(tv[proc_op.ir]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_op_tree(const Expression::TreeNode& root)
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::pair<Expression::TreeNode *, int>> items_to_display;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|