📄 constantexpression.jrag
字号:
/* * The JastAdd Extensible Java Compiler (http://jastadd.org) is covered * by the modified BSD License. You should have received a copy of the * modified BSD license with this compiler. * * Copyright (c) 2005-2008, Torbjorn Ekman * All rights reserved. */aspect ConstantExpression { class Constant { static class ConstantInt extends Constant { private int value; public ConstantInt(int i) { this.value = i; } int intValue() { return value; } long longValue() { return value; } float floatValue() { return value; } double doubleValue() { return value; } String stringValue() { return new Integer(value).toString(); } Literal buildLiteral() { return new IntegerLiteral(stringValue()); } } static class ConstantLong extends Constant { private long value; public ConstantLong(long l) { this.value = l; } int intValue() { return (int)value; } long longValue() { return value; } float floatValue() { return value; } double doubleValue() { return value; } String stringValue() { return new Long(value).toString(); } Literal buildLiteral() { return new LongLiteral(stringValue()); } } static class ConstantFloat extends Constant { private float value; public ConstantFloat(float f) { this.value = f; } int intValue() { return (int)value; } long longValue() { return (long)value; } float floatValue() { return value; } double doubleValue() { return value; } String stringValue() { return new Float(value).toString(); } Literal buildLiteral() { return new FloatingPointLiteral(stringValue()); } } static class ConstantDouble extends Constant { private double value; public ConstantDouble(double d) { this.value = d; } int intValue() { return (int)value; } long longValue() { return (long)value; } float floatValue() { return (float)value; } double doubleValue() { return value; } String stringValue() { return new Double(value).toString(); } Literal buildLiteral() { return new DoubleLiteral(stringValue()); } } static class ConstantChar extends Constant { private char value; public ConstantChar(char c) { this.value = c; } int intValue() { return value; } long longValue() { return value; } float floatValue() { return value; } double doubleValue() { return value; } String stringValue() { return new Character(value).toString(); } Literal buildLiteral() { return new CharacterLiteral(stringValue()); } } static class ConstantBoolean extends Constant { private boolean value; public ConstantBoolean(boolean b) { this.value = b; } boolean booleanValue() { return value; } String stringValue() { return new Boolean(value).toString(); } Literal buildLiteral() { return new BooleanLiteral(stringValue()); } } static class ConstantString extends Constant { private String value; public ConstantString(String s) { this.value = s; } String stringValue() { return value; } Literal buildLiteral() { return new StringLiteral(stringValue()); } } int intValue() { throw new UnsupportedOperationException(); } long longValue() { throw new UnsupportedOperationException(); } float floatValue() { throw new UnsupportedOperationException(); } double doubleValue() { throw new UnsupportedOperationException(); } boolean booleanValue() { throw new UnsupportedOperationException(getClass().getName()); } String stringValue() { throw new UnsupportedOperationException(); } Literal buildLiteral() { throw new UnsupportedOperationException(); } protected Constant() { } public boolean error = false; static Constant create(int i) { return new ConstantInt(i); } static Constant create(long l) { return new ConstantLong(l); } static Constant create(float f) { return new ConstantFloat(f); } static Constant create(double d) { return new ConstantDouble(d); } static Constant create(boolean b) { return new ConstantBoolean(b); } static Constant create(char c) { return new ConstantChar(c); } static Constant create(String s) { return new ConstantString(s); } } syn Constant Expr.constant() { throw new UnsupportedOperationException("ConstantExpression operation constant" + " not supported for type " + getClass().getName()); } // enable caching for Literal constants syn lazy Constant Literal.constant() { throw new UnsupportedOperationException("ConstantExpression operation constant" + " not supported for type " + getClass().getName()); } eq VarAccess.constant() = type().cast(decl().getInit().constant()); eq AbstractDot.constant() = lastAccess().constant(); eq CastExpr.constant() = type().cast(getExpr().constant()); eq ParExpr.constant() = getExpr().constant(); eq PlusExpr.constant() = type().plus(getOperand().constant()); eq MinusExpr.constant() = type().minus(getOperand().constant()); eq BitNotExpr.constant() = type().bitNot(getOperand().constant()); eq MulExpr.constant() = type().mul(getLeftOperand().constant(), getRightOperand().constant()); eq DivExpr.constant() = type().div(getLeftOperand().constant(), getRightOperand().constant()); eq ModExpr.constant() = type().mod(getLeftOperand().constant(), getRightOperand().constant()); eq AddExpr.constant() = type().add(getLeftOperand().constant(), getRightOperand().constant()); eq SubExpr.constant() = type().sub(getLeftOperand().constant(), getRightOperand().constant()); eq LShiftExpr.constant() = type().lshift(getLeftOperand().constant(), getRightOperand().constant()); eq RShiftExpr.constant() = type().rshift(getLeftOperand().constant(), getRightOperand().constant()); eq URShiftExpr.constant() = type().urshift(getLeftOperand().constant(), getRightOperand().constant()); eq AndBitwiseExpr.constant() = type().andBitwise(getLeftOperand().constant(), getRightOperand().constant()); eq XorBitwiseExpr.constant() = type().xorBitwise(getLeftOperand().constant(), getRightOperand().constant()); eq OrBitwiseExpr.constant() = type().orBitwise(getLeftOperand().constant(), getRightOperand().constant()); syn lazy Constant ConditionalExpr.constant() = type().questionColon(getCondition().constant(), getTrueExpr().constant(),getFalseExpr().constant()); syn lazy boolean FloatingPointLiteral.isZero() { String s = getLITERAL(); for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c == 'E' || c == 'e') break; if(Character.isDigit(c) && c != '0') { return false; } } return true; } syn lazy boolean DoubleLiteral.isZero() { String s = getLITERAL(); for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c == 'E' || c == 'e') break; if(Character.isDigit(c) && c != '0') { return false; } } return true; } static long Literal.parseLong(String s) { long x = 0L; s = s.toLowerCase(); int radix = 10; boolean neg = false; if(s.startsWith("0x")) { radix = 16; s = s.substring(2); if(s.length() > 16) { for(int i = 0; i < s.length()-16; i++) if(s.charAt(i) != '0') throw new NumberFormatException(""); } } else if(s.startsWith("0")) { radix = 8; s = s.substring(1); // Octals larger than 01777777777777777777777L are not valid if(s.length() > 21) { for(int i = 0; i < s.length() - 21; i++) if(i == s.length() - 21 - 1) { if(s.charAt(i) != '0' && s.charAt(i) != '1') throw new NumberFormatException(""); } else { if(s.charAt(i) != '0') throw new NumberFormatException(""); } } } else if(s.startsWith("-")) { s = s.substring(1); neg = true; } long oldx = 0; for (int i = 0; i < s.length(); i++) { int c = s.charAt(i); if (c < '0' || c > '9') { c = c - 'a' + 10; } else { c = c - '0'; } x *= radix; x += c; if(x < oldx && radix == 10) { boolean negMinValue = i == (s.length()-1) && neg && x == Long.MIN_VALUE; if(!negMinValue) throw new NumberFormatException(""); } oldx = x; } if(radix == 10 && x == Long.MIN_VALUE) return x; if(radix == 10 && x < 0) { throw new NumberFormatException(""); } return neg ? -x : x; } syn boolean IntegerLiteral.isHex() = getLITERAL().toLowerCase().startsWith("0x"); syn boolean IntegerLiteral.isOctal() = getLITERAL().startsWith("0"); syn boolean IntegerLiteral.isDecimal() = !isHex() && !isOctal(); syn boolean LongLiteral.isHex() = getLITERAL().toLowerCase().startsWith("0x"); syn boolean LongLiteral.isOctal() = getLITERAL().startsWith("0"); syn boolean LongLiteral.isDecimal() = !isHex() && !isOctal(); syn boolean Expr.isPositive() = false; eq IntegerLiteral.isPositive() = !getLITERAL().startsWith("-"); eq LongLiteral.isPositive() = !getLITERAL().startsWith("-"); rewrite MinusExpr { when(getOperand() instanceof IntegerLiteral && ((IntegerLiteral)getOperand()).isDecimal() && getOperand().isPositive()) to IntegerLiteral new IntegerLiteral("-" + ((IntegerLiteral)getOperand()).getLITERAL()); } rewrite MinusExpr { when(getOperand() instanceof LongLiteral && ((LongLiteral)getOperand()).isDecimal() && getOperand().isPositive()) to LongLiteral new LongLiteral("-" + ((LongLiteral)getOperand()).getLITERAL()); } eq IntegerLiteral.constant() { long l = 0; try { l = Literal.parseLong(getLITERAL()); } catch (NumberFormatException e) { } Constant c = Constant.create((int)l); if(isDecimal() && l != (int)l) c.error = true; if(isOctal() && l > 037777777777L) c.error = true; if(isHex() && l > 0xffffffffL) c.error = true; return c; } eq LongLiteral.constant() { try { return Constant.create(Literal.parseLong(getLITERAL())); } catch (NumberFormatException e) { Constant c = Constant.create(0L); c.error = true; return c; } } eq FloatingPointLiteral.constant() { try { return Constant.create(Float.parseFloat(getLITERAL())); } catch (NumberFormatException e) { Constant c = Constant.create(0.0f); c.error = true; return c; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -