fix infitite search for prior operator when opening brackets

This commit is contained in:
2026-10-14 20:07:28 +03:00
parent 84d28d3845
commit 0d39290709
+19 -6
View File
@@ -2,10 +2,10 @@
#include "parser.hpp"
#include "print.h"
#include "syntaxer.hpp"
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
namespace Syntax {
@@ -606,13 +606,26 @@ static int same_inner_level_ops(const std::vector<Expression::Token>& tv, const
static Expression::Token::Operator get_prior_mop(const std::vector<Expression::Token>& tv, const int location)
{
for (int i = location - 1; i >= 0; i--)
for (int i = location - 1, lv = 0; i >= 0; i--)
{
if (tv[i].t == Expression::Token::OPERATOR
&& tv[i].v.o != Expression::Token::OPA
&& tv[i].v.o != Expression::Token::CPA)
const Expression::Token &t = tv[i];
if (t.t == Expression::Token::OPERATOR)
{
return tv[i].v.o;
if (t.v.o == Expression::Token::OPA)
{
lv--;
if (lv == -2)
{
// enclosed in another pair of brackets, safe to assume ADD
return Expression::Token::ADD;
}
}
else
{
return t.v.o;
}
}
}