JetCore/Operand.cpp

124 lines
3.4 KiB
C++

#include "Operand.h"
#include "Exception.h"
#include <format>
#include <iostream>
namespace jet {
Operand::Operand(coreutils::ZString &in) {
doubleValue = 0;
in.skipWhitespace();
// 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.");
Operand parm1(in);
if(!in.ifNext(","))
throw coreutils::Exception("Expecting , in SUBSTRING expression.");
Operand parm2(in);
if(in.ifNext(")")) {
string = parm1.string.substring(parm2.string.asInteger());
}
else if(!in.ifNext(","))
throw coreutils::Exception("Expecting , in SUBSTRING expression.");
Operand parm3(in);
if(in.ifNext(")")) {
string = parm1.string.substring(parm2.string.asInteger(), parm3.string.asInteger());
}
else
throw coreutils::Exception("Expecting ) at end of substring expression.");
} else if(in.ifNext("LEFT")) {
} else if(in.ifNext("RIGHT")) {
} else if(in.ifNext("TRIM")) {
} else if(in.ifNext("TOUPPER")) {
} else if(in.ifNext("TOLOWER")) {
} else if(in.ifNext("REVERSE")) {
} else if(in.ifNext("CONCAT")) {
} else if(in.ifNext("true")) {
boolean = true;
string = "true";
} else if(in.ifNext("false")) {
boolean = false;
string = "false";
} else if(in.startsWithNumber()) {
doubleValue = in.asDouble();
string = std::format("{}", doubleValue);
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;
}
}
}