diff options
author | Sven Gothel <[email protected]> | 2015-04-01 15:51:04 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2015-04-01 15:51:04 +0200 |
commit | c6d5ee30e023d030697f14ae2c444ce7a5542e94 (patch) | |
tree | 0013fef29a228522b6791630385932d38a36a399 /src/java/com/jogamp/gluegen/cgram | |
parent | c156343fec33ceea7f238b9766a9f4985fb92687 (diff) |
Bug 1153 - GlueGen: Support [const] [native] expressions and conversion to java space, incl. [native] numbers
Rewrite ConstantDefinition:
Add sub-class CNumber:
- containing integer/float values and their original qualifiers
[long, double, unsigned]
- conversion to java number
ConstantDefinition:
- holds native expression
- optionally holds CNumber representing native expression,
if [only] a number
- can compute equivalent java expression
with result type (JavaExpr)
Add static native number reg-expression
for number detection and parsing.
Add static native number to CNumber conversion methods.
+++
Retrieve full LISP tree and convert to serialized expression
to be utilized for expressions used in enumerates.
Parse enumerates, allowing const native expressions:
- Utilize ConstantDefinition
either for definite CNumber or expression
- Simply add "+1" for new default values,
if previous is an expression
Diffstat (limited to 'src/java/com/jogamp/gluegen/cgram')
-rw-r--r-- | src/java/com/jogamp/gluegen/cgram/TNode.java | 83 | ||||
-rw-r--r-- | src/java/com/jogamp/gluegen/cgram/types/EnumType.java | 76 |
2 files changed, 120 insertions, 39 deletions
diff --git a/src/java/com/jogamp/gluegen/cgram/TNode.java b/src/java/com/jogamp/gluegen/cgram/TNode.java index 70dc2c4..5a36945 100644 --- a/src/java/com/jogamp/gluegen/cgram/TNode.java +++ b/src/java/com/jogamp/gluegen/cgram/TNode.java @@ -10,6 +10,7 @@ import java.util.Enumeration; import com.jogamp.gluegen.ASTLocusTag; import com.jogamp.gluegen.ASTLocusTag.ASTLocusTagProvider; +import com.jogamp.gluegen.GlueGen; /** Class TNode is an implementation of the AST interface @@ -179,15 +180,87 @@ public void initialize(final AST tr) { text = text_; } - /** Returns the text for this node and all children */ - public String getAllChildrenText() { + static class DebugASTVisitor { + protected int level; + private String tabs(final StringBuilder sb) { + sb.setLength(0); + for (int i = 0; i < level; i++) { + sb.append(" "); + } + return sb.toString(); + } + DebugASTVisitor(final int level) { + this.level = level; + } + void visit(final AST node) { + final StringBuilder sb = new StringBuilder(); + AST node2; + for (node2 = node; node2 != null; node2 = node2.getNextSibling()) { + if (node2.getText() == null) { + System.err.printf("%03d: %snil [%d]%n", level, tabs(sb), node2.getType()); + } else { + System.err.printf("%03d: %s%s [%d]%n", level, tabs(sb), node2.getText(), node2.getType()); + } + if (node2.getFirstChild() != null) { + level++; + visit(node2.getFirstChild()); + level--; + } + } + } + } + + /** + * Returns the text for this node, its children and siblings. + * <p> + * Implementation converts the AST LISP notation to serialized form. + * </p> + */ + public String getAllChildrenText(final String name) { + if( GlueGen.debug() ) { + System.err.println("TNode.XXX: "+name); + new DebugASTVisitor(1).visit(getFirstChild()); + } final StringBuilder buf = new StringBuilder(); - buf.append(getText()); - for (TNode node = (TNode) getFirstChild(); node != null; node = (TNode) node.getNextSibling()) { - buf.append(node.getText()); + final TNode down = (TNode) this.getFirstChild(); + if( null == down ) { + buf.append(this.getText()); + } else { + getAllChildrenText(buf, this, down); } return buf.toString(); } + private static void getAllChildrenText(final StringBuilder buf, + final TNode upNode, TNode thisNode) { + boolean first = true; + while( null != thisNode ) { + final boolean isClosing = HeaderParserTokenTypes.RPAREN == thisNode.getType(); + final boolean isGroupStart = HeaderParserTokenTypes.NExpressionGroup == thisNode.getType(); + + final TNode nextNode = (TNode) thisNode.getNextSibling(); + final TNode downNode = (TNode) thisNode.getFirstChild(); + if( !isClosing && + ( null == downNode && null == nextNode || // unary + !first // binary + ) + ) { + buf.append(" ").append(upNode.getText()); + } + if( null != downNode ) { + if( !isGroupStart ) { + buf.append(" ("); + } + getAllChildrenText(buf, thisNode, downNode); + if( !isGroupStart ) { + buf.append(" )"); + } + } else if( !isClosing ) { + buf.append(" ").append(thisNode.getText()); + } + thisNode = nextNode; + first = false; + } + } /** return the last child of this node, or null if there is none */ public TNode getLastChild() { diff --git a/src/java/com/jogamp/gluegen/cgram/types/EnumType.java b/src/java/com/jogamp/gluegen/cgram/types/EnumType.java index f49c2ec..7c2fa73 100644 --- a/src/java/com/jogamp/gluegen/cgram/types/EnumType.java +++ b/src/java/com/jogamp/gluegen/cgram/types/EnumType.java @@ -43,6 +43,9 @@ import java.util.ArrayList; import java.util.NoSuchElementException; import com.jogamp.gluegen.ASTLocusTag; +import com.jogamp.gluegen.ConstantDefinition; +import com.jogamp.gluegen.ConstantDefinition.CNumber; +import com.jogamp.gluegen.GlueGenException; import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp; @@ -50,40 +53,49 @@ import com.jogamp.gluegen.cgram.types.TypeComparator.SemanticEqualityOp; they have a set of named values. */ public class EnumType extends IntType implements Cloneable { - private static class Enum implements TypeComparator.SemanticEqualityOp { - final String name; - final long value; + public static class Enumerator implements TypeComparator.SemanticEqualityOp { + private final String name; + private final String expr; + private final CNumber number; - Enum(final String name, final long value) { + public Enumerator(final String name, final long value) { this.name = name; - this.value = value; + this.number = new CNumber(false, false, value); + this.expr = this.number.toJavaString(); } - - String getName() { - return name; + public Enumerator(final String name, final CNumber number) { + this.name = name; + this.number = number; + this.expr = this.number.toJavaString(); } - - long getValue() { - return value; + public Enumerator(final String name, final String value) { + this.name = name; + this.expr = value; + this.number = ConstantDefinition.decodeIntegerNumber(value); } + public String getName() { return name; } + public String getExpr() { return expr; } + public CNumber getNumber() { return number; } + public boolean hasNumber() { return null != number; } + @Override public int hashCode() { // 31 * x == (x << 5) - x final int hash = name.hashCode(); - return ((hash << 5) - hash) + (int)(value ^ (value >>> 32)); + return ((hash << 5) - hash) + expr.hashCode(); } @Override public boolean equals(final Object arg) { if (arg == this) { return true; - } else if ( !(arg instanceof Enum) ) { + } else if ( !(arg instanceof Enumerator) ) { return false; } - final Enum t = (Enum) arg; + final Enumerator t = (Enumerator) arg; return name.equals(t.name) && - value == t.value; + expr.equals(t.expr); } @Override @@ -96,11 +108,12 @@ public class EnumType extends IntType implements Cloneable { return equals(arg); } - public String toString() { return name+" = "+value; } + @Override + public String toString() { return "["+name+" = ["+expr+", "+number+"]"; } } private final IntType underlyingType; - private ArrayList<Enum> enums; + private ArrayList<Enumerator> enums; public EnumType(final String name) { super(name, SizeThunk.LONG, false, CVAttributes.CONST); @@ -116,7 +129,7 @@ public class EnumType extends IntType implements Cloneable { super(o, cvAttributes, astLocus); underlyingType = o.underlyingType; if(null != o.enums) { - enums = new ArrayList<Enum>(o.enums); + enums = new ArrayList<Enumerator>(o.enums); } } @@ -164,11 +177,11 @@ public class EnumType extends IntType implements Cloneable { public Type getUnderlyingType() { return this.underlyingType; } - public void addEnum(final String name, final long val) { + public void addEnum(final String name, final Enumerator newEnum) { if (enums == null) { - enums = new ArrayList<Enum>(); + enums = new ArrayList<Enumerator>(); } - enums.add(new Enum(name, val)); + enums.add(newEnum); clearCache(); } @@ -177,22 +190,17 @@ public class EnumType extends IntType implements Cloneable { return enums.size(); } - /** Fetch <i>i</i>th (0..getNumEnumerates() - 1) name */ - public String getEnumName(final int i) { - return (enums.get(i)).getName(); - } - - /** Fetch <i>i</i>th (0..getNumEnumerates() - 1) value */ - public long getEnumValue(final int i) { - return (enums.get(i)).getValue(); + /** Fetch <i>i</i>th (0..getNumEnumerates() - 1) {@link Enumerator} */ + public Enumerator getEnum(final int i) { + return enums.get(i); } - /** Fetch the value of the enumerate with the given name. */ - public long getEnumValue(final String name) { + /** Fetch the enumerate with the given name. */ + public Enumerator getEnum(final String name) { for (int i = 0; i < enums.size(); ++i) { - final Enum n = (enums.get(i)); + final Enumerator n = (enums.get(i)); if (n.getName().equals(name)) { - return n.getValue(); + return n; } } throw new NoSuchElementException( @@ -215,7 +223,7 @@ public class EnumType extends IntType implements Cloneable { */ public boolean removeEnumerate(final String name) { for (int i = 0; i < enums.size(); ++i) { - final Enum e = enums.get(i); + final Enumerator e = enums.get(i); if (e.getName().equals(name)) { enums.remove(e); clearCache(); |