Avoid unnecessary parenthesis

This commit is contained in:
konsoletyper 2015-02-11 23:11:58 +04:00
parent 857ed9754e
commit 90f1f19f08
5 changed files with 206 additions and 48 deletions

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public enum Associativity {
LEFT,
RIGHT,
NONE
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2015 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public enum Priority {
COMMA,
ASSIGNMENT,
CONDITIONAL,
LOGICAL_OR,
LOGICAL_AND,
BITWISE_OR,
BITWISE_XOR,
BITWISE_AND,
EQUALITY,
COMPARISON,
BITWISE_SHIFT,
ADDITION,
MULTIPLICATION,
UNARY,
FUNCTION_CALL,
MEMBER_ACCESS,
GROUPING
}

View File

@ -58,6 +58,16 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private Set<MethodReference> asyncFamilyMethods; private Set<MethodReference> asyncFamilyMethods;
private Diagnostics diagnostics; private Diagnostics diagnostics;
private boolean async; private boolean async;
private Priority priority;
private Associativity associativity;
private boolean wasGrouped;
private Deque<OperatorPrecedence> precedenceStack = new ArrayDeque<>();
private static class OperatorPrecedence {
Priority priority;
Associativity associativity;
boolean wasGrouped;
}
@Override @Override
public void visit(MonitorEnterStatement statement) { public void visit(MonitorEnterStatement statement) {
@ -65,11 +75,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
try { try {
MethodReference monitorEnterRef = new MethodReference( MethodReference monitorEnterRef = new MethodReference(
Object.class, "monitorEnter", Object.class, void.class); Object.class, "monitorEnter", Object.class, void.class);
writer.appendMethodBody(monitorEnterRef).append("("); writer.appendMethodBody(monitorEnterRef).append("(");
statement.getObjectRef().acceptVisitor(this); statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }
@ -82,14 +90,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
try { try {
MethodReference monitorExitRef = new MethodReference( MethodReference monitorExitRef = new MethodReference(
Object.class, "monitorExit", Object.class, void.class); Object.class, "monitorExit", Object.class, void.class);
writer.appendMethodBody(monitorExitRef).append("("); writer.appendMethodBody(monitorExitRef).append("(");
statement.getObjectRef().acceptVisitor(this); statement.getObjectRef().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();
} catch (IOException ex){ } catch (IOException ex){
throw new RenderingException("IO error occured", ex); throw new RenderingException("IO error occured", ex);
} }
} }
} }
@ -756,9 +762,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
if (statement.getLeftValue() != null) { if (statement.getLeftValue() != null) {
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getLeftValue().acceptVisitor(this); statement.getLeftValue().acceptVisitor(this);
writer.ws().append("=").ws(); writer.ws().append("=").ws();
} }
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getRightValue().acceptVisitor(this); statement.getRightValue().acceptVisitor(this);
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
writer.append(";").softNewLine(); writer.append(";").softNewLine();
@ -792,6 +802,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
writer.append("if").ws().append("("); writer.append("if").ws().append("(");
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
if (statement.getCondition().getLocation() != null) { if (statement.getCondition().getLocation() != null) {
popLocation(); popLocation();
@ -834,6 +846,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
writer.append("switch").ws().append("("); writer.append("switch").ws().append("(");
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getValue().acceptVisitor(this); statement.getValue().acceptVisitor(this);
if (statement.getValue().getLocation() != null) { if (statement.getValue().getLocation() != null) {
popLocation(); popLocation();
@ -876,6 +890,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("while").ws().append("("); writer.append("while").ws().append("(");
if (statement.getCondition() != null) { if (statement.getCondition() != null) {
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
if (statement.getCondition().getLocation() != null) { if (statement.getCondition().getLocation() != null) {
@ -963,6 +979,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(' '); writer.append(' ');
} }
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getResult().acceptVisitor(this); statement.getResult().acceptVisitor(this);
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
} }
@ -987,6 +1005,8 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
writer.append("$rt_throw("); writer.append("$rt_throw(");
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
priority = Priority.COMMA;
associativity = Associativity.NONE;
statement.getException().acceptVisitor(this); statement.getException().acceptVisitor(this);
writer.append(");").softNewLine(); writer.append(");").softNewLine();
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
@ -1027,16 +1047,17 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
} }
private void visitBinary(BinaryExpr expr, String op) { private void visitBinary(BinaryExpr expr, String op, Priority priority, Associativity associativity) {
try { try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
writer.append('('); enterPriority(priority, associativity == Associativity.LEFT ? associativity : Associativity.NONE, true);
expr.getFirstOperand().acceptVisitor(this); expr.getFirstOperand().acceptVisitor(this);
writer.ws().append(op).ws(); writer.ws().append(op).ws();
this.associativity = associativity == Associativity.RIGHT ? associativity : Associativity.NONE;
expr.getSecondOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this);
writer.append(')'); exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1045,17 +1066,46 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
} }
private void enterPriority(Priority priority, Associativity associativity, boolean autoGroup) throws IOException {
OperatorPrecedence precedence = new OperatorPrecedence();
precedence.wasGrouped = this.wasGrouped;
precedence.priority = this.priority;
precedence.associativity = this.associativity;
precedenceStack.push(precedence);
wasGrouped = false;
if (autoGroup && (priority.ordinal() < this.priority.ordinal() ||
priority.ordinal() == this.priority.ordinal() &&
(associativity != this.associativity || associativity == Associativity.NONE))) {
wasGrouped = true;
writer.append('(');
}
this.priority = priority;
this.associativity = associativity;
}
private void exitPriority() throws IOException {
if (wasGrouped) {
writer.append(')');
}
OperatorPrecedence precedence = precedenceStack.pop();
this.priority = precedence.priority;
this.associativity = precedence.associativity;
this.wasGrouped = precedence.wasGrouped;
}
private void visitBinaryFunction(BinaryExpr expr, String function) { private void visitBinaryFunction(BinaryExpr expr, String function) {
try { try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append(function); writer.append(function);
writer.append('('); writer.append('(');
expr.getFirstOperand().acceptVisitor(this); expr.getFirstOperand().acceptVisitor(this);
writer.append(",").ws(); writer.append(",").ws();
expr.getSecondOperand().acceptVisitor(this); expr.getSecondOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1068,58 +1118,58 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
public void visit(BinaryExpr expr) { public void visit(BinaryExpr expr) {
switch (expr.getOperation()) { switch (expr.getOperation()) {
case ADD: case ADD:
visitBinary(expr, "+"); visitBinary(expr, "+", Priority.ADDITION, Associativity.LEFT);
break; break;
case ADD_LONG: case ADD_LONG:
visitBinaryFunction(expr, "Long_add"); visitBinaryFunction(expr, "Long_add");
break; break;
case SUBTRACT: case SUBTRACT:
visitBinary(expr, "-"); visitBinary(expr, "-", Priority.ADDITION, Associativity.LEFT);
break; break;
case SUBTRACT_LONG: case SUBTRACT_LONG:
visitBinaryFunction(expr, "Long_sub"); visitBinaryFunction(expr, "Long_sub");
break; break;
case MULTIPLY: case MULTIPLY:
visitBinary(expr, "*"); visitBinary(expr, "*", Priority.MULTIPLICATION, Associativity.LEFT);
break; break;
case MULTIPLY_LONG: case MULTIPLY_LONG:
visitBinaryFunction(expr, "Long_mul"); visitBinaryFunction(expr, "Long_mul");
break; break;
case DIVIDE: case DIVIDE:
visitBinary(expr, "/"); visitBinary(expr, "/", Priority.MULTIPLICATION, Associativity.LEFT);
break; break;
case DIVIDE_LONG: case DIVIDE_LONG:
visitBinaryFunction(expr, "Long_div"); visitBinaryFunction(expr, "Long_div");
break; break;
case MODULO: case MODULO:
visitBinary(expr, "%"); visitBinary(expr, "%", Priority.MULTIPLICATION, Associativity.LEFT);
break; break;
case MODULO_LONG: case MODULO_LONG:
visitBinaryFunction(expr, "Long_rem"); visitBinaryFunction(expr, "Long_rem");
break; break;
case EQUALS: case EQUALS:
visitBinary(expr, "=="); visitBinary(expr, "==", Priority.EQUALITY, Associativity.LEFT);
break; break;
case NOT_EQUALS: case NOT_EQUALS:
visitBinary(expr, "!="); visitBinary(expr, "!=", Priority.EQUALITY, Associativity.LEFT);
break; break;
case GREATER: case GREATER:
visitBinary(expr, ">"); visitBinary(expr, ">", Priority.COMPARISON, Associativity.LEFT);
break; break;
case GREATER_OR_EQUALS: case GREATER_OR_EQUALS:
visitBinary(expr, ">="); visitBinary(expr, ">=", Priority.COMPARISON, Associativity.LEFT);
break; break;
case LESS: case LESS:
visitBinary(expr, "<"); visitBinary(expr, "<", Priority.COMPARISON, Associativity.LEFT);
break; break;
case LESS_OR_EQUALS: case LESS_OR_EQUALS:
visitBinary(expr, "<="); visitBinary(expr, "<=", Priority.COMPARISON, Associativity.LEFT);
break; break;
case STRICT_EQUALS: case STRICT_EQUALS:
visitBinary(expr, "==="); visitBinary(expr, "===", Priority.COMPARISON, Associativity.LEFT);
break; break;
case STRICT_NOT_EQUALS: case STRICT_NOT_EQUALS:
visitBinary(expr, "!=="); visitBinary(expr, "!==", Priority.COMPARISON, Associativity.LEFT);
break; break;
case COMPARE: case COMPARE:
visitBinaryFunction(expr, "$rt_compare"); visitBinaryFunction(expr, "$rt_compare");
@ -1128,43 +1178,43 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
visitBinaryFunction(expr, "Long_compare"); visitBinaryFunction(expr, "Long_compare");
break; break;
case OR: case OR:
visitBinary(expr, "||"); visitBinary(expr, "||", Priority.LOGICAL_OR, Associativity.LEFT);
break; break;
case AND: case AND:
visitBinary(expr, "&&"); visitBinary(expr, "&&", Priority.LOGICAL_AND, Associativity.LEFT);
break; break;
case BITWISE_OR: case BITWISE_OR:
visitBinary(expr, "|"); visitBinary(expr, "|", Priority.BITWISE_OR, Associativity.LEFT);
break; break;
case BITWISE_OR_LONG: case BITWISE_OR_LONG:
visitBinaryFunction(expr, "Long_or"); visitBinaryFunction(expr, "Long_or");
break; break;
case BITWISE_AND: case BITWISE_AND:
visitBinary(expr, "&"); visitBinary(expr, "&", Priority.BITWISE_AND, Associativity.LEFT);
break; break;
case BITWISE_AND_LONG: case BITWISE_AND_LONG:
visitBinaryFunction(expr, "Long_and"); visitBinaryFunction(expr, "Long_and");
break; break;
case BITWISE_XOR: case BITWISE_XOR:
visitBinary(expr, "^"); visitBinary(expr, "^", Priority.BITWISE_XOR, Associativity.LEFT);
break; break;
case BITWISE_XOR_LONG: case BITWISE_XOR_LONG:
visitBinaryFunction(expr, "Long_xor"); visitBinaryFunction(expr, "Long_xor");
break; break;
case LEFT_SHIFT: case LEFT_SHIFT:
visitBinary(expr, "<<"); visitBinary(expr, "<<", Priority.BITWISE_SHIFT, Associativity.LEFT);
break; break;
case LEFT_SHIFT_LONG: case LEFT_SHIFT_LONG:
visitBinaryFunction(expr, "Long_shl"); visitBinaryFunction(expr, "Long_shl");
break; break;
case RIGHT_SHIFT: case RIGHT_SHIFT:
visitBinary(expr, ">>"); visitBinary(expr, ">>", Priority.BITWISE_SHIFT, Associativity.LEFT);
break; break;
case RIGHT_SHIFT_LONG: case RIGHT_SHIFT_LONG:
visitBinaryFunction(expr, "Long_shr"); visitBinaryFunction(expr, "Long_shr");
break; break;
case UNSIGNED_RIGHT_SHIFT: case UNSIGNED_RIGHT_SHIFT:
visitBinary(expr, ">>>"); visitBinary(expr, ">>>", Priority.BITWISE_SHIFT, Associativity.LEFT);
break; break;
case UNSIGNED_RIGHT_SHIFT_LONG: case UNSIGNED_RIGHT_SHIFT_LONG:
visitBinaryFunction(expr, "Long_shru"); visitBinaryFunction(expr, "Long_shru");
@ -1180,62 +1230,84 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
switch (expr.getOperation()) { switch (expr.getOperation()) {
case NOT: case NOT:
writer.append("(!"); enterPriority(Priority.UNARY, Associativity.RIGHT, true);
writer.append("!");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); exitPriority();
break; break;
case NEGATE: case NEGATE:
writer.append("(-"); enterPriority(Priority.UNARY, Associativity.RIGHT, true);
writer.append("-");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); exitPriority();
break; break;
case LENGTH: case LENGTH:
enterPriority(Priority.MEMBER_ACCESS, Associativity.LEFT, true);
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
exitPriority();
writer.append(".length"); writer.append(".length");
break; break;
case INT_TO_LONG: case INT_TO_LONG:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("Long_fromInt("); writer.append("Long_fromInt(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
case NUM_TO_LONG: case NUM_TO_LONG:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("Long_fromNumber("); writer.append("Long_fromNumber(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
case LONG_TO_NUM: case LONG_TO_NUM:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("Long_toNumber("); writer.append("Long_toNumber(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
case LONG_TO_INT: case LONG_TO_INT:
enterPriority(Priority.MEMBER_ACCESS, Associativity.LEFT, false);
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
exitPriority();
writer.append(".lo"); writer.append(".lo");
break; break;
case NEGATE_LONG: case NEGATE_LONG:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("Long_neg("); writer.append("Long_neg(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
case NOT_LONG: case NOT_LONG:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("Long_not("); writer.append("Long_not(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
case BYTE_TO_INT: case BYTE_TO_INT:
writer.append("(("); enterPriority(Priority.BITWISE_SHIFT, Associativity.LEFT, true);
writer.append("(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.ws().append("<<").ws().append("24)").ws().append(">>").ws().append("24)"); writer.ws().append("<<").ws().append("24)").ws().append(">>").ws().append("24");
exitPriority();
break; break;
case SHORT_TO_INT: case SHORT_TO_INT:
writer.append("(("); enterPriority(Priority.BITWISE_SHIFT, Associativity.LEFT, true);
writer.append("(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.ws().append("<<").ws().append("16)").ws().append(">>").ws().append("16)"); writer.ws().append("<<").ws().append("16)").ws().append(">>").ws().append("16");
exitPriority();
break; break;
case NULL_CHECK: case NULL_CHECK:
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("$rt_nullCheck("); writer.append("$rt_nullCheck(");
expr.getOperand().acceptVisitor(this); expr.getOperand().acceptVisitor(this);
writer.append(')'); writer.append(')');
exitPriority();
break; break;
} }
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
@ -1252,13 +1324,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
writer.append('('); enterPriority(priority, Associativity.RIGHT, async);
expr.getCondition().acceptVisitor(this); expr.getCondition().acceptVisitor(this);
writer.ws().append("?").ws(); writer.ws().append("?").ws();
expr.getConsequent().acceptVisitor(this); expr.getConsequent().acceptVisitor(this);
writer.ws().append(":").ws(); writer.ws().append(":").ws();
expr.getAlternative().acceptVisitor(this); expr.getAlternative().acceptVisitor(this);
writer.append(')'); exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1429,10 +1501,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
enterPriority(Priority.MEMBER_ACCESS, Associativity.LEFT, true);
expr.getArray().acceptVisitor(this); expr.getArray().acceptVisitor(this);
writer.append('['); writer.append('[');
enterPriority(Priority.COMMA, Associativity.NONE, false);
expr.getIndex().acceptVisitor(this); expr.getIndex().acceptVisitor(this);
exitPriority();
writer.append(']'); writer.append(']');
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1447,11 +1523,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
enterPriority(Priority.MEMBER_ACCESS, Associativity.LEFT, true);
expr.getArray().acceptVisitor(this); expr.getArray().acceptVisitor(this);
writer.append(".data"); writer.append(".data");
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
exitPriority();
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
} }
@ -1483,6 +1561,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
boolean virtual = false; boolean virtual = false;
boolean hasParams = false; boolean hasParams = false;
enterPriority(Priority.COMMA, Associativity.NONE, false);
switch (expr.getType()) { switch (expr.getType()) {
case STATIC: case STATIC:
writer.append(fullName).append("("); writer.append(fullName).append("(");
@ -1536,6 +1615,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("$rt_continue($part_").append(expr.getAsyncTarget()).append(')'); writer.append("$rt_continue($part_").append(expr.getAsyncTarget()).append(')');
} }
writer.append(')'); writer.append(')');
exitPriority();
if (lastCallSite != null) { if (lastCallSite != null) {
if (virtual) { if (virtual) {
lastCallSite.setVirtualMethod(expr.getMethod()); lastCallSite.setVirtualMethod(expr.getMethod());
@ -1562,11 +1642,13 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
enterPriority(Priority.MEMBER_ACCESS, Associativity.LEFT, true);
expr.getQualified().acceptVisitor(this); expr.getQualified().acceptVisitor(this);
writer.append('.').appendField(expr.getField()); writer.append('.').appendField(expr.getField());
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
exitPriority();
} catch (IOException e) { } catch (IOException e) {
throw new RenderingException("IO error occured", e); throw new RenderingException("IO error occured", e);
} }
@ -1578,7 +1660,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
writer.append("new ").append(naming.getNameFor(expr.getConstructedClass())).append("()"); enterPriority(Priority.FUNCTION_CALL, Associativity.RIGHT, true);
writer.append("new ").append(naming.getNameFor(expr.getConstructedClass()));
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1594,6 +1678,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
ValueType type = expr.getType(); ValueType type = expr.getType();
enterPriority(Priority.COMMA, Associativity.NONE, false);
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) { switch (((ValueType.Primitive)type).getKind()) {
case BOOLEAN: case BOOLEAN:
@ -1642,6 +1727,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
} }
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1660,6 +1746,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
for (int i = 0; i < expr.getDimensions().size(); ++i) { for (int i = 0; i < expr.getDimensions().size(); ++i) {
type = ((ValueType.Array)type).getItemType(); type = ((ValueType.Array)type).getItemType();
} }
enterPriority(Priority.COMMA, Associativity.NONE, false);
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) { switch (((ValueType.Primitive)type).getKind()) {
case BOOLEAN: case BOOLEAN:
@ -1703,6 +1790,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
dimension.acceptVisitor(this); dimension.acceptVisitor(this);
} }
writer.append("])"); writer.append("])");
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
@ -1721,18 +1809,21 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
String clsName = ((ValueType.Object)expr.getType()).getClassName(); String clsName = ((ValueType.Object)expr.getType()).getClassName();
ClassHolder cls = classSource.get(clsName); ClassHolder cls = classSource.get(clsName);
if (cls != null && !cls.getModifiers().contains(ElementModifier.INTERFACE)) { if (cls != null && !cls.getModifiers().contains(ElementModifier.INTERFACE)) {
writer.append("("); enterPriority(Priority.COMPARISON, Associativity.LEFT, true);
expr.getExpr().acceptVisitor(this); expr.getExpr().acceptVisitor(this);
writer.append(" instanceof ").appendClass(clsName).append(")"); writer.append(" instanceof ").appendClass(clsName);
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
return; return;
} }
} }
enterPriority(Priority.COMMA, Associativity.NONE, false);
writer.append("$rt_isInstance("); writer.append("$rt_isInstance(");
expr.getExpr().acceptVisitor(this); expr.getExpr().acceptVisitor(this);
writer.append(",").ws().append(typeToClsString(naming, expr.getType())).append(")"); writer.append(",").ws().append(typeToClsString(naming, expr.getType())).append(")");
exitPriority();
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }

View File

@ -234,11 +234,11 @@ class UnusedVariableEliminator implements ExprVisitor, StatementVisitor {
@Override @Override
public void visit(MonitorEnterStatement statement) { public void visit(MonitorEnterStatement statement) {
statement.getObjectRef().acceptVisitor(this);
} }
@Override @Override
public void visit(MonitorExitStatement statement) { public void visit(MonitorExitStatement statement) {
statement.getObjectRef().acceptVisitor(this);
} }
} }

View File

@ -405,6 +405,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
Renderer renderer = new Renderer(sourceWriter, classSet, classLoader, this, asyncMethods, asyncFamilyMethods, Renderer renderer = new Renderer(sourceWriter, classSet, classLoader, this, asyncMethods, asyncFamilyMethods,
diagnostics); diagnostics);
renderer.setProperties(properties); renderer.setProperties(properties);
renderer.setMinifying(minifying);
if (debugEmitter != null) { if (debugEmitter != null) {
int classIndex = 0; int classIndex = 0;
for (String className : classSet.getClassNames()) { for (String className : classSet.getClassNames()) {