Fixed * and / operators to prioritize over + and -.

This commit is contained in:
Brad Arant 2025-11-25 13:16:26 -08:00
parent 61216a2296
commit a91d78f524
4 changed files with 36 additions and 6 deletions

View File

@ -9,7 +9,7 @@
namespace jet {
Operand::Operand(coreutils::ZString &in, Tag &tag) {
Operand::Operand(coreutils::ZString &in, Tag &tag, bool stop) : in(in), tag(tag) {
doubleValue = 0;
@ -378,7 +378,15 @@ namespace jet {
throw coreutils::Exception("operand is not valid.");
in.skipWhitespace();
if(!stop)
parseOperator();
return;
}
int Operand::parseOperator() {
if(in.ifNext("!=") || in.ifNext("<>")) {
Operand op(in, tag);
if(isNumber && op.isNumber) {
@ -523,6 +531,8 @@ namespace jet {
}
}
}
if(in.ifNext("+")) {
if(isNumber) {
Operand op(in, tag);
@ -547,28 +557,30 @@ namespace jet {
throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("*")) {
if(isNumber) {
Operand op(in, tag);
Operand op(in, tag, true);
if(op.isNumber) {
doubleValue *= op.doubleValue;
string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
parseOperator();
} else
throw coreutils::Exception("operand is not a number.");
} else
throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("/")) {
if(isNumber) {
Operand op(in, tag);
Operand op(in, tag, true);
if(op.isNumber) {
doubleValue /= op.doubleValue;
string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
parseOperator();
} else
throw coreutils::Exception("operand is not a number.");
} else
throw coreutils::Exception("operand is not a number.");
} else
return;
}
return true;
}
}

View File

@ -10,8 +10,11 @@ namespace jet {
class Operand {
public:
Operand(coreutils::ZString &in, Tag &tag);
Operand(coreutils::ZString &in, Tag &tag, bool stop = false);
coreutils::ZString &in;
Tag &tag;
bool isNumber;
///
@ -23,6 +26,8 @@ namespace jet {
coreutils::MString string = "";
double doubleValue;
int parseOperator();
};

5
tests/debug.jet Executable file
View File

@ -0,0 +1,5 @@
#!../jet-2.0
<jet name1="localname" filterblanklines="true" trimlines="true">
<set name="result" expr="2 + 2 * 3 + 2" />
$[result]=10
</jet>

View File

@ -12,4 +12,12 @@ $[righty]=56789
$[trim]=this is a test
<set name="integer" expr="integer(12430.54356546)" />
$[integer]=12430
<set name="result" expr="5 + 5 * 5" />
$[result]=30
<set name="result" expr="5 + 5 * 5 + 5" />
$[result]=35
<set name="result" expr="(5 * 2) + (5 * 5) + (5 * 2)" />
$[result]=45
<set name="result" expr="2 + 2 * 3 + 2" />
$[result]=10
</jet>