2026-10-04 14:13:14 +03:00
|
|
|
#include "syntaxer.hpp"
|
|
|
|
|
#include "expression.hpp"
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "print.h"
|
|
|
|
|
|
|
|
|
|
namespace Syntax {
|
|
|
|
|
|
2026-10-05 11:17:48 +03:00
|
|
|
#if 0
|
2026-10-04 14:13:14 +03:00
|
|
|
struct OpImportance
|
|
|
|
|
{
|
2026-10-05 11:17:48 +03:00
|
|
|
//int pos;
|
2026-10-04 14:13:14 +03:00
|
|
|
int prio;
|
|
|
|
|
};
|
2026-10-05 11:17:48 +03:00
|
|
|
#endif
|
2026-10-04 14:13:14 +03:00
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
std::vector<SyntaxIssue> check_syntax(std::vector<Expression::Token>& tv);
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2026-10-05 11:17:48 +03:00
|
|
|
// get mop = Math OPerator index
|
|
|
|
|
static int get_mop_location(const std::vector<Expression::Token>& v, int position)
|
|
|
|
|
{
|
|
|
|
|
int i = 0, l = v.size(), p = -1;
|
|
|
|
|
for (; p += v[i].t == Expression::Token::OPERATOR
|
|
|
|
|
&& 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(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 void write_operation(
|
|
|
|
|
std::vector<Expression::Operation>& ov,
|
|
|
|
|
const std::vector<Expression::Token>& tv,
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
Expression::Token &o = mv[location],
|
|
|
|
|
&l = mv[left_location],
|
|
|
|
|
&r = mv[right_location];
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// update mp
|
|
|
|
|
mp.erase(mp.begin() + position, mp.begin() + position + 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++)
|
2026-10-04 14:13:14 +03:00
|
|
|
{
|
|
|
|
|
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:
|
2026-10-05 11:17:48 +03:00
|
|
|
pv.emplace_back(cp);
|
2026-10-04 14:13:14 +03:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pr_warn("Token vector has broken operator at " << i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-05 11:17:48 +03:00
|
|
|
}
|
2026-10-04 14:13:14 +03:00
|
|
|
|
2026-10-05 11:17:48 +03:00
|
|
|
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++)
|
2026-10-04 14:13:14 +03:00
|
|
|
{
|
2026-10-05 11:17:48 +03:00
|
|
|
if (mp[i] > prio)
|
|
|
|
|
{
|
|
|
|
|
prio = mp[i];
|
|
|
|
|
start = finish = i;
|
|
|
|
|
}
|
|
|
|
|
else if (mp[i] == prio && finish == i - 1)
|
|
|
|
|
{
|
|
|
|
|
finish = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void apply_corrections(std::vector<Expression::Token>& tv)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
for (int i = 0, l = g_prios.size(); i < l; i++)
|
|
|
|
|
{
|
|
|
|
|
pr_debug("prio=" << g_prios[i]);
|
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
|
|
|
{
|
|
|
|
|
std::vector<Expression::Token> mv = tv;
|
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);
|
|
|
|
|
|
|
|
|
|
write_operation(ov, tv, mv, mp, start + inner_top_ops[get_min(operator_weights)]);
|
|
|
|
|
finish--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// process final operation w/out running selection algorithm
|
|
|
|
|
write_operation(ov, tv, mv, mp, start);
|
|
|
|
|
}
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
|
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-05 11:17:48 +03:00
|
|
|
prs_debug("OP: ");
|
|
|
|
|
|
|
|
|
|
if (o.tl == Expression::Operation::NORMAL)
|
|
|
|
|
{
|
|
|
|
|
const Expression::Token &real_value = tv[o.il];
|
|
|
|
|
|
|
|
|
|
if (real_value.t == Expression::Token::VAR)
|
|
|
|
|
{
|
|
|
|
|
pri_debug(*real_value.v.s);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug(real_value.v.d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug("t" << o.il);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pri_debug(" " << o.o.op_math_str() << " ");
|
|
|
|
|
|
|
|
|
|
if (o.tr == Expression::Operation::NORMAL)
|
|
|
|
|
{
|
|
|
|
|
const Expression::Token &real_value = tv[o.ir];
|
|
|
|
|
|
|
|
|
|
if (real_value.t == Expression::Token::VAR)
|
|
|
|
|
{
|
|
|
|
|
pri_debug(*real_value.v.s);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug(real_value.v.d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pri_debug("t" << o.ir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pri_debug(" = t" << i << "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ov;
|
2026-10-04 14:13:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|