Created
January 18, 2014 10:43
-
-
Save m0sth8/8488787 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Double getNumericValueFromLiteralExpr(GoExpr expr) { | |
if (expr instanceof GoLiteralExpression){ | |
GoLiteral literal = ((GoLiteralExpression) expr).getLiteral(); | |
if (literal instanceof GoLiteralIdentifier){ | |
if (((GoLiteralIdentifier) literal).isIota()){ | |
Integer iotaValue = ((GoLiteralIdentifier) literal).getIotaValue(); | |
if (iotaValue != null) | |
return iotaValue.doubleValue(); | |
} else { | |
PsiElement goConstIdentifier = GoUtil.ResolveReferece(literal); | |
PsiElement goConstSpec = goConstIdentifier.getParent(); | |
if (goConstSpec instanceof GoConstDeclaration) { | |
GoExpr goConstExpr = ((GoConstDeclaration) goConstSpec).getExpression((GoLiteralIdentifier) goConstIdentifier); | |
if (goConstExpr != null) | |
return getNumericValueFromLiteralExpr(goConstExpr); | |
} | |
} | |
} | |
if (literal instanceof GoLiteralInteger) { | |
Integer value = ((GoLiteralInteger) literal).getValue(); | |
return value.doubleValue(); | |
} | |
if (literal instanceof GoLiteralFloat) { | |
Float value = ((GoLiteralFloat) literal).getValue(); | |
return value.doubleValue(); | |
} | |
if (literal.getNode().getElementType() == GoElementTypes.LITERAL_CHAR){ | |
Integer value = GoPsiUtils.getRuneValue(literal.getText()); | |
if (value != null){ | |
return value.doubleValue(); | |
} | |
} | |
} | |
if (expr instanceof GoBinaryExpression){ | |
GoExpr leftOp = ((GoBinaryExpression) expr).getLeftOperand(); | |
GoExpr rightOp = ((GoBinaryExpression) expr).getRightOperand(); | |
IElementType op = ((GoBinaryExpression) expr).getOperator(); | |
if (op == GoElementTypes.oPLUS || op == GoElementTypes.oMINUS | |
|| op == GoElementTypes.oMUL || op == GoElementTypes.oQUOTIENT | |
|| op == GoElementTypes.oSHIFT_LEFT || op == GoElementTypes.oSHIFT_RIGHT){ | |
Double leftVal = getNumericValueFromLiteralExpr(leftOp); | |
if (leftVal != null){ | |
Double rightVal = getNumericValueFromLiteralExpr(rightOp); | |
if (rightVal != null){ | |
if (op == GoElementTypes.oPLUS) | |
return leftVal + rightVal; | |
if (op == GoElementTypes.oMINUS) | |
return leftVal - rightVal; | |
if (op == GoElementTypes.oMUL) | |
return leftVal * rightVal; | |
if (op == GoElementTypes.oQUOTIENT && rightVal != 0) | |
return leftVal / rightVal; | |
// if (op == GoElementTypes.oSHIFT_LEFT) | |
// return leftVal << rightVal; | |
// if (op == GoElementTypes.oSHIFT_RIGHT) | |
// return leftVal >> rightVal; | |
} | |
} | |
} | |
} | |
if (expr instanceof GoUnaryExpression){ | |
GoUnaryExpression.Op unaryOp = ((GoUnaryExpression) expr).getUnaryOp(); | |
GoExpr unaryExpr = ((GoUnaryExpression) expr).getExpression(); | |
if (unaryOp == GoUnaryExpression.Op.None || unaryOp == GoUnaryExpression.Op.Plus | |
|| unaryOp == GoUnaryExpression.Op.Minus || unaryOp == GoUnaryExpression.Op.Xor) { | |
Double unaryVal = getNumericValueFromLiteralExpr(unaryExpr); | |
if (unaryVal != null) { | |
if (unaryOp == GoUnaryExpression.Op.Minus){ | |
return -unaryVal; | |
} | |
// if (unaryOp == GoUnaryExpression.Op.Xor){ | |
// unaryVal = ~unaryVal; | |
// } | |
} | |
return unaryVal; | |
} | |
} | |
if (expr instanceof GoParenthesisedExpression) | |
return getNumericValueFromLiteralExpr(((GoParenthesisedExpression) expr).getInnerExpression()); | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment