namespace System.Xml.Expression
{
-public class XmlName : IEquatable<XmlName>
+ public class Xex
{
- private static NameTable nt = new NameTable ();
+ public class Name : IEquatable<Name>
+ {
+ private static NameTable nt = new NameTable ();
+
+ private object name;
+
+ private Name (string str)
+ {
+ name = nt.Add (str);
+ }
+
+ private Name (XmlAttribute attr)
+ {
+ name = attr.Value;
+ }
- private object name;
+ private Name (XmlNode node)
+ {
+ name = node.Name;
+ }
- private XmlName (string str)
+ public static implicit operator Name (string str)
{
- name = nt.Add (str);
+ return new Name (str);
}
- public static implicit operator XmlName (string str)
- {
- return new XmlName (str);
- }
-
- public static bool operator== (XmlName n1, XmlName n2)
+ public static implicit operator string (Name name)
{
- return n1.name == n2.name;
+ return (string) name.name;
}
- public static bool operator!= (XmlName n1, XmlName n2)
+ public static bool operator== (Name n1, Name n2)
+ {
+ return n1.name == n2.name;
+ }
+
+ public static bool operator!= (Name n1, Name n2)
+ {
+ return n1.name != n2.name;
+ }
+
+ public bool Equals (Name other) { return name == other.name; }
+ public override bool Equals (object obj) { return obj == name; }
+ public override int GetHashCode ()
{
- return n1.name != n2.name;
+ return ((string) name).GetHashCode ();
}
- public bool Equals (XmlName other) { return name == other.name; }
- public override bool Equals (object obj) { return obj == name; }
- public override int GetHashCode ()
- {
- return ((string) name).GetHashCode ();
+ public static NameTable Table { get { return nt; } }
}
- public static NameTable Table { get { return nt; } }
- }
-
- public class Xex
- {
- private static NameTable nt = new NameTable ();
+ private static Name Nvalue = "value";
+ private static Name Nvar = "var";
+ private static Name Ndefun = "defun";
+ private static Name Ncond = "cond";
+ private static Name Nprogn = "progn";
+ private static Name Nwhen = "when";
- public static object Name (string str)
+ public class Bindings
{
- return nt.Add (str);
- }
+ private Name name;
+ private object value;
+ private Bindings next;
+
+ internal Bindings (Name name, object value)
+ {
+ this.name = name;
+ this.value = value;
+ }
+
+ internal Bindings ()
+ {
+ }
+
+ internal Bindings Bind (Name name, object value)
+ {
+ Bindings bindings = new Bindings (name, value);
+ bindings.next = this;
+ return bindings;
+ }
- public static NameTable Table { get { return nt; } }
+ internal Bindings SetBoundary ()
+ {
+ Bindings bindings = new Bindings ();
+ bindings.next = this;
+ return this;
+ }
- private static object Nvar = Name ("var");
- private static object Ndefun = Name ("defun");
- private static object Ncond = Name ("cond");
- private static object Nprogn = Name ("progn");
- private static object Nwhen = Name ("when");
+ internal Bindings Pop ()
+ {
+ return bindings.next;
+ }
+
+ public override string ToString ()
+ {
+ string str = "(bindings";
+ for (Bindings b = this; b != null; b = b.next)
+ str += " " + name + ":" + value;
+ return str + ")";
+ }
+ }
public class Domain
{
private Domain parent;
- internal Dictionary<object,functions = new MPlist ();
- internal MPlist bindings = new MPlist ();
- private Stack<MPlist> boundaries = new Stack<MPlist> ();
- internal Translator translator;
public object context;
- internal Domain ()
- {
- boundaries.Push (bindings);
- }
+ internal Dictionary<Name, Function> functions
+ = new Dictionary<Name, Function> ();
+ private Bindings bindings;
+
+ internal Domain () { }
public Domain (object context) : this (basic, context) { }
{
this.parent = parent;
this.context = context;
- boundaries.Push (bindings);
}
- public void AddTranslator (Translator translator)
+ public void Bind (Name name, object value)
{
- if (this.translator == null)
- this.translator = translator;
+ if (bindings == null)
+ bindings = new Bindings (name, value);
else
- this.translator += translator;
+ bindings = bindings.Bind (name, value);
}
- public void Bind (MSymbol sym, object value)
- {
- bindings = bindings.Cons (sym, value);
- }
-
- public MPlist SetBoundary ()
+ public Bindings SetBoundary ()
{
boundaries.Push (bindings);
return bindings;
}
- public void UnboundTo (MPlist boundary)
+ public void UnboundTo (Bindings boundary)
{
while (boundary != boundaries.Pop ());
- while (bindings != boundary)
- bindings = bindings.next;
+ while (bindings.Pop != boundary);
}
- public void Defun (string name, Builtin builtin, int min_arg, int max_arg)
+ public void Defun (Name name, Builtin builtin, int min_arg, int max_arg)
{
Defun (name, builtin, min_arg, max_arg, false);
}
- public void Defun (string name, Builtin builtin, int min_arg, int max_arg,
+ public void Defun (Name name, Builtin builtin, int min_arg, int max_arg,
bool specialp)
{
- MSymbol sym = name;
- Function func = (Function) functions.Get (sym);
+ Function func;
- if (func != null)
+ if (functions.TryGetValue (name, out func))
{
if (func.min_arg < min_arg || func.max_arg > max_arg)
throw new Exception ("Incompatible argument numbers to override: "
}
else
{
- func = new Function (sym, builtin, min_arg, max_arg, specialp);
- functions = functions.Cons (sym, func);
+ functions[name]
+ = new Function (name, builtin, min_arg, max_arg, specialp);
}
}
- public void Defun (MSymbol sym, MPlist args, MPlist body)
- {
- Function func = (Function) functions.Get (sym);
-
- if (func != null)
- {
- int nargs = args == null ? 0 : args.Count;
-
- if (func.min_arg < nargs || func.max_arg > nargs)
- throw new Exception ("Incompatible argument numbers to override: "
- + sym);
- func.lambda.SetArgs (args);
- func.lambda.SetBody (body, this);
- func.builtin = null;
- }
- else
- {
- func = new Function (sym, args, body, this);
- functions = functions.Cons (sym, func);
- }
- }
-
public void Defun (XmlNode node)
{
- MSymbol sym = node.Attributes["id"].Value;
- Function func = (Function) functions.Get (sym);
+ Name name = node.Attributes["id"];
+ Function func;
- if (func != null)
+ if (functions.TryGetValue (name, out func))
{
XmlNode args = node.FirstChild;
int nargs = args.Name == "args" ? args.ChildNodes.Count : 0;
}
}
- internal Function GetFunc (MSymbol name)
+ internal Function GetFunc (Name name)
{
Function func = (Function) functions.Get (name);
return func;
}
- public bool CopyFunc (Domain domain, MSymbol name)
+ public bool CopyFunc (Domain domain, Name name)
{
Function func = (Function) functions.Get (name);
if (func == null)
domain.functions = domain.functions.Cons (p.key, p.val);
}
- public object GetValue (MSymbol name)
+ public object GetValue (Name name)
{
MPlist slot = bindings.Find (name);
return slot.val;
}
- public object SetValue (MSymbol name, object val)
+ public object SetValue (Name name, object val)
{
MPlist boundary = boundaries.Peek ();
return val;
}
- public bool IsBound (MSymbol name)
+ public bool IsBound (Name name)
{
return (bindings.Find (name) != null);
}
- public void Translate (MPlist plist)
- {
- if (parent != null)
- parent.Translate (plist);
- if (translator != null)
- for (MPlist p = plist; ! p.IsEmpty; p = p.next)
- translator (p, this);
- }
-
public override string ToString ()
{
string str = "<(functions";
- foreach (MPlist p in functions)
- str += " " + p.key;
- str += ") (bindings " + bindings + ")";
+ foreach (KeyValuePair<Name, Function> kv in functions)
+ str += " " + kv.Key;
+ str += ") " + bindings;
if (context != null)
str += " (" + context + ")";
str += ">";
}
}
- public delegate object Builtin (MExpression[] args, Domain domain);
- public delegate void Translator (MPlist plist, Domain domain);
+ public delegate object Builtin (Xex[] args, Domain domain);
internal class Function
{
internal class Lambda
{
- internal MSymbol[] args;
- internal MExpression[] body;
-
- public Lambda (MPlist args, MPlist body, Domain domain)
- {
- SetArgs (args);
- SetBody (body, domain);
- }
+ internal Name[] args;
+ internal Xex[] body;
public Lambda (XmlNode node, Domain domain)
{
Set (node, domain);
}
- public void SetArgs (MPlist args)
- {
- int len = args == null ? 0 : args.Count;
-
- if (this.args == null)
- this.args = new MSymbol[len];
- for (int i = 0; i < len; i++, args = args.next)
- this.args[i] = args.Symbol;
- }
-
- public void SetBody (MPlist body, Domain domain)
- {
- int len = body == null ? 0 : body.Count;
- if (this.body == null)
- this.body = new MExpression[len];
- for (int i = 0; i < len; i++, body = body.next)
- {
- domain.Translate (body);
- this.body[i] = new MExpression (body.key, body.val, domain);
- }
- }
-
public void Set (XmlNode node, Domain domain)
{
XmlNodeList body = node.ChildNodes;
{
XmlNodeList args = body[0].ChildNodes;
if (this.args == null)
- this.args = new MSymbol[args.Count];
+ this.args = new Name[args.Count];
for (int i = 0; i < args.Count; i++)
this.args[i] = args[i].InnerText;
idx++;
}
else if (this.args == null)
- this.args = new MSymbol[0];
+ this.args = new Name[0];
if (this.body == null)
- this.body = new MExpression[body.Count - idx];
+ this.body = new Xex[body.Count - idx];
for (int i = 0; idx < body.Count; i++, idx++)
- this.body[i] = new MExpression (body[idx], domain);
+ this.body[i] = new Xex (body[idx], domain);
}
}
- public readonly MSymbol Name;
+ public readonly Name Name;
public Builtin builtin;
public int min_arg, max_arg;
internal Lambda lambda;
internal static Function ignore, varref, block;
- public Function (MSymbol name, Builtin builtin,
+ public Function (Name name, Builtin builtin,
int min_arg, int max_arg, bool specialp)
{
Name = name;
this.specialp = specialp;
}
- internal Function (MSymbol name, MPlist args, MPlist body,
- Domain domain)
- {
- Name = name;
- lambda = new Lambda (args, body, domain);
- this.min_arg = this.max_arg = lambda.args.Length;
- }
-
- internal Function (MSymbol name, XmlNode node, Domain domain)
+ internal Function (Name name, XmlNode node, Domain domain)
{
Name = name;
lambda = new Lambda (node, domain);
private Function ()
{
- Name = MSymbol.nil;
+ Name = Name.nil;
}
static Function ()
block = new Function (Mprogn, progn, 0, -1, true);
}
- private static object get_value (MExpression[] args, Domain domain)
+ private static object get_value (Xex[] args, Domain domain)
{
- return domain.GetValue ((MSymbol) args[0].val);
+ return domain.GetValue ((Name) args[0].val);
}
- public object Call (MExpression[] args, Domain domain)
+ public object Call (Xex[] args, Domain domain)
{
if (builtin != null)
{
if (! specialp)
- foreach (MExpression e in args)
+ foreach (Xex e in args)
e.Eval (domain);
return builtin (args, domain);
}
if (lambda == null)
return null;
- MPlist orig_bindings = domain.bindings;
+ Bindings orig_bindings = domain.bindings;
object result = false;
try {
int i = 0;
- foreach (MSymbol arg in lambda.args)
+ foreach (Name arg in lambda.args)
domain.Bind (arg, args[i++].Eval (domain));
- foreach (MExpression e in lambda.body)
+ foreach (Xex e in lambda.body)
result = e.Eval (domain);
} finally {
domain.bindings = orig_bindings;
private static Domain basic;
- static MExpression ()
+ static Xex ()
{
basic = new Domain ();
basic.AddTranslator (new Translator (translate_cond));
}
- private static object set_value (MExpression[] args, Domain domain)
+ private static object set_value (Xex[] args, Domain domain)
{
- return domain.SetValue ((MSymbol) args[0].args[0].val,
+ return domain.SetValue ((Name) args[0].args[0].val,
args[1].Eval (domain));
}
- private static object not (MExpression[] args, Domain domain)
+ private static object not (Xex[] args, Domain domain)
{
if (args[0].val is int)
return (int) args[0].val == 0;
return true;
}
- private static object plus (MExpression[] args, Domain domain)
+ private static object plus (Xex[] args, Domain domain)
{
if (args[0].val is int)
{
int n = 0;
- foreach (MExpression e in args)
+ foreach (Xex e in args)
n += (int) e.val;
return n;
}
else if (args[0].val is MText)
{
MText mt = new MText ();
- foreach (MExpression e in args)
+ foreach (Xex e in args)
mt += (MText) e.val;
return mt;
}
throw new Exception ("Not an integer nor MText: " + args[0].val);
}
- private static object multi (MExpression[] args, Domain domain)
+ private static object multi (Xex[] args, Domain domain)
{
int n = 1;
- foreach (MExpression e in args)
+ foreach (Xex e in args)
n *= (int) e.val;
return n;
}
- private static object minus (MExpression[] args, Domain domain)
+ private static object minus (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
if (args.Length == 1)
return n;
}
- private static object divide (MExpression[] args, Domain domain)
+ private static object divide (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
for (int i = 1; i < args.Length; i++)
return n;
}
- private static object percent (MExpression[] args, Domain domain)
+ private static object percent (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
for (int i = 1; i < args.Length; i++)
return n;
}
- private static object logior (MExpression[] args, Domain domain)
+ private static object logior (Xex[] args, Domain domain)
{
int n = 0;
- foreach (MExpression e in args)
+ foreach (Xex e in args)
n |= (int) e.val;
return n;
}
- private static object logand (MExpression[] args, Domain domain)
+ private static object logand (Xex[] args, Domain domain)
{
int n = 0;
- foreach (MExpression e in args)
+ foreach (Xex e in args)
n &= (int) e.val;
return n;
}
- private static object pluseq (MExpression[] args, Domain domain)
+ private static object pluseq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
object val = domain.GetValue (sym);
if (val is int)
return val;
}
- private static object multieq (MExpression[] args, Domain domain)
+ private static object multieq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object minuseq (MExpression[] args, Domain domain)
+ private static object minuseq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object divideeq (MExpression[] args, Domain domain)
+ private static object divideeq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object percenteq (MExpression[] args, Domain domain)
+ private static object percenteq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object logioreq (MExpression[] args, Domain domain)
+ private static object logioreq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object logandeq (MExpression[] args, Domain domain)
+ private static object logandeq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
for (int i = 1; i < args.Length; i++)
return domain.SetValue (sym, (object) n);
}
- private static object lshift (MExpression[] args, Domain domain)
+ private static object lshift (Xex[] args, Domain domain)
{
return (int) args[0].val << (int) args[1].val;
}
- private static object lshifteq (MExpression[] args, Domain domain)
+ private static object lshifteq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
n <<= (int) args[1].Eval (domain);
return domain.SetValue (sym, (object) n);
}
- private static object rshift (MExpression[] args, Domain domain)
+ private static object rshift (Xex[] args, Domain domain)
{
return (int) args[0].val >> (int) args[1].val;
}
- private static object rshifteq (MExpression[] args, Domain domain)
+ private static object rshifteq (Xex[] args, Domain domain)
{
- MSymbol sym = (MSymbol) args[0].args[0].val;
+ Name sym = (Name) args[0].args[0].val;
int n = (int) domain.GetValue (sym);
n >>= (int) args[1].Eval (domain);
return domain.SetValue (sym, (object) n);
}
- private static object eq (MExpression[] args, Domain domain)
+ private static object eq (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
return true;
}
- private static object noteq (MExpression[] args, Domain domain)
+ private static object noteq (Xex[] args, Domain domain)
{
return ((int) args[0].val != (int) args[1].val);
}
- private static object less (MExpression[] args, Domain domain)
+ private static object less (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
return true;
}
- private static object lesseq (MExpression[] args, Domain domain)
+ private static object lesseq (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
for (int i = 1; i < args.Length; i++)
return true;
}
- private static object more (MExpression[] args, Domain domain)
+ private static object more (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
for (int i = 1; i < args.Length; i++)
return true;
}
- private static object moreeq (MExpression[] args, Domain domain)
+ private static object moreeq (Xex[] args, Domain domain)
{
int n = (int) args[0].val;
for (int i = 1; i < args.Length; i++)
return true;
}
- private static object progn (MExpression[] args, Domain domain)
+ private static object progn (Xex[] args, Domain domain)
{
object result = false;
- foreach (MExpression e in args)
+ foreach (Xex e in args)
result = e.Eval (domain);
return result;
}
- private static bool check_condition (MExpression condition, Domain domain)
+ private static bool check_condition (Xex condition, Domain domain)
{
object result = condition.Eval (domain);
return (result is bool ? (bool) result
: true);
}
- private static object ifclause (MExpression[] args, Domain domain)
+ private static object ifclause (Xex[] args, Domain domain)
{
object result = false;
return result;
}
- private static object whenclause (MExpression[] args, Domain domain)
+ private static object whenclause (Xex[] args, Domain domain)
{
object result = false;
return result;
}
- private static object whileclause (MExpression[] args, Domain domain)
+ private static object whileclause (Xex[] args, Domain domain)
{
while (check_condition (args[0], domain))
for (int i = 1; i < args.Length; i++)
return false;
}
- // (cond (COND1 ...) (COND2 ...) ...)
- // => (cond (when COND1 ...) (when COND2 ...) ...)
- private static void translate_cond (MPlist plist, Domain domain)
- {
- if (plist.IsPlist)
- {
- plist = plist.Plist;
- if (plist.IsSymbol && plist.Symbol == Mcond)
- {
- plist.val = Mprogn;
- for (plist = plist.next; ! plist.IsEmpty; plist = plist.next)
- {
- if (! plist.IsPlist)
- throw new Exception ("Invalid cond form: " + plist);
- plist.Plist.Push (MSymbol.symbol, Mwhen);
- }
- }
- }
- }
+ // FUNCALL: function != null
+ // VARREF: function == null, args[0] = DIRECT-SYMBOL
+ // DIRECT: function == null, args == null
private Function function;
- private MExpression[] args;
+ private Xex[] args;
private object val;
- public MExpression[] Args { get { return args; } }
+ public Xex[] Args { get { return args; } }
public object Val { get { return val; } }
- private MExpression (object val)
+ private Xex (object val)
{
this.val = val;
}
// EXPR = SYMBOL | MTEXT | INTEGER | FUNCALL | PROGN
// FUNCALL = '(' SYMBOL EXPR* ')'
// PROGN = '(' EXPR * ')'
- private MExpression (MSymbol key, object val, Domain domain)
+ private Xex (XmlNode node, Domain domain)
{
- if (key == MSymbol.symbol)
+ object name = node.Name;
+
+ if (name == Nvalue)
{
- function = Function.varref;
- args = new MExpression[1];
- args[0] = new MExpression (val);
+ object type = node.Attributes["type"];
+
+ if (type == Ninteger)
+ val = parse_integer (node.InnerText);
+ else if (type == Nstring)
+ val = node.InnerText;
+ else if (type == Nsymbol)
+ val = (Name) node.InnerText;
+ else if (type == Nboolean)
+ val = node.InnerText == "true";
+ else if (type == Nlist)
+ {
+ List<Xex> list = new List<Xex>;
+ for (XmlNode n = node.FirstChild; n != null; n = n.NextSibling)
+ list.Add (new Xex (n, domain));
+ val = list;
+ }
+ else
+ throw new Exception ("Unknown type: " + type);
}
- else if (key == MSymbol.integer
- || key == MSymbol.mtext)
+ else if (name = Nval)
{
- this.val = val;
+ name = name.Attributes[];
+
+ args = new Xex[1];
+ args[0] = new Xex (
+
+
}
- else if (key == MSymbol.plist)
+ else if (key == Name.plist)
{
MPlist p = (MPlist) val;
if (p.IsSymbol)
{
- MSymbol sym = p.Symbol;
+ Name sym = p.Symbol;
if (sym == Mdefun)
{
throw new Exception ("Invalid function argument: "
+ p.val);
domain.Defun (sym, p.Plist, p.next);
- // This Mexpression is just a dummy.
+ // This Xex is just a dummy.
}
else
{
|| (function.max_arg >= 0 && nargs > function.max_arg))
throw new Exception ("Invalid number of arguments to: "
+ sym + " " + nargs);
- args = new MExpression[nargs];
+ args = new Xex[nargs];
for (int i = 0; i < nargs; i++, p = p.next)
{
domain.Translate (p);
- args[i] = new MExpression (p.key, p.val, domain);
+ args[i] = new Xex (p.key, p.val, domain);
}
}
}
else
{
- args = new MExpression[1];
- args[0] = new MExpression (p, domain);
+ args = new Xex[1];
+ args[0] = new Xex (p, domain);
}
}
else
throw new Exception ("Invalid expression: " + key + ":" + val);
}
- public MExpression (MPlist plist, Domain domain)
- {
- function = Function.block;
- args = new MExpression[plist.Count];
- for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next)
- {
- domain.Translate (plist);
- args[i] = new MExpression (plist.key, plist.val, domain);
- }
- }
-
- public MExpression (XmlNode node, Domain domain)
+ public Xex (XmlNode node, Domain domain)
{
- MSymbol sym = node.Name;
+ Name sym = node.Name;
function = domain.GetFunc (sym);
if (nargs < function.min_arg
|| (function.max_arg >= 0 && nargs > function.max_arg))
throw new Exception ("Invalid number of arguments: " + node.InnerXml);
- args = new MExpression[nargs];
+ args = new Xex[nargs];
int i;
for (i = 0; i < attrs.Count; i++)
{
- string str = sttrs[i].Value;
+ string str = attrs[i].Value;
}
}
{
if (function == null)
{
- if (val is MSymbol)
- val = domain.GetValue ((MSymbol) val);
+ if (val is Name)
+ val = domain.GetValue ((Name) val);
}
else
val = function.Call (args, domain);
{
str = "(" + function.Name.Name;
if (args != null)
- foreach (MExpression e in args)
+ foreach (Xex e in args)
str += " " + e.ToString ();
str += ")";
}