private static Name Nexpr = "expr";
private static Name Ntype = "type";
- private static Name Nargs = "args";
private static Name Ninteger = "integer";
private static Name Nstring = "string";
private static Name Nobject = "object";
private static Name Ndefun = "defun";
+ private static Name Nfname = "fname";
+ private static Name Nargs = "args";
+ private static Name Noptional = "optional";
+ private static Name Nrest = "rest";
+ private static Name Nbody = "body";
+
private static Name Ndefvar = "defvar";
private static Name Nconst = "const";
private static Name Nvariable = "variable";
private static Name Nfuncall = "funcall";
private static Name Nprogn = "progn";
- private static Name Neval = "eval";
internal class Function
{
internal Xex[] args;
internal Xex[] body;
- public Lambda (XmlNode node, Domain domain) { }
-
public void SetArgs (XmlNode node, int nargs, Domain domain)
{
args = new Xex[nargs];
- for (i = 0, n = node.FirstChild; i < nargs; n = n.NextSibling)
- if (n.Name != Noptional && n.Name != Nrest)
- args[i++] = new Xex (n, domain);
+ node = node.FirstChild;
+ for (int i = 0; i < nargs; node = node.NextSibling)
+ if (node.Name != Noptional && node.Name != Nrest)
+ args[i++] = new Xex (node, domain);
}
public void SetBody (XmlNode node, Domain domain)
body = new Xex[nlist.Count];
for (int i = 0; i < nlist.Count; i++)
- body[i] = new Xex (nlist[idx], domain);
+ body[i] = new Xex (nlist[i], domain);
}
}
public static Name ParseHead (XmlNode node,
out int min_arg, out int max_arg)
{
- Name name = node.Attributes[Ffname].Value;
+ Name name = node.Attributes[Nfname].Value;
int nargs = 0, noptions = 0, nrest = 0;
XmlNode n;
break;
nargs++;
}
- if (n.Name == Noptional)
+ if (n != null && n.Name == Noptional)
for (n = n.NextSibling; n != null; n = n.NextSibling)
{
if (n.Name == Nrest)
break;
noptions++;
}
- if (n.Name == Nrest)
+ if (n != null && n.Name == Nrest)
for (n = n.NextSibling; n != null; n = n.NextSibling)
nrest++;
min_arg = nargs;
Bindings current = domain.bindings;
object result = false;
- Console.Write ("calling (" + this + "(");
+ Console.Write ("calling (" + this);
try {
foreach (Xex e in args)
- Console.Write (e);
- Console.WriteLine (")");
+ Console.Write (" " + e);
+ Console.Write (") => ");
if (builtin != null)
{
if (! specialp)
foreach (Xex a in args)
- a.Eval ();
- return builtin (args, domain);
+ if (a.Eval (domain) == null)
+ throw new Exception ("evaled to null");
+ result = builtin (args, domain);
+ Console.WriteLine (result);
+ return result;
}
- if (lambda == null)
- return null;
int i;
for (i = 0; i < min_arg; i++)
Name name = isdirect ? (Name) a.val : (Name) a.args[0].val;
Variable var = new VarMisc (name, null);
- if (! isdirect)
- args[i].Eval ();
- domain.Bind (bindings, var, args[i]);
+ domain.variables[name] = var;
+ if (isdirect)
+ domain.Bind (var, args[i]);
+ else
+ domain.Bind (var, args[i].Eval (domain));
}
foreach (Xex e in lambda.body)
- {
- result = e.Eval (domain);
- Console.WriteLine (e.ToString () + "=>" + result);
- }
+ result = e.Eval (domain);
+ Console.WriteLine (result);
} finally {
- Console.WriteLine (")");
domain.UnboundTo (current);
}
return result;
public readonly Name type;
internal object val;
- public Variable (Name name, Name type, Xex value)
+ public Variable (Name name, Name type, object value)
{
if (value != null)
Value = value;
public VarBool (Name name, object value)
: base (name, Nboolean, value) { }
- public override bool ValueP (Xex value)
+ public override bool ValueP (object value)
{
if (! (value is bool))
return false;
public Domain (Domain parent, object context)
{
functions = new Dictionary<Name, Function> (parent.functions);
- variables = new Dictionary<Name, Function> (parent.variables);
+ variables = new Dictionary<Name, Variable> (parent.variables);
this.context = context;
}
internal void UnboundTo (Bindings boundary)
{
- bindings = bindings.UnboundTo (boundary);
+ if (bindings != null)
+ bindings = bindings.UnboundTo (boundary);
}
public void Defun (Name name, Builtin builtin, int min_arg, int max_arg)
internal Function Defun (XmlNode node)
{
+ Name name = node.Attributes[Nfname].Value;
Function func;
if (! functions.TryGetValue (name, out func))
func = RegisterFunction (node);
func.Setup (node, this);
+ return func;
}
public void Defvar (Name name, XmlNode node)
Function func;
if (! functions.TryGetValue (name, out func))
- {
- if (parent != null)
- return parent.GetFunc (name);
- throw new Exception ("Unknown function: " + name);
- }
+ throw new Exception ("Unknown function: " + name);
return func;
}
basic.Defun (">", greater_than, 2, -1, false);
basic.Defun ("ge", greater_eq, 2, -1, false);
basic.Defun (">=", greater_eq, 2, -1, false);
+ basic.Defun ("eval", eval_clause, 1, 1, true);
basic.Defun ("progn", progn_clause, 0, -1, true);
basic.Defun ("expr", progn_clause, 0, -1, true);
basic.Defun ("if", if_clause, 2, -1, true);
return n;
}
- private static object mul (Xex[] args, Domain domain)
- {
- int n = 1;
- foreach (Xex e in args)
- n *= (int) e.val;
- return n;
- }
-
- private static object sub (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- if (args.Length == 1)
- return - n;
- for (int i = 1; i < args.Length; i++)
- n -= (int) args[i].val;
- return n;
- }
+ private static object mul (Xex[] args, Domain domain)
+ {
+ int n = 1;
+ foreach (Xex e in args)
+ n *= (int) e.val;
+ return n;
+ }
- private static object div (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- for (int i = 1; i < args.Length; i++)
- n /= (int) args[i].val;
- return n;
- }
+ private static object sub (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ if (args.Length == 1)
+ return - n;
+ for (int i = 1; i < args.Length; i++)
+ n -= (int) args[i].val;
+ return n;
+ }
- private static object mod (Xex[] args, Domain domain)
- {
- return ((int) args[0].val % (int) args[1].val);
- }
+ private static object div (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ for (int i = 1; i < args.Length; i++)
+ n /= (int) args[i].val;
+ return n;
+ }
- private static object logior (Xex[] args, Domain domain)
- {
- int n = 0;
- foreach (Xex e in args)
- n |= (int) e.val;
- return n;
- }
+ private static object mod (Xex[] args, Domain domain)
+ {
+ return ((int) args[0].val % (int) args[1].val);
+ }
- private static object logand (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- for (int i = 1; i < args.Length; i++)
- n &= (int) args[i].val;
- return n;
- }
+ private static object logior (Xex[] args, Domain domain)
+ {
+ int n = 0;
+ foreach (Xex e in args)
+ n |= (int) e.val;
+ return n;
+ }
- private static object add_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ private static object logand (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ for (int i = 1; i < args.Length; i++)
+ n &= (int) args[i].val;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n += (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object add_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object mul_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n += (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n *= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object mul_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object sub_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n *= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n -= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object sub_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object div_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n -= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n /= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object div_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object mod_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n /= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n %= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object mod_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object logior_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n %= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n |= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object logior_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object logand_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ for (int i = 1; i < args.Length; i++)
+ n |= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- n &= (int) args[i].val;
- vari.val = n;
- return n;
- }
+ private static object logand_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object lsh (Xex[] args, Domain domain)
- {
- return (int) args[0].val << (int) args[1].val;
- }
+ for (int i = 1; i < args.Length; i++)
+ n &= (int) args[i].val;
+ vari.val = n;
+ return n;
+ }
- private static object lsh_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ private static object lsh (Xex[] args, Domain domain)
+ {
+ return (int) args[0].val << (int) args[1].val;
+ }
- n <<= (int) args[1].val;
- vari.val = n;
- return n;
- }
+ private static object lsh_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object rsh (Xex[] args, Domain domain)
- {
- return (int) args[0].val >> (int) args[1].val;
- }
+ n <<= (int) args[1].val;
+ vari.val = n;
+ return n;
+ }
- private static object rsh_set (Xex[] args, Domain domain)
- {
- Variable vari = domain.GetVar (args[0]);
- int n = (int) vari.val;
+ private static object rsh (Xex[] args, Domain domain)
+ {
+ return (int) args[0].val >> (int) args[1].val;
+ }
- n >>= (int) args[1].val;
- vari.val = n;
- return n;
- }
+ private static object rsh_set (Xex[] args, Domain domain)
+ {
+ Variable vari = domain.GetVar (args[0]);
+ int n = (int) vari.val;
- private static object eq (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
+ n >>= (int) args[1].val;
+ vari.val = n;
+ return n;
+ }
- for (int i = 1; i < args.Length; i++)
- if (n != (int) args[i].val)
- return false;
- return true;
- }
+ private static object eq (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
- private static object noteq (Xex[] args, Domain domain)
- {
- return ((int) args[0].val != (int) args[1].val);
- }
+ for (int i = 1; i < args.Length; i++)
+ if (n != (int) args[i].val)
+ return false;
+ return true;
+ }
- private static object less_than (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
+ private static object noteq (Xex[] args, Domain domain)
+ {
+ return ((int) args[0].val != (int) args[1].val);
+ }
- for (int i = 1; i < args.Length; i++)
- {
- int n1 = (int) args[i].val;
- if (n >= n1)
- return false;
- n = n1;
- }
- return true;
- }
+ private static object less_than (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
- private static object less_eq (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- for (int i = 1; i < args.Length; i++)
- {
- int n1 = (int) args[i].val;
- if (n > n1)
- return false;
- n = n1;
- }
- return true;
- }
+ for (int i = 1; i < args.Length; i++)
+ {
+ int n1 = (int) args[i].val;
+ if (n >= n1)
+ return false;
+ n = n1;
+ }
+ return true;
+ }
- private static object greater_than (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- for (int i = 1; i < args.Length; i++)
- {
- int n1 = (int) args[i].val;
- if (n <= n1)
- return false;
- n = n1;
- }
- return true;
- }
+ private static object less_eq (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ for (int i = 1; i < args.Length; i++)
+ {
+ int n1 = (int) args[i].val;
+ if (n > n1)
+ return false;
+ n = n1;
+ }
+ return true;
+ }
- private static object greater_eq (Xex[] args, Domain domain)
- {
- int n = (int) args[0].val;
- for (int i = 1; i < args.Length; i++)
- {
- int n1 = (int) args[i].val;
- if (n < n1)
- return false;
- n = n1;
- }
- return true;
- }
+ private static object greater_than (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ for (int i = 1; i < args.Length; i++)
+ {
+ int n1 = (int) args[i].val;
+ if (n <= n1)
+ return false;
+ n = n1;
+ }
+ return true;
+ }
- private static object progn_clause (Xex[] args, Domain domain)
- {
- object result = true;
+ private static object greater_eq (Xex[] args, Domain domain)
+ {
+ int n = (int) args[0].val;
+ for (int i = 1; i < args.Length; i++)
+ {
+ int n1 = (int) args[i].val;
+ if (n < n1)
+ return false;
+ n = n1;
+ }
+ return true;
+ }
- foreach (Xex e in args)
- result = e.Eval (domain);
- return result;
- }
+ private static object eval_clause (Xex[] args, Domain domain)
+ {
+ return args[0].Eval (domain);
+ }
- private static object if_clause (Xex[] args, Domain domain)
- {
- object result;
+ private static object progn_clause (Xex[] args, Domain domain)
+ {
+ object result = true;
- if (is_true (args[0].Eval (domain)))
- result = args[1].Eval (domain);
- else
- {
- result = false;
- for (int i = 2; i < args.Length; i++)
- result = args[i].Eval (domain);
- }
- return result;
- }
+ foreach (Xex e in args)
+ result = e.Eval (domain);
+ return result;
+ }
- private static object when_clause (Xex[] args, Domain domain)
- {
- if (! is_true (args[0].Eval (domain)))
- return false;
+ private static object if_clause (Xex[] args, Domain domain)
+ {
+ object result;
- object result = true;
- for (int i = 1; i < args.Length; i++)
- result = args[i].Eval (domain);
- return result;
- }
+ if (is_true (args[0].Eval (domain)))
+ result = args[1].Eval (domain);
+ else
+ {
+ result = false;
+ for (int i = 2; i < args.Length; i++)
+ result = args[i].Eval (domain);
+ }
+ return result;
+ }
- private static object while_clause (Xex[] args, Domain domain)
- {
- while (is_true (args[0].Eval (domain)))
- for (int i = 1; i < args.Length; i++)
- args[i].Eval (domain);
+ private static object when_clause (Xex[] args, Domain domain)
+ {
+ if (! is_true (args[0].Eval (domain)))
return false;
- }
+
+ object result = true;
+ for (int i = 1; i < args.Length; i++)
+ result = args[i].Eval (domain);
+ return result;
+ }
+
+ private static object while_clause (Xex[] args, Domain domain)
+ {
+ while (is_true (args[0].Eval (domain)))
+ for (int i = 1; i < args.Length; i++)
+ args[i].Eval (domain);
+ return false;
+ }
// FUNCALL: function != null
// VARREF: function == null, args[0] = DIRECT-SYMBOL
this.val = val;
}
- private static Xex macro_expand (Xex[] bindings, Name[] args, Xex e)
- {
- Xex xex;
-
- if (e.function != null)
- {
- xex = new Xex ();
- xex.function = e.function;
- xex.args = new Xex[e.args.Length];
- for (int i = e.args.Length - 1; i >= 0; i--)
- xex.args[i] = macro_expand (bindings, args, e.args[i]);
- }
- else if (e.args != null)
- {
- xex = new Xex ();
- Name name = (Name) e.args[0].val;
- for (int i = args.Length - 1; i >= 0; i--)
- if (args[i] == name)
- return bindings[i];
- xex.function = null;
- xex.args = new Xex[1];
- xex.args[0] = e.args[0];
- }
- else
- {
- xex = e;
- }
- return xex;
- }
-
internal static int parse_integer (string str)
{
int len = str.Length;
}
else
{
- bool is_macro = false;
-
if (name == Nfuncall)
- {
- name = node.Attributes[0].Value;
- if (Nmname == node.Attributes[0].Name)
- is_macro = true;
- }
-
+ name = node.Attributes[0].Value;
function = domain.GetFunc (name);
XmlNodeList nlist = node.ChildNodes;
nargs = pre_parse (nlist, domain);
args = new Xex[nargs];
post_parse (nlist, domain);
-
- if (is_macro)
- {
- Function.Lambda lambda = function.lambda;
- Xex[] body = lambda.body;
- int len = body.Length;
- Xex[] newargs = new Xex[len];
-
- for (int i = 0; i < len; i++)
- newargs[i] = macro_expand (args, lambda.args, body[i]);
- function = Fprogn;
- args = newargs;
- }
}
}
reader.Read ();
} while (reader.NodeType != XmlNodeType.None
&& (reader.NodeType != XmlNodeType.Element
- || Nexpr != reader.Name));
+ || Nexpr != reader.Name));
if (reader.NodeType == XmlNodeType.None)
throw new Exception ("Node <expr> not found");
node = doc.ReadNode (reader);