Heavy work on while tag. Still needs work.

This commit is contained in:
Brad Arant 2024-11-04 16:48:14 -08:00
parent b08d2b59eb
commit ed00a330a4
5 changed files with 46 additions and 25 deletions

View File

@ -70,7 +70,8 @@ namespace jet {
unsigned int seed = (unsigned int)clock(); unsigned int seed = (unsigned int)clock();
doubleValue = (double) rand_r(&seed) / (RAND_MAX + 1.0); doubleValue = (double) rand_r(&seed) / (RAND_MAX + 1.0);
isNumber = true; isNumber = true;
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
} else if(in.ifNext("true")) { } else if(in.ifNext("true")) {
boolean = true; boolean = true;
string = "true"; string = "true";
@ -79,7 +80,7 @@ namespace jet {
string = "false"; string = "false";
} else if(in.startsWithNumber()) { } else if(in.startsWithNumber()) {
doubleValue = in.asDouble(); doubleValue = in.asDouble();
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
isNumber = true; isNumber = true;
} else if(in.ifNext("'")) { } else if(in.ifNext("'")) {
string = in.getTokenExclude("'"); string = in.getTokenExclude("'");
@ -116,6 +117,7 @@ namespace jet {
if(in.ifNext("<")) { if(in.ifNext("<")) {
Operand op(in); Operand op(in);
if(isNumber && op.isNumber) { if(isNumber && op.isNumber) {
std::cout << doubleValue << " < " << op.doubleValue << std::endl;
if(doubleValue < op.doubleValue) { if(doubleValue < op.doubleValue) {
boolean = true; boolean = true;
isNumber = false; isNumber = false;
@ -238,7 +240,8 @@ namespace jet {
Operand op(in); Operand op(in);
if(op.isNumber) { if(op.isNumber) {
doubleValue += op.doubleValue; doubleValue += op.doubleValue;
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else
@ -248,7 +251,8 @@ namespace jet {
Operand op(in); Operand op(in);
if(op.isNumber) { if(op.isNumber) {
doubleValue -= op.doubleValue; doubleValue -= op.doubleValue;
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else
@ -258,7 +262,8 @@ namespace jet {
Operand op(in); Operand op(in);
if(op.isNumber) { if(op.isNumber) {
doubleValue *= op.doubleValue; doubleValue *= op.doubleValue;
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else
@ -268,7 +273,8 @@ namespace jet {
Operand op(in); Operand op(in);
if(op.isNumber) { if(op.isNumber) {
doubleValue /= op.doubleValue; doubleValue /= op.doubleValue;
string = std::format("{}", doubleValue); string = std::format("{:.12f}", doubleValue);
string.removeTrailingZeros();
} else } else
throw coreutils::Exception("operand is not a number."); throw coreutils::Exception("operand is not a number.");
} else } else

View File

@ -34,7 +34,6 @@ namespace jet {
if(in.ifNext("<")) { if(in.ifNext("<")) {
name = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!"); name = in.getTokenInclude("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!");
if(in.startsWith(" ") || in.startsWith("/") || in.startsWith(">")) { if(in.startsWith(" ") || in.startsWith("/") || in.startsWith(">")) {
coreutils::Log(coreutils::LOG_DEBUG_1) << "Processing tag: " << name;
bool finished = false; bool finished = false;
while(!finished) { while(!finished) {
in.skipWhitespace(); in.skipWhitespace();

View File

@ -9,24 +9,17 @@ namespace jet {
coreutils::MString result; coreutils::MString result;
bool booleanResult = false; bool booleanResult = false;
bool exprMethod = false;
if(variableDefined("value1")) { if(variableDefined("value1")) {
if(variableDefined("expr")) if(variableDefined("expr"))
throw coreutils::Exception("Either value1 or expr can be specified but not both."); throw coreutils::Exception("either value1 or expr can be specified but not both.");
if(variableDefined("value2")) { if(!variableDefined("value2"))
if(variableDefined("type")) {
}
else
throw coreutils::Exception("type expected if value1 and value2 specified.");
}
else
throw coreutils::Exception("value2 required if value1 specified."); throw coreutils::Exception("value2 required if value1 specified.");
if(!variableDefined("type")) if(!variableDefined("type"))
throw coreutils::Exception("type required for a value1 and value2 comparison."); throw coreutils::Exception("type expected if value1 and value2 specified.");
int rc = variables["value1"].compare(variables["value2"]); int rc = variables["value1"].compare(variables["value2"]);
if(((variables["type"] == "eq") && (rc == 0)) || if(((variables["type"] == "eq") && (rc == 0)) ||
((variables["type"] == "ne") && (rc != 0)) || ((variables["type"] == "ne") && (rc != 0)) ||
@ -39,17 +32,31 @@ namespace jet {
throw coreutils::Exception("type value must be 'eq','ne','lt','le','gt','ge'."); throw coreutils::Exception("type value must be 'eq','ne','lt','le','gt','ge'.");
} }
else if(variableDefined("expr")) { else if(variableDefined("expr")) {
if(variableDefined("value2")) if(variableDefined("value2"))
throw coreutils::Exception("value2 should not be specified with expr."); throw coreutils::Exception("value2 should not be specified with expr.");
if(variableDefined("type")) if(variableDefined("type"))
throw coreutils::Exception("type should not be specified with expr."); throw coreutils::Exception("type should not be specified with expr.");
exprMethod = true;
booleanResult = Operand(variables["expr"]).boolean; booleanResult = Operand(variables["expr"]).boolean;
} }
while(booleanResult) { while(booleanResult) {
processContainer(container); processContainer(container);
container.reset(); container.reset();
if(exprMethod) {
variables["expr"].reset();
booleanResult = Operand(variables["expr"]).boolean;
} else {
booleanResult = false;
int rc = variables["value1"].compare(variables["value2"]);
if(((variables["type"] == "eq") && (rc == 0)) ||
((variables["type"] == "ne") && (rc != 0)) ||
((variables["type"] == "lt") && (rc == -1)) ||
((variables["type"] == "le") && (rc != 1)) ||
((variables["type"] == "gt") && (rc == 1)) ||
((variables["type"] == "ge") && (rc != -1)))
booleanResult = true;
}
} }
} }

View File

@ -1,6 +1,6 @@
#!./jet-2.0 #!../jet-2.0
<jet cgi="true" name1="localname" filterblanklines="true" trimlines="true"> <jet cgi="true" name1="localname" filterblanklines="true" trimlines="true">
<for name="ix" start="1" end="5" step="1"> <for name="ix" start="1" end="5" step=".1">
-->#[ix]<-- -->#[ix]<--
</for> </for>
</jet> </jet>

9
tests/testwhile.jet Executable file
View File

@ -0,0 +1,9 @@
#!../jet-2.0
<jet cgi="false" name1="localname" filterblanklines="true" trimlines="true">
<set name="ix" value="1" />
<while expr="$[ix]<10">
<set name="check" expr="$[ix]<10" />
-->$[check]-$[ix]<--
<set name="ix" expr="$[ix]+1" />
</while>
</jet>