diff --git a/Expression.cpp b/Expression.cpp deleted file mode 100644 index cf24baa..0000000 --- a/Expression.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "Expression.h" -#include "Operand.h" -#include "Exception.h" -#include - -namespace jet { - - Expression::Expression(coreutils::ZString &in) : MString() { - std::cout << "Expression construction:" << in << std::endl; - - Operand op1(in); - - boolean = op1.boolean; - string = op1.string; - - if(in.unparsed().getLength() > 0) { - std::cout << "1)" << in.unparsed() << "(" << std::endl; - if(in.ifNext("+")) - operation = (char *)"+"; - else if(in.ifNext("-")) - operation = (char *)"-"; - else - throw coreutils::Exception("Expecting operator."); - in.skipWhitespace(); - if(in.unparsed().getLength() == 0) - throw coreutils::Exception("Expecting operand."); - Operand op2(in); - std::cout << op1.string << ":" << operation << ":" << op2.string << std::endl; - } - - - - std::cout << "Leaving expression string: " << string << std::endl; - std::cout << "Unparsed: [" << in.unparsed() << "]" << std::endl; - } - - Expression::~Expression() {} - - bool Expression::getBooleanResult() { - return boolean; - } - - double Expression::getNumericResult() { - return 0; - } - - coreutils::ZString Expression::getStringResult() { - return coreutils::ZString(); - } - -} diff --git a/Expression.h b/Expression.h deleted file mode 100644 index 7a1c788..0000000 --- a/Expression.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef __Expression_h__ -#define __Expression_h__ - -#include "MString.h" - -namespace jet { - - class Expression : public coreutils::MString { - - public: - Expression(coreutils::ZString &in); - virtual ~Expression(); - - bool getBooleanResult(); - double getNumericResult(); - coreutils::ZString getStringResult(); - bool boolean; - coreutils::MString string; - char *operation; - }; - -} - -#endif diff --git a/Operand.cpp b/Operand.cpp index 516306e..377f336 100644 --- a/Operand.cpp +++ b/Operand.cpp @@ -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; } - + } } diff --git a/Operand.h b/Operand.h index 073af77..d6550a7 100644 --- a/Operand.h +++ b/Operand.h @@ -10,8 +10,7 @@ namespace jet { public: Operand(coreutils::ZString &in); - bool isNumber(); - bool isString(); + bool isNumber; /// /// boolean is set by internal processes to return the boolean @@ -21,8 +20,6 @@ namespace jet { bool boolean; coreutils::MString string; - private: -// dataType enum ={}; double doubleValue; diff --git a/__for.cpp b/__for.cpp index 29d0a41..940d018 100644 --- a/__for.cpp +++ b/__for.cpp @@ -18,7 +18,6 @@ namespace jet { if(variableDefined(coreutils::ZString("step"))) { step = variables["step"].asDouble(); } - std::cout << "start: " << counter << "; end: " << end << "; step: " << step << std::endl; for(double ix = counter; ix <= end; ix += step) { if(nameDefined) variables[variables["name"]] = ix; diff --git a/__header.cpp b/__header.cpp index 54599ff..a6463b2 100644 --- a/__header.cpp +++ b/__header.cpp @@ -1,6 +1,6 @@ #include "__header.h" #include "Exception.h" -#include "Expression.h" +#include "Operand.h" #include namespace jet { @@ -20,7 +20,7 @@ namespace jet { if(variableDefined("expr")) { if(variableDefined("eval")) throw coreutils::Exception("Cannot use eval with expr."); - global.headers[variables["name"]] = Expression(variables["expr"]).string; + global.headers[variables["name"]] = Operand(variables["expr"]).string; } else if(hasContainer) { processContainer(container); if(evaluate) { diff --git a/__if.cpp b/__if.cpp index d7b5ab1..ba3fa64 100644 --- a/__if.cpp +++ b/__if.cpp @@ -1,7 +1,7 @@ #include "__if.h" #include "Exception.h" #include -#include "Expression.h" +#include "Operand.h" namespace jet { @@ -15,7 +15,7 @@ namespace jet { throw coreutils::Exception("Either value1 or expr can be specified but not both."); if(variableDefined("value2")) { if(variableDefined("type")) { - result = Expression(variables["expr"]); + } else throw coreutils::Exception("type expected if value1 and value2 specified."); } else @@ -37,8 +37,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."); - result = Expression(variables["expr"]); - booleanResult = true; + booleanResult = Operand(variables["expr"]).boolean; } if(booleanResult) processContainer(container); diff --git a/__set.cpp b/__set.cpp index 5b8c43b..ddccb6f 100644 --- a/__set.cpp +++ b/__set.cpp @@ -1,6 +1,6 @@ #include "__set.h" #include "Exception.h" -#include "Expression.h" +#include "Operand.h" #include namespace jet { @@ -21,7 +21,7 @@ namespace jet { if(variableDefined("expr")) { if(variableDefined("eval")) throw coreutils::Exception("Cannot use eval with expr."); - global.variables[variables["name"]] = Expression(variables["expr"]).string; + global.variables[variables["name"]] = Operand(variables["expr"]).string; } else if(hasContainer) { processContainer(container); if(evaluate) { diff --git a/__write.cpp b/__write.cpp index 96e43b3..6bf327a 100644 --- a/__write.cpp +++ b/__write.cpp @@ -1,6 +1,6 @@ #include "__write.h" #include "Exception.h" -#include "Expression.h" +#include "Operand.h" #include #include #include diff --git a/jet-2.0 b/jet-2.0 index 202ba81..f8ee2a9 100755 Binary files a/jet-2.0 and b/jet-2.0 differ diff --git a/testjet.jet b/testjet.jet index f45ab0c..f82f0cb 100755 --- a/testjet.jet +++ b/testjet.jet @@ -13,10 +13,19 @@ - - - theexpr=($[theexpr]) - theexpr2($[theexpr2]) + + + + + + + + $[nested] + substring('abcdefg', 1, 3)=[$[theexpr]] + 5+3=($[addition]) + 5-3($[subtraction]) + 5*3($[multiplication]) + 5/3($[division]) this is the value store in #[name]. this is the value store in #[name].