[Swift-commit] r6999 - branches/release-0.94/src/org/griphyn/vdl/engine

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Mon Aug 26 02:08:47 CDT 2013


Author: hategan
Date: 2013-08-26 02:08:46 -0500 (Mon, 26 Aug 2013)
New Revision: 6999

Modified:
   branches/release-0.94/src/org/griphyn/vdl/engine/FormalArgumentSignature.java
   branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java
   branches/release-0.94/src/org/griphyn/vdl/engine/ProcedureSignature.java
Log:
use inference when assigning from procedure calls and properly complain when inference is not possible

Modified: branches/release-0.94/src/org/griphyn/vdl/engine/FormalArgumentSignature.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/engine/FormalArgumentSignature.java	2013-08-26 07:06:13 UTC (rev 6998)
+++ branches/release-0.94/src/org/griphyn/vdl/engine/FormalArgumentSignature.java	2013-08-26 07:08:46 UTC (rev 6999)
@@ -41,6 +41,9 @@
 	public FormalArgumentSignature(boolean anyType) {
 		this.anyType = anyType;
 		this.optionalArg = false;
+		if (anyType) {
+		    this.type = ProcedureSignature.ANY;
+		}
 	}
 	
 	public FormalArgumentSignature(String type) {

Modified: branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java	2013-08-26 07:06:13 UTC (rev 6998)
+++ branches/release-0.94/src/org/griphyn/vdl/engine/Karajan.java	2013-08-26 07:08:46 UTC (rev 6999)
@@ -583,11 +583,34 @@
 		try {
 			StringTemplate assignST = template("assign");
 			StringTemplate varST = expressionToKarajan(assign.getAbstractExpressionArray(0), scope, true);
-			StringTemplate valueST = expressionToKarajan(assign.getAbstractExpressionArray(1),scope);
-			if (! (datatype(varST).equals(datatype(valueST)) ||
-			       datatype(valueST).equals("java")))
-				throw new CompilationException("You cannot assign value of type " + datatype(valueST) +
-						" to a variable of type " + datatype(varST));
+			String lValueType = datatype(varST);
+			
+			StringTemplate valueST = expressionToKarajan(assign.getAbstractExpressionArray(1), scope, false, lValueType);
+			
+			String rValueType = datatype(valueST);
+			
+			if (isAnyType(lValueType)) {
+			    if (isAnyType(rValueType)) {
+			        // any <- any
+			    }
+			    else {
+			        // any <- someType, so infer lValueType as rValueType
+			        setDatatype(varST, rValueType);
+			    }
+			}
+			else {
+			    if (isAnyType(rValueType)) {
+			        // someType <- any
+			        // only expressions that are allowed to return 'any' are procedures
+			        // for example readData(ret, file). These are special procedures that
+			        // need to look at the return type at run-time.
+			    }
+			    else if (!lValueType.equals(rValueType)){
+			        throw new CompilationException("You cannot assign value of type " + rValueType +
+                        " to a variable of type " + lValueType);
+			    }
+			}
+				
 			assignST.setAttribute("var", varST);
 			assignST.setAttribute("value", valueST);
 			assignST.setAttribute("line", getLine(assign));
@@ -599,7 +622,11 @@
 		}
 	}
 	
-	public void append(Append append, VariableScope scope) throws CompilationException {
+    private boolean isAnyType(String type) {
+        return ProcedureSignature.ANY.equals(type);
+    }
+    
+    public void append(Append append, VariableScope scope) throws CompilationException {
         try {
             StringTemplate appendST = template("append");
             StringTemplate array = expressionToKarajan(append.getAbstractExpressionArray(0),scope);
@@ -1228,14 +1255,20 @@
 	static final QName CALL_EXPR = new QName(SWIFTSCRIPT_NS, "call");
 	
 	public StringTemplate expressionToKarajan(XmlObject expression, VariableScope scope) throws CompilationException {
-	    return expressionToKarajan(expression, scope, false);
+	    return expressionToKarajan(expression, scope, false, null);
     }
+	
+	public StringTemplate expressionToKarajan(XmlObject expression, VariableScope scope, 
+            boolean lvalue) throws CompilationException {
+	    return expressionToKarajan(expression, scope, lvalue, null);
+	}
 
 	/** converts an XML intermediate form expression into a
 	 *  Karajan expression.
 	 */
-	public StringTemplate expressionToKarajan(XmlObject expression, VariableScope scope, boolean lvalue) throws CompilationException
-	{
+	public StringTemplate expressionToKarajan(XmlObject expression, VariableScope scope, 
+	        boolean lvalue, String expectedType) throws CompilationException {
+	    
 		Node expressionDOM = expression.getDomNode();
 		String namespaceURI = expressionDOM.getNamespaceURI();
 		String localName = expressionDOM.getLocalName();
@@ -1559,6 +1592,14 @@
 		    StringTemplate call = template("callexpr");
 
 		    String type = funcSignature.getOutputArray(0).getType();
+		    if (isAnyType(type)) {
+		        if (expectedType != null) {
+		            type = expectedType;
+		        }
+		        else {
+		            throw new CompilationException("Cannot infer return type of procedure call");
+		        }
+		    }
 		    subscope.addInternalVariable("swift#callintermediate", type, null);
 
 		    call.setAttribute("datatype", type);
@@ -1719,4 +1760,9 @@
 	        throw new RuntimeException("Not typed properly: " + st);
 	    }
 	}
+	
+	protected void setDatatype(StringTemplate st, String type) {
+	    st.removeAttribute("datatype");
+	    st.setAttribute("datatype", type);
+	}
 }

Modified: branches/release-0.94/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- branches/release-0.94/src/org/griphyn/vdl/engine/ProcedureSignature.java	2013-08-26 07:06:13 UTC (rev 6998)
+++ branches/release-0.94/src/org/griphyn/vdl/engine/ProcedureSignature.java	2013-08-26 07:08:46 UTC (rev 6999)
@@ -147,7 +147,7 @@
     public static final String INT = "int";
     public static final String FLOAT = "float";
     public static final String BOOLEAN = "boolean";
-    public static final String ANY = "-any";
+    public static final String ANY = "any";
     public static final String VARGS = "-vargs";
 
 	public static Map<String,ProcedureSignature>
@@ -204,7 +204,7 @@
 		add(functionsMap, "format", returns(STRING), args(FLOAT, INT));
 		add(functionsMap, "pad", returns(STRING), args(INT, INT));
 		
-		add(functionsMap, "java", returns("java"), args(VARGS));
+		add(functionsMap, "java", returns(ANY), args(VARGS));
 
 		add(functionsMap, "exists", returns(BOOLEAN), args(VARGS));
 		




More information about the Swift-commit mailing list