{
public static bool Debug = false;
- public static void DebugWrite (string fmt, params object[] arg)
- {
- if (Debug)
- Console.Write (fmt, arg);
- }
-
public struct Name : IEquatable<Name>
{
private static NameTable nt = new NameTable ();
for (int i = 0; i < args.Length; i++)
{
- object val = ((Xex) args[i]).Eval (domain);
+ object val = args[i];
+ if (val is Xex)
+ val = ((Xex) val).Eval (domain);
if (val == null)
throw new Exception (args[i] + ":evaled to null");
args[i] = val;
}
- DebugWrite ("calling (" + this);
- foreach (object a in args)
- DebugWrite (" " + a);
- DebugWrite (") => ");
result = builtin (args, domain);
- DebugWrite (result + "\n");
return result;
}
}
public override object Call (object[] args, Domain domain)
{
- object result;
-
- DebugWrite ("calling (" + this);
- foreach (object a in args)
- DebugWrite (" " + a);
- DebugWrite (") => ");
- result = builtin (args, domain);
- DebugWrite (result + "\n");
- return result;
+ return builtin (args, domain);
}
}
Variable var = domain.GetVar (this.args[i]);
domain.Bind (var, args[i]);
}
- DebugWrite ("calling (" + this);
foreach (Xex e in body)
result = e.Eval (domain);
- DebugWrite (") => " + result + "\n");
} finally {
domain.UnboundTo (current);
}
public abstract bool ValueP (object value);
- public override string ToString () { return name + "=" + val; }
+ public override string ToString () { return name + "(" + val + ")"; }
}
internal class VarInt : Variable
internal Bindings UnboundTo (Bindings boundary)
{
for (Bindings b = this; b != boundary; b = b.next)
- vari.val = b.old_value;
+ b.vari.val = b.old_value;
return boundary;
}
public class Domain
{
public object context;
+ public int depth = 0;
internal Dictionary<Name, Function> functions;
internal Dictionary<Name, Variable> variables;
internal void Bind (Variable vari, object value)
{
bindings = Bindings.Bind (bindings, vari, value);
- DebugWrite ("binding " + vari);
}
internal void UnboundTo (Bindings boundary)
str += ">";
return str;
}
+
+ public void DebugWrite (bool head, string fmt, params object[] arg)
+ {
+ if (Debug)
+ {
+ if (head)
+ {
+ Console.WriteLine ();
+ for (int i = 0; i < depth; i++)
+ Console.Write (" ");
+ }
+ Console.Write (fmt, arg);
+ }
+ }
}
public delegate object Builtin (object[] args, Domain domain);
public override object Eval (Domain domain)
{
+ domain.DebugWrite (true, "(({0}", func);
for (int i = 0; i < args.Length; i++)
- real_args[i] = args[i];
- return func.Call (real_args, domain);
+ {
+ domain.DebugWrite (false, " {0}", args[i]);
+ real_args[i] = args[i];
+ }
+ domain.DebugWrite (false, ")");
+ domain.depth += 2;
+ object result = func.Call (real_args, domain);
+ domain.depth -= 2;
+ domain.DebugWrite (true, " => {0})", result);
+ return result;
}
public override Name TypeOf { get { return Nfuncall; } }
public override object Eval (Domain domain)
{
+ domain.DebugWrite (true, "(get-value {0})", vari);
return vari.val;
}
public Const (object val) { this.val = val; }
- public override object Eval (Domain domain) { return val; }
+ public override object Eval (Domain domain)
+ {
+ domain.DebugWrite (true, "(const {0})\n", val);
+ return val;
+ }
public override Name TypeOf {
get