Skip to content

Instantly share code, notes, and snippets.

@m0sth8
Created January 18, 2014 10:43
Show Gist options
  • Save m0sth8/8488787 to your computer and use it in GitHub Desktop.
Save m0sth8/8488787 to your computer and use it in GitHub Desktop.
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