This is the mail archive of the
kawa@sourceware.cygnus.com
mailing list for the Kawa project.
Re: tracing cosmetics
Per Bothner <per@bothner.com> writes:
> (3) Instead of using a static field, one should use a static
> Binding field, with a FluidConstraint and a per-thread FluidBinding.
> That may be a little hairy to figure out, that it has the advantage
> of being a local change.
I implemented this.
1999-10-07 Per Bothner <per@bothner.com>
* kawa/standard/TracedProcedure.java: Indent nested calls.
Index: TracedProcedure.java
===================================================================
RCS file: /cvs/kawa/kawa/kawa/standard/TracedProcedure.java,v
retrieving revision 1.1
diff -u -r1.1 TracedProcedure.java
--- TracedProcedure.java 1999/07/20 01:59:36 1.1
+++ TracedProcedure.java 1999/10/08 05:23:57
@@ -1,6 +1,7 @@
package kawa.standard;
import gnu.mapping.*;
import java.io.PrintWriter;
+import gnu.math.IntNum;
/** A TracedProcedure is a Procedure wrapper that writes trace output. */
@@ -9,6 +10,10 @@
public Procedure proc;
boolean enabled;
+ static Binding indentation
+ = Binding.make(gnu.math.IntNum.zero(), "indentation");
+ static int indentationStep = 2;
+
public TracedProcedure (Procedure proc, boolean enable)
{
this.proc = proc;
@@ -33,14 +38,24 @@
}
}
+ static void indent(int i, PrintWriter out)
+ {
+ while (--i >= 0)
+ out.print(' ');
+ }
+
public Object applyN(Object[] args)
{
if (enabled)
{
+ int curIndent = ((IntNum) indentation.getValue()).intValue();
PrintWriter out = OutPort.errDefault();
String name = getName();
if (name == null)
name = "??";
+
+ // Print the call arguments (indented).
+ indent(curIndent, out);
out.print("call to ");
out.print(name);
int len = args.length;
@@ -52,7 +67,26 @@
put(args[i], out);
}
out.println(")");
- Object result = proc.applyN(args);
+
+ // Now do the actual call, but with the indentation incremented.
+ gnu.mapping.Future context = gnu.mapping.Future.getContext();
+ FluidBinding oldBindings = context.fluidBindings;
+ IntNum newIndentation = IntNum.make(curIndent+indentationStep);
+ FluidBinding newBindings
+ = new FluidBinding(oldBindings, newIndentation, indentation);
+ Object result;
+ try
+ {
+ context.setFluids(newBindings);
+ result = proc.applyN(args);
+ }
+ finally
+ {
+ context.resetFluids(oldBindings);
+ }
+
+ // Print the result (indented).
+ indent(curIndent, out);
out.print("return from ");
out.print(name);
out.print(" => ");
Sample output:
#|kawa:1|# (define (fac x) (if (> x 1) (* x (fac (- x 1))) 1))
#|kawa:2|# (fac 4)
24
#|kawa:3|# (trace fac)
#|kawa:4|# (fac 4)
call to fac (4)
call to fac (3)
call to fac (2)
call to fac (1)
return from fac => 1
return from fac => 2
return from fac => 6
return from fac => 24
24
--
--Per Bothner
bothner@pacbell.net per@bothner.com http://www.bothner.com/~per/