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

This commit is contained in:
Brad Arant committed 2025-11-25 13:16:26 -08:00
1 parent 61216a2296
commit a91d78f524
4 files changed
+36 -6

No files matched your search

+17 -5
View File
@@ -9,7 +9,7 @@
namespace jet { namespace jet {
Operand::Operand(coreutils::ZString &in, Tag &tag) { Operand::Operand(coreutils::ZString &in, Tag &tag, bool stop) : in(in), tag(tag) {
doubleValue = 0; doubleValue = 0;
@@ -378,7 +378,15 @@ namespace jet {
throw coreutils::Exception("operand is not valid."); throw coreutils::Exception("operand is not valid.");
in.skipWhitespace(); in.skipWhitespace();
if(!stop)
parseOperator();
return;
}
int Operand::parseOperator() {
if(in.ifNext("!=") || in.ifNext("<>")) { if(in.ifNext("!=") || in.ifNext("<>")) {
Operand op(in, tag); Operand op(in, tag);
if(isNumber && op.isNumber) { if(isNumber && op.isNumber) {
@@ -523,6 +531,8 @@ namespace jet {
} }
} }
} }
if(in.ifNext("+")) { if(in.ifNext("+")) {
if(isNumber) { if(isNumber) {
Operand op(in, tag); Operand op(in, tag);
@@ -547,28 +557,30 @@ namespace jet {
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("*")) { } else if(in.ifNext("*")) {
if(isNumber) { if(isNumber) {
Operand op(in, tag); Operand op(in, tag, true);
if(op.isNumber) { if(op.isNumber) {
doubleValue *= op.doubleValue; doubleValue *= op.doubleValue;
string = std::format("{:.12f}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros(); string.removeTrailingZeros();
parseOperator();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("/")) { } else if(in.ifNext("/")) {
if(isNumber) { if(isNumber) {
Operand op(in, tag); Operand op(in, tag, true);
if(op.isNumber) { if(op.isNumber) {
doubleValue /= op.doubleValue; doubleValue /= op.doubleValue;
string = std::format("{:.12f}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros(); string.removeTrailingZeros();
parseOperator();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else }
return; return true;
} }
} }
+6 -1
View File
@@ -10,8 +10,11 @@ namespace jet {
class Operand { class Operand {
public: public:
Operand(coreutils::ZString &in, Tag &tag); Operand(coreutils::ZString &in, Tag &tag, bool stop = false);
coreutils::ZString &in;
Tag &tag;
bool isNumber; bool isNumber;
/// ///
@@ -23,6 +26,8 @@ namespace jet {
coreutils::MString string = ""; coreutils::MString string = "";
double doubleValue; double doubleValue;
int parseOperator();
}; };
+5
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>
+8
View File
@@ -12,4 +12,12 @@ $[righty]=56789
$[trim]=this is a test $[trim]=this is a test
<set name="integer" expr="integer(12430.54356546)" /> <set name="integer" expr="integer(12430.54356546)" />
$[integer]=12430 $[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> </jet>