org.extendj.ast.FloatingPointLiteral Maven / Gradle / Ivy
/* This file was generated with JastAdd2 (http://jastadd.org) version 2.3.0 */
package org.extendj.ast;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.ArrayList;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.zip.*;
import java.io.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import org.jastadd.util.PrettyPrintable;
import org.jastadd.util.PrettyPrinter;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.jastadd.util.*;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import beaver.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* Java floating point literal. Can store any value representable as an
* IEEE 754 32-bit single-precision floating point number.
* @ast node
* @declaredat /home/jesper/git/extendj/java4/grammar/Literals.ast:47
* @astdecl FloatingPointLiteral : Literal;
* @production FloatingPointLiteral : {@link Literal};
*/
public class FloatingPointLiteral extends Literal implements Cloneable {
/**
* @aspect Java4PrettyPrint
* @declaredat /home/jesper/git/extendj/java4/frontend/PrettyPrint.jadd:386
*/
public void prettyPrint(PrettyPrinter out) {
out.print(getLITERAL());
}
/**
* @aspect CodeGeneration
* @declaredat /home/jesper/git/extendj/java4/backend/CodeGeneration.jrag:99
*/
public static void push(CodeGeneration gen, float value) {
gen.FCONST(value);
}
/**
* @aspect CodeGeneration
* @declaredat /home/jesper/git/extendj/java4/backend/CodeGeneration.jrag:129
*/
public void emitPushConstant(CodeGeneration gen) {
FloatingPointLiteral.push(gen, constant().floatValue());
}
/**
* @declaredat ASTNode:1
*/
public FloatingPointLiteral() {
super();
}
/**
* Initializes the child array to the correct size.
* Initializes List and Opt nta children.
* @apilevel internal
* @ast method
* @declaredat ASTNode:10
*/
public void init$Children() {
}
/**
* @declaredat ASTNode:12
*/
@ASTNodeAnnotation.Constructor(
name = {"LITERAL"},
type = {"String"},
kind = {"Token"}
)
public FloatingPointLiteral(String p0) {
setLITERAL(p0);
}
/**
* @declaredat ASTNode:20
*/
public FloatingPointLiteral(beaver.Symbol p0) {
setLITERAL(p0);
}
/** @apilevel low-level
* @declaredat ASTNode:24
*/
protected int numChildren() {
return 0;
}
/**
* @apilevel internal
* @declaredat ASTNode:30
*/
public boolean mayHaveRewrite() {
return false;
}
/** @apilevel internal
* @declaredat ASTNode:34
*/
public void flushAttrCache() {
super.flushAttrCache();
type_reset();
isZero_reset();
constant_reset();
}
/** @apilevel internal
* @declaredat ASTNode:41
*/
public void flushCollectionCache() {
super.flushCollectionCache();
}
/** @apilevel internal
* @declaredat ASTNode:45
*/
public FloatingPointLiteral clone() throws CloneNotSupportedException {
FloatingPointLiteral node = (FloatingPointLiteral) super.clone();
return node;
}
/** @apilevel internal
* @declaredat ASTNode:50
*/
public FloatingPointLiteral copy() {
try {
FloatingPointLiteral node = (FloatingPointLiteral) clone();
node.parent = null;
if (children != null) {
node.children = (ASTNode[]) children.clone();
}
return node;
} catch (CloneNotSupportedException e) {
throw new Error("Error: clone not supported for " + getClass().getName());
}
}
/**
* Create a deep copy of the AST subtree at this node.
* The copy is dangling, i.e. has no parent.
* @return dangling copy of the subtree at this node
* @apilevel low-level
* @deprecated Please use treeCopy or treeCopyNoTransform instead
* @declaredat ASTNode:69
*/
@Deprecated
public FloatingPointLiteral fullCopy() {
return treeCopyNoTransform();
}
/**
* Create a deep copy of the AST subtree at this node.
* The copy is dangling, i.e. has no parent.
* @return dangling copy of the subtree at this node
* @apilevel low-level
* @declaredat ASTNode:79
*/
public FloatingPointLiteral treeCopyNoTransform() {
FloatingPointLiteral tree = (FloatingPointLiteral) copy();
if (children != null) {
for (int i = 0; i < children.length; ++i) {
ASTNode child = (ASTNode) children[i];
if (child != null) {
child = child.treeCopyNoTransform();
tree.setChild(child, i);
}
}
}
return tree;
}
/**
* Create a deep copy of the AST subtree at this node.
* The subtree of this node is traversed to trigger rewrites before copy.
* The copy is dangling, i.e. has no parent.
* @return dangling copy of the subtree at this node
* @apilevel low-level
* @declaredat ASTNode:99
*/
public FloatingPointLiteral treeCopy() {
FloatingPointLiteral tree = (FloatingPointLiteral) copy();
if (children != null) {
for (int i = 0; i < children.length; ++i) {
ASTNode child = (ASTNode) getChild(i);
if (child != null) {
child = child.treeCopy();
tree.setChild(child, i);
}
}
}
return tree;
}
/** @apilevel internal
* @declaredat ASTNode:113
*/
protected boolean is$Equal(ASTNode node) {
return super.is$Equal(node) && (tokenString_LITERAL == ((FloatingPointLiteral) node).tokenString_LITERAL);
}
/**
* Replaces the lexeme LITERAL.
* @param value The new value for the lexeme LITERAL.
* @apilevel high-level
*/
public void setLITERAL(String value) {
tokenString_LITERAL = value;
}
/**
* JastAdd-internal setter for lexeme LITERAL using the Beaver parser.
* @param symbol Symbol containing the new value for the lexeme LITERAL
* @apilevel internal
*/
public void setLITERAL(beaver.Symbol symbol) {
if (symbol.value != null && !(symbol.value instanceof String))
throw new UnsupportedOperationException("setLITERAL is only valid for String lexemes");
tokenString_LITERAL = (String)symbol.value;
LITERALstart = symbol.getStart();
LITERALend = symbol.getEnd();
}
/**
* Retrieves the value for the lexeme LITERAL.
* @return The value for the lexeme LITERAL.
* @apilevel high-level
*/
@ASTNodeAnnotation.Token(name="LITERAL")
public String getLITERAL() {
return tokenString_LITERAL != null ? tokenString_LITERAL : "";
}
/** @apilevel internal */
private void type_reset() {
type_computed = null;
type_value = null;
}
/** @apilevel internal */
protected ASTState.Cycle type_computed = null;
/** @apilevel internal */
protected TypeDecl type_value;
/**
* @attribute syn
* @aspect TypeAnalysis
* @declaredat /home/jesper/git/extendj/java4/frontend/TypeAnalysis.jrag:296
*/
@ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
@ASTNodeAnnotation.Source(aspect="TypeAnalysis", declaredAt="/home/jesper/git/extendj/java4/frontend/TypeAnalysis.jrag:296")
public TypeDecl type() {
ASTState state = state();
if (type_computed == ASTState.NON_CYCLE || type_computed == state().cycle()) {
return type_value;
}
type_value = typeFloat();
if (state().inCircle()) {
type_computed = state().cycle();
} else {
type_computed = ASTState.NON_CYCLE;
}
return type_value;
}
/** @apilevel internal */
private void isZero_reset() {
isZero_computed = null;
}
/** @apilevel internal */
protected ASTState.Cycle isZero_computed = null;
/** @apilevel internal */
protected boolean isZero_value;
/**
* @attribute syn
* @aspect Literals
* @declaredat /home/jesper/git/extendj/java4/frontend/Literals.jrag:37
*/
@ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
@ASTNodeAnnotation.Source(aspect="Literals", declaredAt="/home/jesper/git/extendj/java4/frontend/Literals.jrag:37")
public boolean isZero() {
ASTState state = state();
if (isZero_computed == ASTState.NON_CYCLE || isZero_computed == state().cycle()) {
return isZero_value;
}
isZero_value = isZero_compute();
if (state().inCircle()) {
isZero_computed = state().cycle();
} else {
isZero_computed = ASTState.NON_CYCLE;
}
return isZero_value;
}
/** @apilevel internal */
private boolean isZero_compute() {
String s = getLITERAL();
int len = s.length();
if (s.charAt(len - 1) == 'f' || s.charAt(len - 1) == 'F') {
len -= 1;
}
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c == 'E' || c == 'e' || c == 'P' || c == 'p') {
break;
}
if (Character.isDigit(c) && c != '0') {
return false;
}
}
return true;
}
/** @apilevel internal */
private void constant_reset() {
constant_computed = null;
constant_value = null;
}
/** @apilevel internal */
protected ASTState.Cycle constant_computed = null;
/** @apilevel internal */
protected Constant constant_value;
/**
* @attribute syn
* @aspect ConstantExpression
* @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:38
*/
@ASTNodeAnnotation.Attribute(kind=ASTNodeAnnotation.Kind.SYN)
@ASTNodeAnnotation.Source(aspect="ConstantExpression", declaredAt="/home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:38")
public Constant constant() {
ASTState state = state();
if (constant_computed == ASTState.NON_CYCLE || constant_computed == state().cycle()) {
return constant_value;
}
constant_value = constant_compute();
if (state().inCircle()) {
constant_computed = state().cycle();
} else {
constant_computed = ASTState.NON_CYCLE;
}
return constant_value;
}
/** @apilevel internal */
private Constant constant_compute() {
try {
String literal = getLITERAL();
int len = literal.length();
if (literal.charAt(len - 1) == 'f' || literal.charAt(len - 1) == 'F') {
literal = literal.substring(0, len - 1);
}
return Constant.create(Float.parseFloat(literal));
} catch (NumberFormatException e) {
Constant c = Constant.create(0.0f);
c.error = true;
return c;
}
}
/** @apilevel internal */
public ASTNode rewriteTo() {
return super.rewriteTo();
}
/** @apilevel internal */
public boolean canRewrite() {
return false;
}
/** @apilevel internal */
protected void collect_contributors_CompilationUnit_problems(CompilationUnit _root, java.util.Map> _map) {
// @declaredat /home/jesper/git/extendj/java4/frontend/TypeCheck.jrag:767
if (!isZero() && constant().floatValue() == 0.0f) {
{
java.util.Set contributors = _map.get(_root);
if (contributors == null) {
contributors = new java.util.LinkedHashSet();
_map.put((ASTNode) _root, contributors);
}
contributors.add(this);
}
}
// @declaredat /home/jesper/git/extendj/java4/frontend/TypeCheck.jrag:772
if (constant().floatValue() == Float.NEGATIVE_INFINITY
|| constant().floatValue() == Float.POSITIVE_INFINITY) {
{
java.util.Set contributors = _map.get(_root);
if (contributors == null) {
contributors = new java.util.LinkedHashSet();
_map.put((ASTNode) _root, contributors);
}
contributors.add(this);
}
}
super.collect_contributors_CompilationUnit_problems(_root, _map);
}
/** @apilevel internal */
protected void contributeTo_CompilationUnit_problems(LinkedList collection) {
super.contributeTo_CompilationUnit_problems(collection);
if (!isZero() && constant().floatValue() == 0.0f) {
collection.add(errorf("It is an error for nonzero floating-point %s to round to zero", getLITERAL()));
}
if (constant().floatValue() == Float.NEGATIVE_INFINITY
|| constant().floatValue() == Float.POSITIVE_INFINITY) {
collection.add(errorf("the floating-point literal \"%s\" is too large", getLITERAL()));
}
}
}