From: handa Date: Mon, 14 Sep 2009 23:44:03 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=139bdc3719eee81aa203e40f40c612fb799b8ed4;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/XmlExpr.cs b/XmlExpr.cs index 33439d6..610e055 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -81,7 +81,6 @@ namespace System.Xml.Expression 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"; @@ -91,6 +90,12 @@ namespace System.Xml.Expression 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"; @@ -99,7 +104,6 @@ namespace System.Xml.Expression private static Name Nfuncall = "funcall"; private static Name Nprogn = "progn"; - private static Name Neval = "eval"; internal class Function { @@ -108,14 +112,13 @@ namespace System.Xml.Expression 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) @@ -124,7 +127,7 @@ namespace System.Xml.Expression 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); } } @@ -167,7 +170,7 @@ namespace System.Xml.Expression 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; @@ -177,14 +180,14 @@ namespace System.Xml.Expression 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; @@ -199,20 +202,21 @@ namespace System.Xml.Expression 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++) @@ -222,17 +226,16 @@ namespace System.Xml.Expression 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; @@ -250,7 +253,7 @@ namespace System.Xml.Expression 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; @@ -328,7 +331,7 @@ namespace System.Xml.Expression 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; @@ -419,7 +422,7 @@ namespace System.Xml.Expression public Domain (Domain parent, object context) { functions = new Dictionary (parent.functions); - variables = new Dictionary (parent.variables); + variables = new Dictionary (parent.variables); this.context = context; } @@ -430,7 +433,8 @@ namespace System.Xml.Expression 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) @@ -473,11 +477,13 @@ namespace System.Xml.Expression 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) @@ -553,11 +559,7 @@ namespace System.Xml.Expression 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; } @@ -673,6 +675,7 @@ namespace System.Xml.Expression 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); @@ -726,270 +729,275 @@ namespace System.Xml.Expression 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 @@ -1009,36 +1017,6 @@ namespace System.Xml.Expression 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; @@ -1147,15 +1125,8 @@ namespace System.Xml.Expression } 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; @@ -1168,19 +1139,6 @@ namespace System.Xml.Expression 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; - } } } @@ -1195,7 +1153,7 @@ namespace System.Xml.Expression 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 not found"); node = doc.ReadNode (reader); diff --git a/xex.xml b/xex.xml index 09f7c34..6f84e29 100644 --- a/xex.xml +++ b/xex.xml @@ -13,7 +13,7 @@ x 10 - + 1