Skip to content

Athenizer use cases examples

Yuval Simon edited this page Jun 2, 2017 · 7 revisions

Many times we find ourselves struggling with some complicated, hard for understanding code - not with the Athenizer!
Using the Athenizer would convert some code to much more readable equivalent code.
Here are a few examples of complicated original code and much simpler athenized code:


Original code:

return precedence.greater(host, inner) || precedence.equal(host, inner) || simple(inner) ? inner : parenthesize(inner);

Would be converted by the Athenizer to:

if (precedence.greater(host, inner) || precedence.equal(host, inner) || simple(inner)) {
    return inner;
}
ParenthesizedExpression x1;
x1 = parenthesize(inner);
return x1;

Original code:

return iz.nodeTypeEquals($, PREFIX_EXPRESSION) ? peel((PrefixExpression) $)
    : iz.nodeTypeEquals($, PARENTHESIZED_EXPRESSION) ? peel(core($))
        : iz.nodeTypeEquals($, INFIX_EXPRESSION) ? peel((InfixExpression) $)
            : iz.nodeTypeEquals($, NUMBER_LITERAL) ? peel((NumberLiteral) $)
                : $;

Would be converted by the Athenizer to:

if (iz.nodeTypeEquals($, PREFIX_EXPRESSION)) {
  Expression x1;
  x1 = peel((PrefixExpression) $);
  return x1;
}
if (iz.nodeTypeEquals($, PARENTHESIZED_EXPRESSION)) {
  Expression x2;
  Expression x1;
  x1 = core($);
  x2 = peel(x1);
  return x2;
}
if (iz.nodeTypeEquals($, INFIX_EXPRESSION)) {
  Expression x3;
  x3 = peel((InfixExpression) $);
  return x3;
}
if (iz.nodeTypeEquals($, NUMBER_LITERAL)) {
  Expression x4;
  x4 = peel((NumberLiteral) $);
  return x4;
}
return $;

Original code:

final InfixExpression $ = ast.newInfixExpression();
$.setOperator(¢);
$.setLeftOperand(make.plant(left).intoLeft($));
$.setRightOperand(¢ != op.PLUS2 ? make.plant(right).into($)
    : !precedence.greater($, right)
        && !precedence.equal($, right)
        && !iz.simple(right) ? subject.operand(right).parenthesis() : right);

After a few applications:

final InfixExpression $;
$ = ast.newInfixExpression();
$.setOperator(¢);
Expression x1;
PlantingExpression x2;
x2 = make.plant(left);
x1 = x2.intoLeft($);
$.setLeftOperand(x1);
Expression x3 = ¢ != op.PLUS2 ? make.plant(right).into($)
    : !precedence.greater($, right) && !precedence.equal($, right) && !iz.simple(right) ? subject.operand(right).parenthesis() : right;
$.setRightOperand(x3);

After few more applications we would end up with:

final InfixExpression $;
$ = ast.newInfixExpression();
$.setOperator(¢);
Expression x1;
PlantingExpression x2;
x2 = make.plant(left);
x1 = x2.intoLeft($);
$.setLeftOperand(x1);
Expression x3;
if (¢ != op.PLUS2) {
  PlantingExpression x4;
  x4 = make.plant(right);
  x3 = x4.into($);
} else {
  if (!precedence.greater($, right) && !precedence.equal($, right) && !iz.simple(right)) {
    x3 = subject.operand(right).parenthesis();
  } else {
    x3 = right;
  }
}
$.setRightOperand(x3);

Clone this wiki locally