[Swift-commit] r4861 - in trunk/src/org/griphyn/vdl: engine type

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Tue Jul 26 17:38:53 CDT 2011


Author: hategan
Date: 2011-07-26 17:38:52 -0500 (Tue, 26 Jul 2011)
New Revision: 4861

Modified:
   trunk/src/org/griphyn/vdl/engine/Karajan.java
   trunk/src/org/griphyn/vdl/type/Types.java
Log:
fixed array access for multi-dimensional arrays

Modified: trunk/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/Karajan.java	2011-07-26 21:33:03 UTC (rev 4860)
+++ trunk/src/org/griphyn/vdl/engine/Karajan.java	2011-07-26 22:38:52 UTC (rev 4861)
@@ -495,12 +495,12 @@
             StringTemplate appendST = template("append");
             StringTemplate array = expressionToKarajan(append.getAbstractExpressionArray(0),scope);
             StringTemplate value = expressionToKarajan(append.getAbstractExpressionArray(1),scope);
-            String indexType = org.griphyn.vdl.type.Types.getArrayIndexTypeName(datatype(array));
+            String indexType = org.griphyn.vdl.type.Types.getArrayInnerIndexTypeName(datatype(array));
             if (!"auto".equals(indexType)) {
                 throw new CompilationException("You can only append to an array with " +
                 		"'auto' index type. Current index type: " + indexType);
             }
-            if (!datatype(value).equals(org.griphyn.vdl.type.Types.getArrayItemTypeName(datatype(array)))) {
+            if (!datatype(value).equals(org.griphyn.vdl.type.Types.getArrayInnerItemTypeName(datatype(array)))) {
                 throw new CompilationException("You cannot append value of type " + datatype(value) +
                         " to an array of type " + datatype(array));
             }
@@ -809,8 +809,8 @@
 			foreachST.setAttribute("in", inST);
 
 			String inType = datatype(inST);
-			String itemType = org.griphyn.vdl.type.Types.getArrayItemTypeName(inType);
-			String keyType = org.griphyn.vdl.type.Types.getArrayIndexTypeName(inType);
+			String itemType = org.griphyn.vdl.type.Types.getArrayInnerItemTypeName(inType);
+			String keyType = org.griphyn.vdl.type.Types.getArrayInnerIndexTypeName(inType);
 			if (itemType == null) {
 			    throw new CompilationException("You can iterate through an array structure only");
 			}
@@ -950,7 +950,7 @@
 				XmlObject argument = app.getAbstractExpressionArray(i);
 				StringTemplate argumentST = expressionToKarajan(argument, scope);
 				String type = datatype(argumentST);
-				String base = org.griphyn.vdl.type.Types.getArrayItemTypeName(type);
+				String base = org.griphyn.vdl.type.Types.getArrayInnerItemTypeName(type);
 				String testType;
 				// if array then use the array item type for testing
 				if (base != null) {
@@ -1192,7 +1192,7 @@
 			StringTemplate parentST = expressionToKarajan(op.getAbstractExpressionArray(0), scope);
 
 			String indexType = datatype(arrayST);
-			String declaredIndexType = org.griphyn.vdl.type.Types.getArrayIndexTypeName(datatype(parentST));
+			String declaredIndexType = org.griphyn.vdl.type.Types.getArrayOuterIndexTypeName(datatype(parentST));
 			// the index type must match the declared index type,
 			// unless the declared index type is *
 			
@@ -1215,7 +1215,7 @@
 			StringTemplate newst = template("extractarrayelement");
 			newst.setAttribute("arraychild", arrayST);
 			newst.setAttribute("parent", parentST);
-			newst.setAttribute("datatype", org.griphyn.vdl.type.Types.getArrayItemTypeName(datatype(parentST)));
+			newst.setAttribute("datatype", org.griphyn.vdl.type.Types.getArrayOuterItemTypeName(datatype(parentST)));
 
 			return newst;
 		} else if (expressionQName.equals(STRUCTURE_MEMBER_EXPR)) {
@@ -1227,8 +1227,8 @@
 			// if the parent is an array, then check against
 			// the base type of the array
 			
-			String baseType = org.griphyn.vdl.type.Types.getArrayItemTypeName(parentType);
-			String indexType = org.griphyn.vdl.type.Types.getArrayIndexTypeName(parentType);
+			String baseType = org.griphyn.vdl.type.Types.getArrayInnerItemTypeName(parentType);
+			String indexType = org.griphyn.vdl.type.Types.getArrayInnerIndexTypeName(parentType);
 			String arrayType = parentType;
 
 			boolean arrayMode = false;

Modified: trunk/src/org/griphyn/vdl/type/Types.java
===================================================================
--- trunk/src/org/griphyn/vdl/type/Types.java	2011-07-26 21:33:03 UTC (rev 4860)
+++ trunk/src/org/griphyn/vdl/type/Types.java	2011-07-26 22:38:52 UTC (rev 4861)
@@ -7,6 +7,8 @@
 import org.griphyn.vdl.type.impl.TypeImpl.Array;
 
 public abstract class Types {
+    
+    public static final String DEFAULT_ARRAY_KEY_TYPE_NAME = "int";
 
 	//TODO: check namespace references in type definitions
 	private static Map<String, Type> types = 
@@ -17,7 +19,7 @@
 	        return true;
 	    }
 	    else {
-	        String[] names = getArrayTypeNames(name);
+	        String[] names = getArrayInnerTypeNames(name);
 	        if (names == null) {
 	            return false;
 	        }
@@ -27,7 +29,7 @@
 	    }
 	}
 	
-	private static String[] getArrayTypeNames(String name) {
+	private static String[] getArrayInnerTypeNames(String name) {
 	    if(name.endsWith("]")) {
             int index = name.lastIndexOf('[');
             if (index == -1) {
@@ -36,7 +38,7 @@
             String keyTypeName = name.substring(index + 1, name.length() - 1);
             String baseTypeName = name.substring(0, index);
             if (keyTypeName.equals("")) {
-                keyTypeName = "int";
+                keyTypeName = DEFAULT_ARRAY_KEY_TYPE_NAME;
             }
             return new String[] {baseTypeName, keyTypeName};
         }
@@ -45,8 +47,29 @@
 	    }
 	}
 	
-	private static String getArrayComponentTypeName(String name, int index) {
-	    String[] names = getArrayTypeNames(name);
+	private static String[] getArrayOuterTypeNames(String name) {
+	    int index = name.indexOf('[');
+	    if (index >= 0) {
+	        int i2 = name.indexOf(']');
+	        if (i2 < index) {
+	            throw new RuntimeException("Malformed type name: " + name);
+	        }
+	        // if a: tv[tk1][tk2]
+	        // then a[k1] is a tv[tk2].
+	        String keyTypeName = name.substring(index + 1, i2);
+	        String baseTypeName = name.substring(0, index) + name.substring(i2 + 1);
+	        if (keyTypeName.equals("")) {
+	            keyTypeName = DEFAULT_ARRAY_KEY_TYPE_NAME;
+	        }
+	        return new String[] {baseTypeName, keyTypeName};
+	    }
+	    else {
+	        return null;
+	    }
+    }
+	
+	private static String getArrayInnerComponentTypeName(String name, int index) {
+	    String[] names = getArrayInnerTypeNames(name);
         if (names == null) {
             return null;
         }
@@ -55,8 +78,18 @@
         }
 	}
 	
+	private static String getArrayOuterComponentTypeName(String name, int index) {
+        String[] names = getArrayOuterTypeNames(name);
+        if (names == null) {
+            return null;
+        }
+        else {
+            return names[index];
+        }
+    }
+	
 	public static String normalize(String t) {
-        String[] names = getArrayTypeNames(t);
+        String[] names = getArrayInnerTypeNames(t);
         if (names == null) {
             return t;
         }
@@ -65,18 +98,46 @@
         }
     }
 	
-	public static String getArrayItemTypeName(String name) {
-	    return getArrayComponentTypeName(name, 0);
+	/**
+     * Returns the inner item type name for an array. That is, if
+     * a is of type t[tk1][tk2]...[tkN], then the inner item type name
+     * is t[tk1][tk2]...[tk(N-1)]
+     */
+	public static String getArrayInnerItemTypeName(String name) {
+	    return getArrayInnerComponentTypeName(name, 0);
 	}
 	
-	public static String getArrayIndexTypeName(String name) {
-        return getArrayComponentTypeName(name, 1);
+	/**
+     * Returns the inner key type name for an array. That is, if
+     * a is of type t[tk1][tk2]...[tkN], then the inner key type name
+     * is tkN
+     */
+	public static String getArrayInnerIndexTypeName(String name) {
+        return getArrayInnerComponentTypeName(name, 1);
     }
+	
+	/**
+     * Returns the outer key type name for an array. That is, if
+     * a is of type t[tk1][tk2]...[tkN], then the outer key type name
+     * is tk1
+     */
+	public static String getArrayOuterIndexTypeName(String name) {
+        return getArrayOuterComponentTypeName(name, 1);
+    }
+	
+	/**
+     * Returns the outer item type name for an array. That is, if
+     * a is of type t[tk1][tk2]...[tkN], then the outer key type name
+     * is t[tk2]...[tkN]
+     */
+    public static String getArrayOuterItemTypeName(String name) {
+        return getArrayOuterComponentTypeName(name, 0);
+    }
 
 	public synchronized static Type getType(String name) throws NoSuchTypeException {
 		Type type = types.get(name);
 		if (type == null) {
-		    String[] names = getArrayTypeNames(name);
+		    String[] names = getArrayInnerTypeNames(name);
 		    if (names != null) {
 				Type baseType = getType(names[0]);
 				Type keyType = getType(names[1]);




More information about the Swift-commit mailing list