OPerand now does its own expression parsing.

This commit is contained in:
brad Arant 2024-11-16 10:23:16 -08:00
parent 4d016197d2
commit 839f7e2eb8
11 changed files with 120 additions and 115 deletions

View File

@ -6,50 +6,54 @@
namespace jet {
Operand::Operand(coreutils::ZString &in) {
Operand::Operand(coreutils::ZString &in, Global &global, std::map<coreutils::ZString, coreutils::MString> &lvariables) {
doubleValue = 0;
in.skipWhitespace();
if(in.ifNext("(")) {
Operand op(in);
if(in.startsWith("$[") || in.startsWith("#[")) {
string = global.getVariable(in, lvariables);
doubleValue = string.asDouble();
isNumber = string.eod();
string.reset();
if((string == "false") || (string == "true"))
boolean = true;
} else if(in.ifNext("(")) {
Operand op(in, global, lvariables);
string = op.string;
doubleValue = op.doubleValue;
if(!in.ifNext(")"))
throw coreutils::Exception("expected ) in expression.");
}
if(in.ifNextIgnoreCase("SUBSTRING")) {
} else if(in.ifNextIgnoreCase("SUBSTRING")) {
if(!in.ifNext("("))
throw coreutils::Exception("Expecting ( for SUBSTRING parameters.");
Operand parm1(in);
Operand parm1(in, global, lvariables);
if(!in.ifNext(","))
throw coreutils::Exception("Expecting , in SUBSTRING expression.");
Operand parm2(in);
Operand parm2(in, global, lvariables);
if(in.ifNext(")")) {
string = parm1.string.substring(parm2.string.asInteger());
}
else if(!in.ifNext(","))
} else if(!in.ifNext(","))
throw coreutils::Exception("Expecting , in SUBSTRING expression.");
Operand parm3(in);
Operand parm3(in, global, lvariables);
if(in.ifNext(")")) {
string = parm1.string.substring(parm2.string.asInteger(), parm3.string.asInteger());
}
else
} else
throw coreutils::Exception("Expecting ) at end of substring expression.");
} else if(in.ifNextIgnoreCase("LEFT")) {
if(!in.ifNext("("))
throw coreutils::Exception("Expecting ( for LEFT parameters.");
Operand parm1(in);
Operand parm1(in, global, lvariables);
if(!in.ifNext(","))
throw coreutils::Exception("Expecting , in LEFT expression.");
Operand parm2(in);
Operand parm2(in, global, lvariables);
if(in.ifNext(")")) {
string = parm1.string.substring(0, parm2.string.asInteger());
}
else
} else
throw coreutils::Exception("Expecting ) at end of LEFT expression.");
} else if(in.ifNextIgnoreCase("EXPR")) {
} else if(in.ifNextIgnoreCase("RIGHT")) {
} else if(in.ifNextIgnoreCase("TRIM")) {
@ -90,80 +94,8 @@ namespace jet {
in.skipWhitespace();
if(in.ifNext("=")) {
Operand op(in);
if(isNumber && op.isNumber) {
if(doubleValue == op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string == op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext("<")) {
Operand op(in);
if(isNumber && op.isNumber) {
if(doubleValue < op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string < op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext(">")) {
Operand op(in);
if(isNumber && op.isNumber) {
if(doubleValue > op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string > op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext("!=") || in.ifNext("<>")) {
Operand op(in);
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue != op.doubleValue) {
boolean = true;
@ -187,7 +119,7 @@ namespace jet {
}
}
if(in.ifNext("<=")) {
Operand op(in);
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue <= op.doubleValue) {
boolean = true;
@ -211,7 +143,7 @@ namespace jet {
}
}
if(in.ifNext(">=")) {
Operand op(in);
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue >= op.doubleValue) {
boolean = true;
@ -234,9 +166,81 @@ namespace jet {
}
}
}
if(in.ifNext("=")) {
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue == op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string == op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext("<")) {
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue < op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string < op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext(">")) {
Operand op(in, global, lvariables);
if(isNumber && op.isNumber) {
if(doubleValue > op.doubleValue) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
} else if(!isNumber && !op.isNumber) {
if(string > op.string) {
boolean = true;
isNumber = false;
string = "true";
} else {
boolean = false;
isNumber = false;
string = "false";
}
}
}
if(in.ifNext("+")) {
if(isNumber) {
Operand op(in);
Operand op(in, global, lvariables);
if(op.isNumber) {
doubleValue += op.doubleValue;
string = std::format("{:.12f}", doubleValue);
@ -247,7 +251,7 @@ namespace jet {
throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("-")) {
if(isNumber) {
Operand op(in);
Operand op(in, global, lvariables);
if(op.isNumber) {
doubleValue -= op.doubleValue;
string = std::format("{:.12f}", doubleValue);
@ -258,7 +262,7 @@ namespace jet {
throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("*")) {
if(isNumber) {
Operand op(in);
Operand op(in, global, lvariables);
if(op.isNumber) {
doubleValue *= op.doubleValue;
string = std::format("{:.12f}", doubleValue);
@ -269,7 +273,7 @@ namespace jet {
throw coreutils::Exception("operand is not a number.");
} else if(in.ifNext("/")) {
if(isNumber) {
Operand op(in);
Operand op(in, global, lvariables);
if(op.isNumber) {
doubleValue /= op.doubleValue;
string = std::format("{:.12f}", doubleValue);

View File

@ -2,13 +2,14 @@
#define __Operand_h__
#include "MString.h"
#include "Global.h"
namespace jet {
class Operand {
public:
Operand(coreutils::ZString &in);
Operand(coreutils::ZString &in, Global &global, std::map<coreutils::ZString, coreutils::MString> &lvariables);
bool isNumber;

View File

@ -21,7 +21,7 @@ namespace jet {
if(variableDefined("expr")) {
if(variableDefined("eval"))
throw coreutils::Exception("Cannot use eval with expr.");
global.headers[variables["name"]] = Operand(variables["expr"]).string;
global.headers[variables["name"]] = Operand(variables["expr"], global, parent->variables).string;
} else if(hasContainer) {
processContainer(container);
if(evaluate) {

View File

@ -36,7 +36,7 @@ namespace jet {
throw coreutils::Exception("value2 should not be specified with expr.");
if(variableDefined("type"))
throw coreutils::Exception("type should not be specified with expr.");
booleanResult = Operand(variables["expr"]).boolean;
booleanResult = Operand(variables["expr"], global, parent->variables).boolean;
}
if(booleanResult)
processContainer(container);

View File

@ -24,13 +24,12 @@ namespace jet {
resolveKeyword("name");
if(variableDefined("expr")) {
resolveKeyword("expr");
if(!variableDefined("scope") || (variables["scope"] == "global"))
global.variables[variables["name"]] = Operand(variables["expr"]).string;
global.variables[variables["name"]] = Operand(variables["expr"], global, parent->variables).string;
else if(variables["scope"] == "local")
parent->variables[variables["name"]] = Operand(variables["expr"]).string;
parent->variables[variables["name"]] = Operand(variables["expr"], global, parent->variables).string;
else if(variables["scope"] == "parent")
parent->parent->variables[variables["name"]] = Operand(variables["expr"]).string;
parent->parent->variables[variables["name"]] = Operand(variables["expr"], global, parent->variables).string;
} else if(hasContainer) {
processContainer(container);
if(evaluate) {

View File

@ -47,7 +47,7 @@ namespace jet {
variables["expr"].reset();
variables["expr"] = exprSaved;
resolveKeyword("expr");
booleanResult = Operand(variables["expr"]).boolean;
booleanResult = Operand(variables["expr"], global, parent->variables).boolean;
} else {
booleanResult = false;
int rc = variables["value1"].compare(variables["value2"]);

View File

@ -39,8 +39,7 @@ namespace jet {
throw coreutils::Exception("type should not be specified with expr.");
exprMethod = true;
exprSaved = variables["expr"];
resolveKeyword("expr");
booleanResult = Operand(variables["expr"]).boolean;
booleanResult = Operand(variables["expr"], global, parent->variables).boolean;
}
while(booleanResult) {
processContainer(container);
@ -48,8 +47,7 @@ namespace jet {
if(exprMethod) {
variables["expr"].reset();
variables["expr"] = exprSaved;
resolveKeyword("expr");
booleanResult = Operand(variables["expr"]).boolean;
booleanResult = Operand(variables["expr"], global, parent->variables).boolean;
} else {
booleanResult = false;
int rc = variables["value1"].compare(variables["value2"]);

View File

@ -1,4 +1,4 @@
#!./jet-2.0
#!../jet-2.0
<jet>
This is output
</jet>

View File

@ -1,4 +1,4 @@
#!./jet-2.0
#!../jet-2.0
<jet cgi="true" name1="localname" filterblanklines="true" trimlines="true">
<mysql host="localhost" database="barant" user="barant" password="uversa" sessionid="1">
<sql sessionid="1">select * from testdata</sql>

View File

@ -1,13 +1,16 @@
#!./jet-2.0
#!../jet-2.0
<jet filterblanklines="true" trimlines="true">
<tag name="test">
x
<set name="ix" expr="$[ix]+1" />
<container />
$[ix]
z
</tag>
<set name="ix" value="1" />
<test>
<set name="ix" expr="$[ix]+1" />
y
<set name="ix" expr="$[ix]+3" />
</test>
<test />
</jet>

View File

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