Skip to content

Commit e4e9355

Browse files
author
zhourenjian@gmail.com
committed
Fixing bug that char arguments should be marked as String arguments
Improving compiling char comparison and other operations
1 parent eac6fde commit e4e9355

File tree

2 files changed

+82
-26
lines changed

2 files changed

+82
-26
lines changed

src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -846,13 +846,13 @@ public boolean visit(PostfixExpression node) {
846846
}
847847
return false;
848848
}
849-
ITypeBinding typeBinding = node.getOperand().resolveTypeBinding();
849+
ITypeBinding typeBinding = left.resolveTypeBinding();
850850
if (typeBinding != null && typeBinding.isPrimitive()) {
851851
if ("char".equals(typeBinding.getName())) {
852852
buffer.append("(");
853-
node.getOperand().accept(this);
853+
left.accept(this);
854854
buffer.append(" = String.fromCharCode (($c$ = ");
855-
node.getOperand().accept(this);
855+
left.accept(this);
856856
String op = node.getOperator().toString();
857857
if ("++".equals(op)) {
858858
buffer.append(").charCodeAt (0) + 1)");
@@ -863,7 +863,7 @@ public boolean visit(PostfixExpression node) {
863863
return false;
864864
}
865865
}
866-
boxingNode(node.getOperand());
866+
boxingNode(left);
867867
return false;
868868
//return super.visit(node);
869869
}
@@ -971,24 +971,30 @@ public boolean visit(PrefixExpression node) {
971971
}
972972
return false;
973973
}
974-
ITypeBinding typeBinding = node.getOperand().resolveTypeBinding();
974+
ITypeBinding typeBinding = left.resolveTypeBinding();
975975
if (typeBinding.isPrimitive()) {
976976
if ("char".equals(typeBinding.getName())) {
977977
buffer.append("(");
978-
node.getOperand().accept(this);
979-
buffer.append(" = String.fromCharCode ((");
980-
node.getOperand().accept(this);
978+
left.accept(this);
979+
buffer.append(" = String.fromCharCode (");
980+
if (left instanceof SimpleName || left instanceof QualifiedName) {
981+
left.accept(this);
982+
} else {
983+
buffer.append("(");
984+
left.accept(this);
985+
buffer.append(")");
986+
}
981987
if ("++".equals(op)) {
982-
buffer.append(").charCodeAt (0) + 1)");
988+
buffer.append(".charCodeAt (0) + 1)");
983989
} else {
984-
buffer.append(").charCodeAt (0) - 1)");
990+
buffer.append(".charCodeAt (0) - 1)");
985991
}
986992
buffer.append(")");
987993
return false;
988994
}
989995
}
990996
buffer.append(node.getOperator());
991-
boxingNode(node.getOperand());
997+
boxingNode(left);
992998
return false;
993999
}
9941000

src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.eclipse.jdt.core.dom.ArrayType;
2020
import org.eclipse.jdt.core.dom.Block;
2121
import org.eclipse.jdt.core.dom.CastExpression;
22+
import org.eclipse.jdt.core.dom.CharacterLiteral;
2223
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
2324
import org.eclipse.jdt.core.dom.ConstructorInvocation;
2425
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
@@ -434,8 +435,9 @@ public boolean visit(CastExpression node) {
434435
* TODO: some casting should have its meaning!
435436
* int to byte, int to short, long to int will lost values
436437
*/
438+
Expression expression = node.getExpression();
437439
if (type.isPrimitiveType()) {
438-
ITypeBinding resolveTypeBinding = node.getExpression().resolveTypeBinding();
440+
ITypeBinding resolveTypeBinding = expression.resolveTypeBinding();
439441
if(resolveTypeBinding != null){
440442
String name = resolveTypeBinding.getName();
441443
PrimitiveType pType = (PrimitiveType) type;
@@ -445,12 +447,12 @@ public boolean visit(CastExpression node) {
445447
|| pType.getPrimitiveTypeCode() == PrimitiveType.LONG) {
446448
if ("char".equals(name)) {
447449
buffer.append("(");
448-
node.getExpression().accept(this);
450+
expression.accept(this);
449451
buffer.append (").charCodeAt (0)");
450452
return false;
451453
} else if ("float".equals(name) || "double".equals(name)) {
452454
buffer.append("Math.round (");
453-
node.getExpression().accept(this);
455+
expression.accept(this);
454456
buffer.append (")");
455457
return false;
456458
}
@@ -465,22 +467,46 @@ public boolean visit(CastExpression node) {
465467
// TODO:
466468
buffer.append("String.fromCharCode (");
467469
buffer.append("Math.round (");
468-
node.getExpression().accept(this);
470+
expression.accept(this);
469471
buffer.append (")");
470472
buffer.append (")");
471473
return false;
472474
} else if ("int".equals(name) || "byte".equals(name)
473-
|| "double".equals(name) || "float".equals(name)
475+
// || "double".equals(name) || "float".equals(name)
474476
|| "short".equals(name) || "long".equals(name)) {
477+
Object constantValue = expression.resolveConstantExpressionValue();
478+
if (constantValue != null) {
479+
if (constantValue instanceof Integer) {
480+
int value = ((Integer) constantValue).intValue();
481+
if ((value >= '0' && value <= '9')
482+
|| (value >= 'A' && value <= 'Z')
483+
|| (value >= 'a' && value <= 'z')) {
484+
buffer.append('\'');
485+
buffer.append((char) value);
486+
buffer.append('\'');
487+
return false;
488+
}
489+
} else if (constantValue instanceof Long) {
490+
long value = ((Long) constantValue).longValue();
491+
if ((value >= '0' && value <= '9')
492+
|| (value >= 'A' && value <= 'Z')
493+
|| (value >= 'a' && value <= 'z')) {
494+
buffer.append('\'');
495+
buffer.append((char) value);
496+
buffer.append('\'');
497+
return false;
498+
}
499+
}
500+
}
475501
buffer.append("String.fromCharCode (");
476-
node.getExpression().accept(this);
502+
expression.accept(this);
477503
buffer.append (")");
478504
return false;
479505
}
480506
}
481507
}
482508
}
483-
node.getExpression().accept(this);
509+
expression.accept(this);
484510
return false;
485511
}
486512

@@ -1295,9 +1321,17 @@ private void charVisit(ASTNode node, boolean beCare) {
12951321
}
12961322
ITypeBinding binding = ((Expression) node).resolveTypeBinding();
12971323
if (binding.isPrimitive() && "char".equals(binding.getName())) {
1298-
buffer.append("(");
1299-
boxingNode(node);
1300-
buffer.append(").charCodeAt (0)");
1324+
if (node instanceof CharacterLiteral) {
1325+
CharacterLiteral cl = (CharacterLiteral) node;
1326+
buffer.append(0 + cl.charValue());
1327+
} else if (node instanceof SimpleName || node instanceof QualifiedName) {
1328+
boxingNode(node);
1329+
buffer.append(".charCodeAt (0)");
1330+
} else {
1331+
buffer.append("(");
1332+
boxingNode(node);
1333+
buffer.append(").charCodeAt (0)");
1334+
}
13011335
} else {
13021336
boxingNode(node);
13031337
}
@@ -1316,11 +1350,25 @@ public boolean visit(InfixExpression node) {
13161350
}
13171351
String operator = node.getOperator().toString();
13181352
Expression left = node.getLeftOperand();
1353+
Expression right = node.getRightOperand();
13191354
ITypeBinding typeBinding = left.resolveTypeBinding();
1355+
1356+
if ((left instanceof SimpleName || left instanceof CharacterLiteral) && (right instanceof SimpleName || right instanceof CharacterLiteral)
1357+
&& (">".equals(operator) || "<".equals(operator) || ">=".equals(operator) || "<=".equals(operator))) {
1358+
ITypeBinding rightBinding = right.resolveTypeBinding();
1359+
if (typeBinding.isPrimitive() && "char".equals(typeBinding.getName())
1360+
&& rightBinding.isPrimitive() && "char".equals(rightBinding.getName())) {
1361+
boxingNode(left);
1362+
buffer.append(' ');
1363+
buffer.append(operator);
1364+
buffer.append(' ');
1365+
boxingNode(right);
1366+
return false;
1367+
}
1368+
}
13201369
if ("/".equals(operator)) {
13211370
if (typeBinding != null && typeBinding.isPrimitive()) {
13221371
if (isIntegerType(typeBinding.getName())) {
1323-
Expression right = node.getRightOperand();
13241372
ITypeBinding rightTypeBinding = right.resolveTypeBinding();
13251373
if (isIntegerType(rightTypeBinding.getName())) {
13261374
StringBuffer tmpBuffer = buffer;
@@ -1375,20 +1423,20 @@ public boolean visit(InfixExpression node) {
13751423
}
13761424
}
13771425

1378-
charVisit(node.getLeftOperand(), beCare);
1426+
charVisit(left, beCare);
13791427
buffer.append(' ');
13801428
buffer.append(operator);
13811429
if ("==".equals(operator) || "!=".equals(operator)) {
13821430
if (typeBinding != null && !typeBinding.isPrimitive()
1383-
&& !(node.getLeftOperand() instanceof NullLiteral)
1384-
&& !(node.getRightOperand() instanceof NullLiteral)
1431+
&& !(left instanceof NullLiteral)
1432+
&& !(right instanceof NullLiteral)
13851433
/*&& !(node.getLeftOperand() instanceof StringLiteral) // "abc" == ...
13861434
&& !(node.getRightOperand() instanceof StringLiteral)*/) {
13871435
buffer.append('=');
13881436
}
13891437
}
13901438
buffer.append(' ');
1391-
charVisit(node.getRightOperand(), beCare);
1439+
charVisit(right, beCare);
13921440
List extendedOperands = node.extendedOperands();
13931441
if (extendedOperands.size() > 0) {
13941442
for (Iterator iter = extendedOperands.iterator(); iter.hasNext();) {
@@ -1742,6 +1790,8 @@ public boolean visit(MethodDeclaration node) {
17421790
PrimitiveType pType = (PrimitiveType) type;
17431791
if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
17441792
buffer.append("~B"); // Boolean
1793+
} else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) {
1794+
buffer.append("~S"); // String for char
17451795
} else {
17461796
buffer.append("~N"); // Number
17471797
}

0 commit comments

Comments
 (0)