expression.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. 
  2. #include "expression.h"
  3. #include "../../../DFPSR/api/stringAPI.h"
  4. using namespace dsr;
  5. POIndex::POIndex() {}
  6. POIndex::POIndex(int16_t precedenceIndex, int16_t operationIndex) : precedenceIndex(precedenceIndex), operationIndex(operationIndex) {}
  7. Operation::Operation(int16_t symbolIndex, std::function<dsr::String(dsr::ReadableString, dsr::ReadableString)> action)
  8. : symbolIndex(symbolIndex), action(action) {
  9. }
  10. static int16_t addOperation(ExpressionSyntax &targetSyntax, int16_t symbolIndex, std::function<dsr::String(dsr::ReadableString, dsr::ReadableString)> action) {
  11. int16_t precedenceIndex = targetSyntax.precedences.length() - 1;
  12. int16_t operationIndex = targetSyntax.precedences.last().operations.length();
  13. // TODO: Only allow assigning a symbol once per prefix, infix and postfix.
  14. targetSyntax.symbols[symbolIndex].operations[targetSyntax.precedences.last().notation] = POIndex(precedenceIndex, operationIndex);
  15. targetSyntax.precedences.last().operations.pushConstruct(symbolIndex, action);
  16. return operationIndex;
  17. }
  18. Precedence::Precedence(Notation notation, Associativity associativity)
  19. : notation(notation), associativity(associativity) {}
  20. Symbol::Symbol(const dsr::ReadableString &token, bool atomic, int32_t depthOffset)
  21. : token(token), atomic(atomic), depthOffset(depthOffset) {}
  22. ReadableString expression_getToken(const List<String> &tokens, int64_t index) {
  23. if (0 <= index && index < tokens.length()) {
  24. return tokens[index];
  25. } else {
  26. return U"";
  27. }
  28. }
  29. int64_t expression_interpretAsInteger(const dsr::ReadableString &value) {
  30. if (string_length(value) == 0) {
  31. return 0;
  32. } else {
  33. return string_toInteger(value);
  34. }
  35. }
  36. String expression_unwrapIfNeeded(const dsr::ReadableString &text) {
  37. if (text[0] == U'\"') {
  38. return string_unmangleQuote(text);
  39. } else {
  40. return text;
  41. }
  42. }
  43. static int16_t createSymbol(ExpressionSyntax &targetSyntax, const dsr::ReadableString &token, bool atomic, int32_t depthOffset) {
  44. targetSyntax.symbols.pushConstruct(token, atomic, depthOffset);
  45. return targetSyntax.symbols.length() - 1;
  46. }
  47. #define CREATE_KEYWORD(TOKEN) createSymbol(*this, TOKEN, false, 0);
  48. #define CREATE_ATOMIC(TOKEN) createSymbol(*this, TOKEN, true, 0);
  49. #define CREATE_LEFT(TOKEN) createSymbol(*this, TOKEN, true, 1);
  50. #define CREATE_RIGHT(TOKEN) createSymbol(*this, TOKEN, true, -1);
  51. ExpressionSyntax::ExpressionSyntax() {
  52. // Symbols must be entered with longest match first, so that they can be used for tokenization.
  53. // Keywords
  54. int16_t token_string_match = CREATE_KEYWORD(U"matches");
  55. int16_t token_logical_and = CREATE_KEYWORD(U"and");
  56. int16_t token_logical_xor = CREATE_KEYWORD(U"xor");
  57. int16_t token_logical_or = CREATE_KEYWORD(U"or");
  58. // Length 2 symbols
  59. int16_t token_lesserEqual = CREATE_ATOMIC(U"<=");
  60. int16_t token_greaterEqual = CREATE_ATOMIC(U">=");
  61. int16_t token_equal = CREATE_ATOMIC(U"==");
  62. int16_t token_notEqual = CREATE_ATOMIC(U"!=");
  63. int16_t token_leftArrow = CREATE_ATOMIC(U"<-");
  64. int16_t token_rightArrow = CREATE_ATOMIC(U"->");
  65. // Length 1 symbols
  66. int16_t token_plus = CREATE_ATOMIC(U"+");
  67. int16_t token_minus = CREATE_ATOMIC(U"-");
  68. int16_t token_star = CREATE_ATOMIC(U"*");
  69. int16_t token_forwardSlash = CREATE_ATOMIC(U"/");
  70. int16_t token_backSlash = CREATE_ATOMIC(U"\\");
  71. int16_t token_exclamation = CREATE_ATOMIC(U"!");
  72. int16_t token_lesser = CREATE_ATOMIC(U"<");
  73. int16_t token_greater = CREATE_ATOMIC(U">");
  74. int16_t token_ampersand = CREATE_ATOMIC(U"&");
  75. // TODO: Connect scopes to each other for matching
  76. int16_t token_leftParen = CREATE_LEFT(U"(");
  77. int16_t token_leftBracket = CREATE_LEFT(U"[");
  78. int16_t token_leftCurl = CREATE_LEFT(U"{");
  79. int16_t token_rightParen = CREATE_RIGHT(U")");
  80. int16_t token_rightBracket = CREATE_RIGHT(U"]");
  81. int16_t token_rightCurl = CREATE_RIGHT(U"}");
  82. // Unidentified tokens are treated as identifiers or values with index -1.
  83. // Each symbol can be tied once to prefix, once to infix and once to postfix.
  84. this->precedences.pushConstruct(Notation::Prefix, Associativity::RightToLeft);
  85. // Unary negation
  86. addOperation(*this, token_minus, [](ReadableString lhs, ReadableString rhs) -> String {
  87. return string_combine(-expression_interpretAsInteger(rhs));
  88. });
  89. // Unary logical not
  90. addOperation(*this, token_exclamation, [](ReadableString lhs, ReadableString rhs) -> String {
  91. return string_combine((!expression_interpretAsInteger(rhs)) ? 1 : 0);
  92. });
  93. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  94. // Infix integer multiplication
  95. addOperation(*this, token_star, [](ReadableString lhs, ReadableString rhs) -> String {
  96. return string_combine(expression_interpretAsInteger(lhs) * expression_interpretAsInteger(rhs));
  97. });
  98. // Infix integer division
  99. addOperation(*this, token_forwardSlash, [](ReadableString lhs, ReadableString rhs) -> String {
  100. return string_combine(expression_interpretAsInteger(lhs) / expression_interpretAsInteger(rhs));
  101. });
  102. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  103. // Infix integer addition
  104. addOperation(*this, token_plus, [](ReadableString lhs, ReadableString rhs) -> String {
  105. return string_combine(expression_interpretAsInteger(lhs) + expression_interpretAsInteger(rhs));
  106. });
  107. // Infix integer subtraction
  108. addOperation(*this, token_minus, [](ReadableString lhs, ReadableString rhs) -> String {
  109. return string_combine(expression_interpretAsInteger(lhs) - expression_interpretAsInteger(rhs));
  110. });
  111. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  112. // Infix integer lesser than comparison
  113. addOperation(*this, token_lesser, [](ReadableString lhs, ReadableString rhs) -> String {
  114. return string_combine((expression_interpretAsInteger(lhs) < expression_interpretAsInteger(rhs)) ? 1 : 0);
  115. });
  116. // Infix integer greater than comparison
  117. addOperation(*this, token_greater, [](ReadableString lhs, ReadableString rhs) -> String {
  118. return string_combine((expression_interpretAsInteger(lhs) > expression_interpretAsInteger(rhs)) ? 1 : 0);
  119. });
  120. // Infix integer lesser than or equal to comparison
  121. addOperation(*this, token_lesserEqual, [](ReadableString lhs, ReadableString rhs) -> String {
  122. return string_combine((expression_interpretAsInteger(lhs) <= expression_interpretAsInteger(rhs)) ? 1 : 0);
  123. });
  124. // Infix integer greater than or equal to comparison
  125. addOperation(*this, token_greaterEqual, [](ReadableString lhs, ReadableString rhs) -> String {
  126. return string_combine((expression_interpretAsInteger(lhs) >= expression_interpretAsInteger(rhs)) ? 1 : 0);
  127. });
  128. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  129. // Infix case sensitive string match
  130. addOperation(*this, token_string_match, [](ReadableString lhs, ReadableString rhs) -> String {
  131. return string_combine(string_match(lhs, rhs) ? 1 : 0);
  132. });
  133. // Infix integer equal to comparison
  134. addOperation(*this, token_equal, [](ReadableString lhs, ReadableString rhs) -> String {
  135. return string_combine((expression_interpretAsInteger(lhs) == expression_interpretAsInteger(rhs)) ? 1 : 0);
  136. });
  137. // Infix integer not equal to comparison
  138. addOperation(*this, token_notEqual, [](ReadableString lhs, ReadableString rhs) -> String {
  139. return string_combine((expression_interpretAsInteger(lhs) != expression_interpretAsInteger(rhs)) ? 1 : 0);
  140. });
  141. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  142. // Infix logical and
  143. addOperation(*this, token_logical_and, [](ReadableString lhs, ReadableString rhs) -> String {
  144. return string_combine((expression_interpretAsInteger(lhs) && expression_interpretAsInteger(rhs)) ? 1 : 0);
  145. });
  146. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  147. // Infix logical inclusive or
  148. addOperation(*this, token_logical_or, [](ReadableString lhs, ReadableString rhs) -> String {
  149. return string_combine((expression_interpretAsInteger(lhs) || expression_interpretAsInteger(rhs)) ? 1 : 0);
  150. });
  151. // Infix logical exclusive or
  152. addOperation(*this, token_logical_xor, [](ReadableString lhs, ReadableString rhs) -> String {
  153. return string_combine((!expression_interpretAsInteger(lhs) != !expression_interpretAsInteger(rhs)) ? 1 : 0);
  154. });
  155. this->precedences.pushConstruct(Notation::Infix, Associativity::LeftToRight);
  156. // Infix string concatenation
  157. addOperation(*this, token_ampersand, [](ReadableString lhs, ReadableString rhs) -> String {
  158. return string_combine(lhs, rhs);
  159. });
  160. }
  161. ExpressionSyntax defaultSyntax;
  162. struct TokenInfo {
  163. int32_t depth = -1;
  164. int16_t symbolIndex = -1;
  165. TokenInfo() {}
  166. TokenInfo(int32_t depth, int16_t symbolIndex)
  167. : depth(depth), symbolIndex(symbolIndex) {}
  168. };
  169. /*
  170. static String debugTokens(const List<TokenInfo> &info, int64_t infoStart, const List<String> &tokens, int64_t startTokenIndex, int64_t endTokenIndex) {
  171. String result;
  172. for (int64_t t = startTokenIndex; t <= endTokenIndex; t++) {
  173. int64_t infoIndex = t - infoStart;
  174. if (t > startTokenIndex) {
  175. string_appendChar(result, U' ');
  176. }
  177. string_append(result, tokens[t]);
  178. }
  179. string_append(result, U" : ");
  180. for (int64_t t = startTokenIndex; t <= endTokenIndex; t++) {
  181. int64_t infoIndex = t - infoStart;
  182. if (t > startTokenIndex) {
  183. string_appendChar(result, U' ');
  184. }
  185. string_append(result, "[", info[infoIndex].depth, ",", info[infoIndex].symbolIndex, ",", tokens[t], "]");
  186. }
  187. return result;
  188. }
  189. */
  190. static int16_t identifySymbol(const ReadableString &token, const ExpressionSyntax &syntax) {
  191. for (int64_t s = 0; s < syntax.symbols.length(); s++) {
  192. if (syntax.symbols[s].atomic) {
  193. if (string_match(token, syntax.symbols[s].token)) {
  194. return s;
  195. }
  196. } else {
  197. // TODO: Make case insensitive optional for keywords.
  198. if (string_caseInsensitiveMatch(token, syntax.symbols[s].token)) {
  199. return s;
  200. }
  201. }
  202. }
  203. return -1; // Pattern to resolve later.
  204. }
  205. // Returns true iff the symbol can be at the leftmost side of a sub-expression.
  206. static bool validLeftmostSymbol(const Symbol &symbol) {
  207. if (symbol.depthOffset > 0) {
  208. return true; // ( [ { as the left side of a right hand side
  209. } else {
  210. return symbol.operations[Notation::Prefix].operationIndex != -1; // Accept prefix operations on the rightmost side
  211. }
  212. }
  213. // Returns true iff the symbol can be at the rightmost side of a sub-expression.
  214. static bool validRightmostSymbol(const Symbol &symbol) {
  215. if (symbol.depthOffset < 0) {
  216. return true; // Accept ) ] } as the right side of a left hand side
  217. } else {
  218. return symbol.operations[Notation::Postfix].operationIndex != -1; // Accept postfix operations on the rightmost side
  219. }
  220. }
  221. // Returns true iff the symbol can be at the leftmost side of a sub-expression.
  222. static bool validLeftmostToken(int16_t symbolIndex, const ExpressionSyntax &syntax) {
  223. return symbolIndex < 0 || validLeftmostSymbol(syntax.symbols[symbolIndex]);
  224. }
  225. // Returns true iff the symbol can be at the rightmost side of a sub-expression.
  226. static bool validRightmostToken(int16_t symbolIndex, const ExpressionSyntax &syntax) {
  227. return symbolIndex < 0 || validRightmostSymbol(syntax.symbols[symbolIndex]);
  228. }
  229. // info is a list of additional information starting with info[0] at tokens[startTokenIndex]
  230. // infoStart is the startTokenIndex of the root evaluation call
  231. static String expression_evaluate_helper(const List<TokenInfo> &info, int64_t infoStart, int64_t currentDepth, const List<String> &tokens, int64_t startTokenIndex, int64_t endTokenIndex, const ExpressionSyntax &syntax, std::function<String(ReadableString)> identifierEvaluation) {
  232. //printText(U"Evaluate: ", debugTokens(info, infoStart, tokens, startTokenIndex, endTokenIndex), U"\n");
  233. if (startTokenIndex == endTokenIndex) {
  234. ReadableString first = expression_getToken(tokens, startTokenIndex);
  235. if (string_isInteger(first)) {
  236. return first;
  237. } else if (first[0] == U'\"') {
  238. return string_unmangleQuote(first);
  239. } else {
  240. // Identifier defaulting to empty.
  241. return identifierEvaluation(first);
  242. }
  243. } else {
  244. // Find the outmost operation using recursive descent parsing, in which precedence and direction when going down is reversed relative to order of evaluation when going up.
  245. for (int64_t p = syntax.precedences.length() - 1; p >= 0; p--) {
  246. const Precedence *precedence = &(syntax.precedences[p]);
  247. int64_t leftScanBound = 0;
  248. int64_t rightScanBound = 0;
  249. if (precedence->notation == Notation::Prefix) {
  250. // A prefix can only be used at the start of the current sub-expression
  251. leftScanBound = startTokenIndex;
  252. rightScanBound = startTokenIndex;
  253. //printText("precendence = ", p, U" (prefix)\n");
  254. } else if (precedence->notation == Notation::Infix) {
  255. // Skip ends when looking for infix operations
  256. leftScanBound = startTokenIndex + 1;
  257. rightScanBound = endTokenIndex - 1;
  258. //printText("precendence = ", p, U" (infix)\n");
  259. } else if (precedence->notation == Notation::Postfix) {
  260. // A postfix can only be used at the end of the current sub-expression
  261. leftScanBound = endTokenIndex;
  262. rightScanBound = endTokenIndex;
  263. //printText("precendence = ", p, U" (postfix)\n");
  264. }
  265. int64_t opStep = (precedence->associativity == Associativity::LeftToRight) ? -1 : 1;
  266. int64_t opIndex = (precedence->associativity == Associativity::LeftToRight) ? rightScanBound : leftScanBound;
  267. int64_t stepCount = 1 + rightScanBound - leftScanBound;
  268. for (int64_t i = 0; i < stepCount; i++) {
  269. int64_t infoIndex = opIndex - infoStart;
  270. TokenInfo leftInfo = (opIndex <= startTokenIndex) ? TokenInfo() : info[infoIndex - 1];
  271. TokenInfo currentInfo = info[infoIndex];
  272. TokenInfo rightInfo = (opIndex >= endTokenIndex) ? TokenInfo() : info[infoIndex + 1];
  273. // Only match outmost at currentDepth.
  274. if (currentInfo.depth == currentDepth && currentInfo.symbolIndex > -1) {
  275. // If the current symbol is has an operation in the same notation and precedence, then grab that operation index.
  276. const Symbol *currentSymbol = &(syntax.symbols[currentInfo.symbolIndex]);
  277. if (currentSymbol->operations[precedence->notation].precedenceIndex == p) {
  278. // Resolve the common types of ambiguity that can quickly be resolved and let the other cases fail if the syntax is too ambiguous.
  279. bool validLeft = validRightmostToken(leftInfo.symbolIndex, syntax);
  280. bool validRight = validLeftmostToken(rightInfo.symbolIndex, syntax);
  281. bool valid = true;
  282. if (precedence->notation == Notation::Prefix) {
  283. if (!validRight) valid = false;
  284. } else if (precedence->notation == Notation::Infix) {
  285. if (!validLeft) valid = false;
  286. if (!validRight) valid = false;
  287. } else if (precedence->notation == Notation::Postfix) {
  288. if (!validLeft) valid = false;
  289. }
  290. if (valid) {
  291. const Operation *operation = &(precedence->operations[currentSymbol->operations[precedence->notation].operationIndex]);
  292. String lhs = (precedence->notation == Notation::Prefix) ? U"" : expression_evaluate_helper(info, infoStart, currentDepth, tokens, startTokenIndex, opIndex - 1, syntax, identifierEvaluation);
  293. String rhs = (precedence->notation == Notation::Postfix) ? U"" : expression_evaluate_helper(info, infoStart, currentDepth, tokens, opIndex + 1, endTokenIndex, syntax, identifierEvaluation);
  294. /*
  295. printText(U"Applied ", currentSymbol->token, "\n");
  296. printText(U" currentDepth = ", currentDepth, U"\n");
  297. printText(U" lhs = ", lhs, U"\n");
  298. printText(U" rhs = ", rhs, U"\n");
  299. printText(U" startTokenIndex = ", startTokenIndex, U"\n");
  300. printText(U" leftScanBound = ", leftScanBound, U"\n");
  301. printText(U" rightScanBound = ", rightScanBound, U"\n");
  302. printText(U" endTokenIndex = ", endTokenIndex, U"\n");
  303. printText(U" opStep = ", opStep, U"\n");
  304. printText(U" opIndex = ", opIndex, U"\n");
  305. printText(U" stepCount = ", stepCount, U"\n");
  306. printText(U" notation = ", precedence->notation, U"\n");
  307. printText(U" validLeft(", leftInfo.symbolIndex, U") = ", validLeft, U"\n");
  308. printText(U" validRight(", rightInfo.symbolIndex, U") = ", validRight, U"\n");
  309. */
  310. return operation->action(lhs, rhs);
  311. }
  312. }
  313. }
  314. opIndex += opStep;
  315. }
  316. }
  317. if (string_match(tokens[startTokenIndex], U"(") && string_match(tokens[endTokenIndex], U")")) {
  318. //printText(U"Unwrapping ()\n");
  319. return expression_evaluate_helper(info, infoStart, currentDepth + 1, tokens, startTokenIndex + 1, endTokenIndex - 1, syntax, identifierEvaluation);
  320. }
  321. }
  322. return U"<ERROR:Invalid expression>";
  323. }
  324. String expression_evaluate(const List<String> &tokens, int64_t startTokenIndex, int64_t endTokenIndex, const ExpressionSyntax &syntax, std::function<String(ReadableString)> identifierEvaluation) {
  325. // Scan the whole expression once in the beginning and write useful information into a separate list.
  326. // This allow handling tokens as plain lists of strings while still being able to number what they are.
  327. int32_t depth = 0;
  328. List<TokenInfo> info;
  329. for (int64_t opIndex = startTokenIndex; opIndex <= endTokenIndex; opIndex++) {
  330. String currentToken = tokens[opIndex];
  331. int16_t symbolIndex = identifySymbol(currentToken, syntax);
  332. int32_t depthOffet = (symbolIndex == -1) ? 0 : syntax.symbols[symbolIndex].depthOffset;
  333. if (depthOffet < 0) { // ) ] }
  334. depth += depthOffet;
  335. if (depth < 0) return U"<ERROR:Negative expression depth>";
  336. }
  337. info.pushConstruct(depth, symbolIndex);
  338. if (depthOffet > 0) { // ( [ {
  339. depth += depthOffet;
  340. }
  341. }
  342. if (depth != 0) return U"<ERROR:Unbalanced expression depth>";
  343. return expression_evaluate_helper(info, startTokenIndex, 0, tokens, startTokenIndex, endTokenIndex, syntax, identifierEvaluation);
  344. }
  345. String expression_evaluate(const List<String> &tokens, int64_t startTokenIndex, int64_t endTokenIndex, std::function<String(ReadableString)> identifierEvaluation) {
  346. return expression_evaluate(tokens, startTokenIndex, endTokenIndex, defaultSyntax, identifierEvaluation);
  347. }
  348. String expression_evaluate(const List<String> &tokens, std::function<String(ReadableString)> identifierEvaluation) {
  349. return expression_evaluate(tokens, 0, tokens.length() - 1, defaultSyntax, identifierEvaluation);
  350. }
  351. // -------- Regression tests --------
  352. template<typename TYPE>
  353. inline void appendToken(List<String>& target, const TYPE head) {
  354. target.push(head);
  355. }
  356. template<typename HEAD, typename... TAIL>
  357. inline void appendToken(List<String>& target, const HEAD head, TAIL... tail) {
  358. appendToken(target, head);
  359. appendToken(target, tail...);
  360. }
  361. template<typename... ARGS>
  362. inline List<String> combineTokens(ARGS... args) {
  363. List<String> result;
  364. appendToken(result, args...);
  365. return result;
  366. }
  367. static void expectResult(int64_t &errorCount, const ReadableString &result, const ReadableString &expected) {
  368. if (string_match(result, expected)) {
  369. printText(U"* Passed ", expected, U"\n");
  370. } else {
  371. printText(U" - Failed ", expected, U" with unexpected ", result, U"\n");
  372. errorCount++;
  373. }
  374. }
  375. void expression_runRegressionTests() {
  376. std::function<String(ReadableString)> context = [](ReadableString identifier) -> String {
  377. if (string_caseInsensitiveMatch(identifier, U"x")) {
  378. return U"5";
  379. } else if (string_caseInsensitiveMatch(identifier, U"doorCount")) {
  380. return U"48";
  381. } else if (string_caseInsensitiveMatch(identifier, U"temperature")) {
  382. return U"-18";
  383. } else {
  384. return U"<ERROR:Unresolved identifier>";
  385. }
  386. };
  387. /*for (int64_t s = 0; s < defaultSyntax.symbols.length(); s++) {
  388. printText(U"Symbol ", defaultSyntax.symbols[s].token, U"\n");
  389. if (validLeftmostToken(s, defaultSyntax)) printText(U" Can be leftmost\n");
  390. if (validRightmostToken(s, defaultSyntax)) printText(U" Can be rightmost\n");
  391. }*/
  392. int64_t ec = 0;
  393. expectResult(ec, expression_evaluate(combineTokens(U""), context), U"<ERROR:Unresolved identifier>");
  394. expectResult(ec, expression_evaluate(combineTokens(U"0"), context), U"0");
  395. expectResult(ec, expression_evaluate(combineTokens(U"(", U"19", U")"), context), U"19");
  396. expectResult(ec, expression_evaluate(combineTokens(U"(", U"2", U"+", U"4", U")"), context), U"6");
  397. expectResult(ec, expression_evaluate(combineTokens(U"3"), context), U"3");
  398. expectResult(ec, expression_evaluate(combineTokens(U"-5"), context), U"-5");
  399. expectResult(ec, expression_evaluate(combineTokens(U"-", U"32"), context), U"-32");
  400. expectResult(ec, expression_evaluate(combineTokens(U"3", U"+", U"6"), context), U"9");
  401. expectResult(ec, expression_evaluate(combineTokens(U"x"), context), U"5");
  402. expectResult(ec, expression_evaluate(combineTokens(U"doorCount"), context), U"48");
  403. expectResult(ec, expression_evaluate(combineTokens(U"temperature"), context), U"-18");
  404. expectResult(ec, expression_evaluate(combineTokens(U"nonsense"), context), U"<ERROR:Unresolved identifier>");
  405. expectResult(ec, expression_evaluate(combineTokens(U"6", U"*", U"2", U"+", U"4"), context), U"16");
  406. expectResult(ec, expression_evaluate(combineTokens(U"4", U"+", U"6", U"*", U"2"), context), U"16");
  407. expectResult(ec, expression_evaluate(combineTokens(U"4", U"+", U"(", U"6", U"*", U"2", U")"), context), U"16");
  408. expectResult(ec, expression_evaluate(combineTokens(U"(", U"4", U"+", U"6", U")", U"*", U"2"), context), U"20");
  409. expectResult(ec, expression_evaluate(combineTokens(U"5", U"+", U"-", U"7"), context), U"-2");
  410. expectResult(ec, expression_evaluate(combineTokens(U"5", U"+", U"(", U"-", U"7", U")"), context), U"-2");
  411. expectResult(ec, expression_evaluate(combineTokens(U"5", U"+", U"(", U"-7", U")"), context), U"-2");
  412. expectResult(ec, expression_evaluate(combineTokens(U"5", U"+", U"-7"), context), U"-2");
  413. expectResult(ec, expression_evaluate(combineTokens(U"5", U"-", U"-", U"7"), context), U"12");
  414. expectResult(ec, expression_evaluate(combineTokens(U"5", U"&", U"-", U"7"), context), U"5-7");
  415. expectResult(ec, expression_evaluate(combineTokens(U"(", U"6", U"+", U"8", U")", U"/", U"(", U"9", U"-", U"2", U")"), context), U"2");
  416. expectResult(ec, expression_evaluate(combineTokens(U"(", U"6", U"+", U"8", U")", U"*", U"(", U"9", U"-", U"2", U")"), context), U"98");
  417. expectResult(ec, expression_evaluate(combineTokens(U"&", U"-", U"7"), context), U"<ERROR:Invalid expression>");
  418. expectResult(ec, expression_evaluate(combineTokens(U"(", U"-7"), context), U"<ERROR:Unbalanced expression depth>");
  419. expectResult(ec, expression_evaluate(combineTokens(U")", U"3"), context), U"<ERROR:Negative expression depth>");
  420. expectResult(ec, expression_evaluate(combineTokens(U"[", U"8"), context), U"<ERROR:Unbalanced expression depth>");
  421. expectResult(ec, expression_evaluate(combineTokens(U"]", U"65"), context), U"<ERROR:Negative expression depth>");
  422. expectResult(ec, expression_evaluate(combineTokens(U"{", U"12"), context), U"<ERROR:Unbalanced expression depth>");
  423. expectResult(ec, expression_evaluate(combineTokens(U"}", U"0"), context), U"<ERROR:Negative expression depth>");
  424. expectResult(ec, expression_evaluate(combineTokens(U"12", U"("), context), U"<ERROR:Unbalanced expression depth>");
  425. expectResult(ec, expression_evaluate(combineTokens(U"2", U")"), context), U"<ERROR:Negative expression depth>");
  426. expectResult(ec, expression_evaluate(combineTokens(U"-5", U"["), context), U"<ERROR:Unbalanced expression depth>");
  427. expectResult(ec, expression_evaluate(combineTokens(U"6", U"]"), context), U"<ERROR:Negative expression depth>");
  428. expectResult(ec, expression_evaluate(combineTokens(U"-47", U"{"), context), U"<ERROR:Unbalanced expression depth>");
  429. expectResult(ec, expression_evaluate(combineTokens(U"645", U"}"), context), U"<ERROR:Negative expression depth>");
  430. expectResult(ec, expression_evaluate(combineTokens(U"5", U")", U"+", U"(", U"-7"), context), U"<ERROR:Negative expression depth>");
  431. printText(U"Completed regression tests of expressions with ", ec, U" errors in total.\n");
  432. }