Fixed expressions to be able to do math with nested expressions.

This commit is contained in:
Brad Arant committed 2024-09-30 14:20:59 -07:00
1 parent 0b9f2354a3
commit aa090c8a73
11 files changed
+86 -98

No files matched your search

+64 -5
View File
@@ -7,8 +7,22 @@ namespace jet {
Operand::Operand(coreutils::ZString &in) {
doubleValue = 0;
in.skipWhitespace();
std::cout << "op: [" << in.unparsed() << "]" << std::endl;
// std::cout << "op: [" << in.unparsed() << "]" << std::endl;
if(in.ifNext("(")) {
// std::cout << "(" << in.unparsed() << std::endl;
Operand op(in);
string = op.string;
doubleValue = op.doubleValue;
if(!in.ifNext(")"))
throw coreutils::Exception("expected ) in expression.");
// std::cout << ")" << in.unparsed() << std::endl;
}
if(in.ifNext("SUBSTRING")) {
if(!in.ifNext("("))
throw coreutils::Exception("Expecting ( for SUBSTRING parameters.");
@@ -44,21 +58,66 @@ namespace jet {
} else if(in.ifNext("true")) {
boolean = true;
string = "true";
return;
} else if(in.ifNext("false")) {
boolean = false;
string = "false";
return;
} else if(in.startsWithNumber()) {
doubleValue = in.asDouble();
string = std::format("{}", doubleValue);
return;
isNumber = true;
} else if(in.ifNext("'")) {
string = in.getTokenExclude("'");
in.ifNext("'");
isNumber = false;
}
in.skipWhitespace();
if(in.ifNext("+")) {
if(isNumber) {
Operand op(in);
if(op.isNumber) {
doubleValue += op.doubleValue;
string = std::format("{}", doubleValue);
} 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);
if(op.isNumber) {
doubleValue -= op.doubleValue;
string = std::format("{}", doubleValue);
} 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);
if(op.isNumber) {
doubleValue *= op.doubleValue;
string = std::format("{}", doubleValue);
} 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);
if(op.isNumber) {
doubleValue /= op.doubleValue;
string = std::format("{}", doubleValue);
} else
throw coreutils::Exception("operand is not a number.");
} else
throw coreutils::Exception("operand is not a number.");
} else {
// std::cout << ">" << string << std::endl;
return;
}
}
}