[Swift-commit] r3196 - in trunk: libexec src/org/griphyn/vdl/engine src/org/griphyn/vdl/karajan/lib/swiftscript

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Thu Dec 3 14:19:20 CST 2009


Author: wozniak
Date: 2009-12-03 14:19:20 -0600 (Thu, 03 Dec 2009)
New Revision: 3196

Modified:
   trunk/libexec/vdl-lib.xml
   trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
   trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java
Log:
New tracef() function.  Documented in Misc.java . 


Modified: trunk/libexec/vdl-lib.xml
===================================================================
--- trunk/libexec/vdl-lib.xml	2009-12-03 18:03:29 UTC (rev 3195)
+++ trunk/libexec/vdl-lib.xml	2009-12-03 20:19:20 UTC (rev 3196)
@@ -12,6 +12,7 @@
 	<export name="regexp"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="toint"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="trace"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
+	<export name="tracef"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.Misc"/></export>
 	<export name="fileName"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileName"/></export>
 	<export name="fileNames"><elementDef classname="org.griphyn.vdl.karajan.lib.swiftscript.FileNames"/></export>
 	<export name="arg">

Modified: trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java
===================================================================
--- trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2009-12-03 18:03:29 UTC (rev 3195)
+++ trunk/src/org/griphyn/vdl/engine/ProcedureSignature.java	2009-12-03 20:19:20 UTC (rev 3196)
@@ -137,6 +137,11 @@
 		trace.setAnyNumOfInputArgs();
 		trace.setInvocationMode(INVOCATION_INTERNAL);
 		proceduresMap.put("trace", trace);
+
+		ProcedureSignature tracef = new ProcedureSignature("tracef");
+		trace.setAnyNumOfInputArgs();
+		trace.setInvocationMode(INVOCATION_INTERNAL);
+		proceduresMap.put("tracef", trace);
 			
 		ProcedureSignature writeData = new ProcedureSignature("writeData");
 		FormalArgumentSignature wdInputArg = new FormalArgumentSignature(true);

Modified: trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java
===================================================================
--- trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java	2009-12-03 18:03:29 UTC (rev 3195)
+++ trunk/src/org/griphyn/vdl/karajan/lib/swiftscript/Misc.java	2009-12-03 20:19:20 UTC (rev 3196)
@@ -32,6 +32,7 @@
 
 	static {
 		setArguments("swiftscript_trace", new Arg[] { Arg.VARGS });
+		setArguments("swiftscript_tracef", new Arg[] { Arg.VARGS });
 		setArguments("swiftscript_strcat", new Arg[] { Arg.VARGS });
 		setArguments("swiftscript_strcut", new Arg[] { PA_INPUT, PA_PATTERN });
 		setArguments("swiftscript_strsplit", new Arg[] { PA_INPUT, PA_PATTERN });
@@ -57,6 +58,123 @@
 		return null;
 	}
 
+        /**
+           Formatted trace output. <br>
+           Example: tracef("\t%s\n", "hello"); <br>
+           Differences from trace(): 1) respects \t, \n and \\;
+           2) allows for typechecked format specifiers; 3) allows for
+           consumption of variables without display (%k); 4) does not
+           impose any formatting (commas, etc.).  <br><br>
+           Format specifiers: <br>
+           %%: % sign. <br>
+           %p: Not typechecked, output as in trace(). <br>
+           %i: Typechecked int output. <br>
+           %s: Typechecked string output. <br>
+           %k: Variable sKipped, no output. 
+        */
+	public DSHandle swiftscript_tracef(VariableStack stack) throws ExecutionException, NoSuchTypeException,
+			InvalidPathException {
+
+		DSHandle[] args = SwiftArg.VARGS.asDSHandleArray(stack);
+
+		StringBuffer buf = new StringBuffer();
+		for (int i = 0; i < args.length; i++) {
+			DSHandle handle = args[i];
+			VDLFunction.waitFor(stack, handle);
+		}
+                String msg = format(args); 
+                buf.append(msg);
+                traceLogger.warn(buf); 
+		return null;
+	}
+
+        /**
+           Helper for {@link #swiftscript_tracef}.
+        */
+        private String format(DSHandle[] args) throws ExecutionException {
+	    if (! (args[0].getType() == Types.STRING))
+		throw new ExecutionException("First argument to tracef() must be a string!"); 
+
+	    String spec = args[0].toString(); 
+	    StringBuffer output = new StringBuffer(); 
+	    int i = 0; 
+	    int a = 1; 
+	    while (i < spec.length()) {
+		char c = spec.charAt(i);
+		if (c == '%') {
+		    char d = spec.charAt(++i); 
+		    a = append(d, a, args, output);
+                }
+                else if (c == '\\') {
+                    char d = spec.charAt(++i);
+                    escape(d, output); 
+		}
+                else {
+		    output.append(c);
+		}
+                i++;
+	    }
+	    String result = output.toString(); 
+	    return result; 
+	}
+
+        /**
+           Helper for {@link #swiftscript_tracef}.
+        */
+        private int append(char c, int arg, DSHandle[] args, StringBuffer output) throws ExecutionException {
+            if (c == '%') {
+		output.append('%');
+                return arg;
+            }
+            if (arg >= args.length) {
+                throw new ExecutionException("tracef(): too many specifiers!");
+            }
+	    if (c == 'p') {
+		output.append(args[arg].toString());
+            }
+            else if (c == 's') {
+                if (args[arg].getType() == Types.STRING) {
+		    output.append(args[arg]).toString();
+                }
+                else {
+                    throw new ExecutionException("tracef(): %s requires a string!");
+                }
+            }
+	    else if (c == 'i') {
+                if (args[arg].getType() == Types.INT) {
+		    output.append(args[arg]).toString();
+                }
+                else {
+                    throw new ExecutionException("tracef(): %i requires an int!");
+                }
+            }
+            else if (c == 'k') {
+                ;
+            }
+	    else {
+		throw new ExecutionException("tracef(): Unknown format: %" + c);
+            }
+	    return arg+1;
+	}
+
+        /**
+           Helper for {@link #swiftscript_tracef}.
+        */
+        private void escape(char c, StringBuffer output) throws ExecutionException {
+            if (c == '\\') {
+                output.append('\\');
+            }
+            else if (c == 'n') {
+                output.append('\n');
+            }
+            else if (c == 't') {
+                output.append('\t');
+            }
+            else {
+                throw new ExecutionException("tracef(): unknown backslash escape sequence!");
+            }
+	}
+	
 	public DSHandle swiftscript_strcat(VariableStack stack) throws ExecutionException, NoSuchTypeException,
 			InvalidPathException {
 		Object[] args = SwiftArg.VARGS.asArray(stack);




More information about the Swift-commit mailing list