From 577cd82071ca06b1be8c99b086e00a21a0abc7d4 Mon Sep 17 00:00:00 2001 From: Angelo D <43831545+AngeloD2022@users.noreply.github.com> Date: Wed, 24 May 2023 00:19:30 -0400 Subject: [PATCH] Added syntax simplification logic to AssignmentExpression, LocalAssignmentExpression, and SimpleForStatement regarding shorthand variable updating. --- src/jsxer/nodes/AssignmentExpression.cpp | 19 +++++++++++++++- src/jsxer/nodes/LocalAssignmentExpression.cpp | 19 +++++++++++++++- src/jsxer/nodes/SimpleForStatement.cpp | 22 ++++++++++++++++--- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/jsxer/nodes/AssignmentExpression.cpp b/src/jsxer/nodes/AssignmentExpression.cpp index fff4e94..9bbfbc9 100644 --- a/src/jsxer/nodes/AssignmentExpression.cpp +++ b/src/jsxer/nodes/AssignmentExpression.cpp @@ -12,7 +12,24 @@ namespace jsxer::nodes { if (shorthand) { auto expr = std::dynamic_pointer_cast(expression); string value_assigned = literal.empty() ? expr->get_op() : literal; - return variable->to_string() + ' ' + expr->get_op_name() + "= " + value_assigned; + string op = expr->get_op_name(); + + string result = variable->to_string(); + + bool is_negative = value_assigned[0] == '-'; + + // attempt to simplify the shorthand update expression with + and -... + if (op == "+" || op == "-") { + if (is_negative) { + value_assigned = value_assigned.substr(1); + op = op == "+" ? "-" : "+"; + } + if (value_assigned == "1") { + return result + op + op; + } + } + return result + ' ' + op + "= " + value_assigned; + } else { string value_assigned = literal.empty() ? expression->to_string() : literal; return variable->to_string() + " = " + value_assigned; diff --git a/src/jsxer/nodes/LocalAssignmentExpression.cpp b/src/jsxer/nodes/LocalAssignmentExpression.cpp index 131d6b9..7064af2 100644 --- a/src/jsxer/nodes/LocalAssignmentExpression.cpp +++ b/src/jsxer/nodes/LocalAssignmentExpression.cpp @@ -19,8 +19,25 @@ namespace jsxer::nodes { // FIXME: i have a bad feeling about this one... string value_assigned = literal.empty() ? b->get_op() : literal; + string op = b->get_op_name(); - result += var_name + ' ' + b->get_op_name() + "= " + value_assigned; + result += var_name; + + bool is_negative = value_assigned[0] == '-'; + + // attempt to simplify the shorthand update expression with + and -... + if (op == "+" || op == "-") { + if (is_negative) { + value_assigned = value_assigned.substr(1); + op = op == "+" ? "-" : "+"; + } + if (value_assigned == "1") { + return result + op + op; + } + } + return result + ' ' + op + "= " + value_assigned; + +// result += var_name + ' ' + b->get_op_name() + "= " + value_assigned; } else { string value_assigned = literal.empty() ? expression->to_string() : literal; result += var_name + " = " + value_assigned; diff --git a/src/jsxer/nodes/SimpleForStatement.cpp b/src/jsxer/nodes/SimpleForStatement.cpp index aea1bef..10fb835 100644 --- a/src/jsxer/nodes/SimpleForStatement.cpp +++ b/src/jsxer/nodes/SimpleForStatement.cpp @@ -18,8 +18,24 @@ namespace jsxer::nodes { result += "for (var "; result += varname + " = " + iteratorInitial + "; "; result += varname + ' ' + comparisonOperator + ' ' + upperBound->to_string() + "; "; - result += varname + " += " + stepSize; - result += ") { \n" + bodyInfo.create_body() + '}'; + + result += varname; + + // attempt to simplify the update expression... + string op = "+"; + bool is_negative = stepSize[0] == '-'; + string value_assigned = is_negative ? stepSize.substr(1) : stepSize; + + // we want to alter the operator to be functionally equivalent, such that the step value can be unsigned... + if (is_negative) + op = "-"; + if (value_assigned == "1") + result += op + op; + else + result += " " + op + "= " + value_assigned; + + result += ") {\n" + bodyInfo.create_body() + "\n}"; + return result; } -} \ No newline at end of file +}