Skip to content

Commit

Permalink
Merge pull request #114
Browse files Browse the repository at this point in the history
Added syntax simplification logic to shorthand updates.
  • Loading branch information
AngeloD2022 authored Jun 5, 2023
2 parents 3bda576 + 577cd82 commit 6ea405f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/jsxer/nodes/AssignmentExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ namespace jsxer::nodes {
if (shorthand) {
auto expr = std::dynamic_pointer_cast<BinaryExpression>(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;
Expand Down
19 changes: 18 additions & 1 deletion src/jsxer/nodes/LocalAssignmentExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 19 additions & 3 deletions src/jsxer/nodes/SimpleForStatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 6ea405f

Please sign in to comment.