From 41ffade8cdc252f9c93ac30cda678bedd0def48d Mon Sep 17 00:00:00 2001 From: handa Date: Sun, 1 Nov 2009 10:55:23 +0000 Subject: [PATCH] *** empty log message *** --- MInputMethod.cs | 38 +++ XmlExpr.cs | 999 +++++++++++++++++++++++++++++-------------------------- xex.xml | 2 - 3 files changed, 572 insertions(+), 467 deletions(-) diff --git a/MInputMethod.cs b/MInputMethod.cs index ff6a974..ebb8277 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -2132,6 +2132,44 @@ namespace M17N.Input return terms; } + private Xex.Variable[] parse_args (Xex.Domain domain, Xex.Symbol[] args) + { + int nfixed = 0; + int noptional = 0; + int nrest = 0; + + if (args.Length > 0) + { + int i = 0; + for (i = 0; i < args.Length; i++, nfixed++) + if (args[i] == Qoptional || args[i] == Qrest) + break; + if (i < args.Length) + { + if (args[i] == Qoptional) + { + for (i++; i < args.Length; i++, noptional++) + if (args[i] == Qrest) + break; + if (i < args.Length) + nrest = 1; + } + } + min_args = nfixed; + max_args = nfixed + noptional + nrest; + this.args = new Variable[max_args]; + int j; + for (i = j = 0; j < this.args.Length; i++) + if (args[i] != Qoptional || args[i] != Qrest) + this.args[j++] = domain.Defvar (args[i]); + } + else + { + min_args = max_args = 0; + } + with_var = false; + } + private void parse_macros (MPlist plist) { for (MPlist pl = plist; ! pl.IsEmpty; pl = pl.next) diff --git a/XmlExpr.cs b/XmlExpr.cs index 53c7641..906057b 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -7,7 +7,7 @@ using System.Xml; namespace System.Xml { - public class Xexpression + public static class Xexpression { private static int trace_depth = 0; @@ -16,6 +16,62 @@ namespace System.Xml set { trace_depth = value; } } + public static partial class Error + { + // Load time errors. + public static Symbol UnknownFunction = "unknown-function"; + public static Symbol TypeConflict = "type-conflict"; + public static Symbol AlreadyDefined = "already-defined"; + public static Symbol WrongCallingFormat = "wrong-calling-format"; + public static Symbol ArgumentMismatch = "argument-mismatch"; + + // Run time errors. + public static Symbol ArithmeticError = "arithmetic-error"; + public static Symbol WrongType = "wrong-type"; + public static Symbol WrongRange = "wrong-range"; + public static Symbol OutOfRange = "out-of-range"; + + public static Symbol InvalidVariableType = "invalid variable type"; + public static Symbol NoOuterLoop = "no outer loop"; + public static Symbol UnknownError = "unknown error"; + } + + public class Exception : System.Exception + { + public Symbol ErrorName; + public string ErrorNode; + public Term ErrorTerm; + + internal Exception (Symbol name, string fmt, params object[] args) + : this (name, Zero, fmt, args) { } + + internal Exception (Symbol name, XmlNode node, + string fmt, params object[] args) + : this (name, node, Zero, fmt, args) { } + + internal Exception (Symbol name, Term term, + string fmt, params object[] args) + : this (name, null, term, fmt, args) { } + + internal Exception (Symbol name, XmlNode node, Term term, + string fmt, params object[] args) + : base (String.Format (message, args)) + { + ErrorName = name; + if (node != null) + ErrorNode = node.OuterXml; + ErrorTerm = term; + } + + internal Exception (XmlNode node, System.Exception e) + : base ("System error", e) + { + ErrorName = Qsystem_error; + ErrorNode = node; + ErrorTerm = Zero; + } + } + public struct Symbol : IEquatable { private static NameTable nt = new NameTable (); @@ -107,40 +163,27 @@ namespace System.Xml private static Symbol Qename = "ename"; - public static partial class Error - { - // Load time errors. - public static Symbol UnknownFunction = "unknown function"; - public static Symbol InvalidArgumentNumber = "invalid argument number"; - public static Symbol VariableNotAcceptable = "variable not acceptable"; - } - - public static partial class Error - { - // Run time errors. - public static Symbol ArithmeticError = "arithmetic error"; - public static Symbol InvalidArgumentType = "invalid argument type"; - public static Symbol NoVariable = "no-variable"; - public static Symbol InvalidVariableType = "invalid variable type"; - public static Symbol NoOuterLoop = "no outer loop"; - public static Symbol UnknownError = "unknown error"; - } - - public abstract class Function { public Symbol name; - public int min_arg, max_arg; - public bool setvar; + // True iff the function can be called with vname="VNAME" attribute. + internal readonly bool with_var; + // Minimum number of positional arguments. + internal readonly int min_args; + // If non-nengative, maximum number of arguments. If negative, + // maxinum number of positional arguments is (-1 - max_args), + // and the remaining unlimit number of arguments are given as a + // single list. + internal readonly int max_args; public Function () { } - public Function (Symbol name, int min_arg, int max_arg, bool setvar) + public Function (Symbol name, bool with_var, int min_args, int max_args) { this.name = name; - this.min_arg = min_arg; - this.max_arg = max_arg; - this.setvar = setvar; + this.with_var = with_var; + this.min_args = min_args; + this.max_args = max_args; } public abstract Term Call (Domain domain, Variable vari, Term[] args); @@ -152,11 +195,12 @@ namespace System.Xml internal class Subroutine : Function { - public Builtin builtin; + public Func builtin; - public Subroutine (Builtin builtin, Symbol name, bool setvar, - int min_arg, int max_arg) - : base (name, min_arg, max_arg, setvar) + public Subroutine (Func builtin, + Symbol name, bool with_var, + int min_args, int max_args) + : base (name, with_var, min_args, max_args) { this.builtin = builtin; } @@ -179,11 +223,12 @@ namespace System.Xml internal class SpecialForm : Function { - public Builtin builtin; + public Func builtin; - public SpecialForm (Builtin builtin, Symbol name, bool setvar, - int min_arg, int max_arg) - : base (name, min_arg, max_arg, setvar) + public SpecialForm (Func builtin, + Symbol name, bool with_var, + int min_args, int max_args) + : base (name, with_var, min_args, max_args) { this.builtin = builtin; } @@ -194,176 +239,77 @@ namespace System.Xml } } - private static void parse_head (Domain domain, XmlNode node, - out Symbol name, - out int min_arg, out int max_arg, - out Variable[] args) - { - int nfixed = 0; - int noptional = 0; - int nrest = 0; - name = node.Attributes[Qfname].Value; - - node = node.FirstChild; - if (node != null && node.Name == Qargs) - { - XmlNode n; - for (n = node.FirstChild; n != null; n = n.NextSibling) - { - if (n.Name == Qfixed) - nfixed += n.ChildNodes.Count; - else if (n.Name == Qoptional) - noptional += n.ChildNodes.Count; - else if (n.Name == Qrest) - nrest++; - else - throw new Exception ("Invalid argument type: " + n); - } - min_arg = nfixed; - max_arg = nfixed + noptional + nrest; - args = new Variable[max_arg]; - n = node.FirstChild; - for (int i = 0; i < max_arg; n = n.NextSibling) - { - for (XmlNode nn = n.FirstChild; nn != null; nn = nn.NextSibling) - args[i++] = domain.Defvar ((Symbol) nn.Attributes[0].Value); - } - if (nrest == 1) - max_arg = - max_arg; - } - else - { - min_arg = max_arg = 0; - args = new Variable[0]; - } - } - - private static void parse_body (Domain domain, XmlNode node, - out Term[] body) - { - for (node = node.FirstChild; node != null; node = node.NextSibling) - if (node.Name != Qdescription - && node.Name != Qargs) - break; - int nterms = 0; - for (XmlNode n = node; n != null; n = n.NextSibling) - nterms++; - if (nterms > 0) - { - body = new Term[nterms]; - for (nterms = 0; node != null; node = node.NextSibling, nterms++) - body[nterms] = new Term (domain, node); - } - else - body = null; - } - - internal class Macro : Function + internal class Lambda : Function { - internal Variable[] args; - internal Term[] body; + private Variable[] args; + private Term[] body; - public Macro (Domain domain, XmlNode node) + public Lambda (Symbol name, int min_args, int max_args, Variable[] args, + Term[] body) + : base (name, false, min_args, max_args) { - parse_head (domain, node, out name, out min_arg, out max_arg, - out args); - setvar = false; + this.args = args; + this.body = body; } - public void SetBody (Domain domain, XmlNode node) + public void SetBody (Term[] body) { - parse_body (domain, node, out body); + this.body = body; } public override Term Call (Domain domain, Variable vari, Term[] args) { Bindings current = domain.bindings; Term result = Zero; - int rest_idx = max_idx < 0 ? - max_idx : args.Length; + int limit = max_args >= 0 ? args.Length : args.Length - 1; - for (int i = 0; i < rest_idx; i++) - domain.Bind (this.args[i], args[i]); - if (i < args.Length) + for (int i = 0; i < limit; i++) { - List rest = new List (); - - for (; i < args.Length; i++) - rest.Add (args[i]); - domain.Bind (this.args[rest_idx], rest); + result = args[i].Eval (domain); + if (domain.Thrown) + return result; + domain.Bind (this.args[i], result); } - - if (body != null) + if (max_args < 0) { - domain.Catch (CatchTag.Return); - foreach (Term term in body) + List list = new List (); + list.InsertRange (0, args[result].Listval); + + for (int i = 0; i < list.Count; i++) { - result = term.Eval (domain); - if (domain.Thrown ()) - break; + result = list[i].Eval (domain); + if (domain.Thrown) + return result; + list[i] = result; } - domain.Uncatch (); + domain.Bind (this.args[limit], new Term (list)); } + domain.Catch (CatchTag.Return); + foreach (Term term in body) + { + result = term.Eval (domain); + if (domain.Thrown ()) + return result; + } + domain.Uncatch (); domain.UnboundTo (current); return result; } } - internal class Lambda : Function + internal class Macro : Function { - internal Variable[] args; - internal Term[] body; - - public Lambda (Domain domain, XmlNode node) - { - parse_head (domain, node, out name, out min_arg, out max_arg, - out args); - setvar = false; - } + private Variable[] args; + private Term[] body; - public Lambda (Domain domain, Symbol name, Symbol[] args) + public Macro (Symbol name, int min_args, int max_args, Variable[] args, + Term[] body) + : base (name, false, min_args, max_args) { - int nfixed = 0; - int noptional = 0; - int nrest = 0; - - this.name = name; - if (args.Length > 0) - { - int i = 0; - for (i = 0; i < args.Length; i++, nfixed++) - if (args[i] == Qoptional || args[i] == Qrest) - break; - if (i < args.Length) - { - if (args[i] == Qoptional) - { - for (i++; i < args.Length; i++, noptional++) - if (args[i] == Qrest) - break; - if (i < args.Length) - nrest = 1; - } - } - min_arg = nfixed; - max_arg = nfixed + noptional + nrest; - this.args = new Variable[max_arg]; - int j; - for (i = j = 0; j < this.args.Length; i++) - if (args[i] != Qoptional || args[i] != Qrest) - this.args[j++] = domain.Defvar (args[i]); - } - else - { - min_arg = max_arg = 0; - } - setvar = false; + this.args = args; + this.body = body; } - public void SetBody (Domain domain, XmlNode node) - { - parse_body (domain, node, out body); - } - public void SetBody (Term[] body) { this.body = body; @@ -374,34 +320,17 @@ namespace System.Xml Bindings current = domain.bindings; Term result = Zero; - try { - int i; - if (args.Length > 0) - { - Term[] newargs = new Term[args.Length]; - for (i = 0; i < min_arg; i++) - newargs[i] = args[i].Eval (domain); - args = newargs; - } - for (i = 0; i < min_arg; i++) - domain.Bind (this.args[i], args[i]); - if (body != null) - { - try { - domain.Catch (CatchTag.Return); - foreach (Term term in body) - { - result = term.Eval (domain); - if (domain.Thrown ()) - return result; - } - } finally { - domain.Uncatch (); - } - } - } finally { - domain.UnboundTo (current); - } + for (int i = 0; i < args.Length; i++) + domain.Bind (this.args[i], args[i]); + domain.Catch (CatchTag.Return); + foreach (Term term in body) + { + result = term.Eval (domain); + if (domain.Thrown ()) + break; + } + domain.Uncatch (); + domain.UnboundTo (current); return result; } } @@ -498,10 +427,13 @@ namespace System.Xml : base (domain, name, new Term (n), desc) { if (range != null && range.Length % 2 == 1) - throw_exception ("Range length for {0} not even", name); + throw new Exception (Qinvalid_range, + "Range length for {0} not even", name); this.range = range; if (! ValueP (val)) - throw_exception ("Invalid integer value for {0}: {1}", name, val); + throw new Exception (Qout_of_range, + "Invalid integer value for {0}: {1}", + name, val); } public override bool ValueP (Term term) @@ -551,7 +483,9 @@ namespace System.Xml { this.range = range; if (! ValueP (val)) - throw_exception ("Invalid string value for {0}: {1}", name, val); + throw new Exception (Qout_of_range, + "Invalid string value for {0}: {1}", + name, val); } public override bool ValueP (Term term) @@ -601,7 +535,9 @@ namespace System.Xml { this.range = range; if (! ValueP (val)) - throw_exception ("Invalid symbol value for {0}: {1}", name, val); + throw new Exception (Qout_of_range, + "Invalid symbol value for {0}: {1}", + name, val); } public override bool ValueP (Term term) @@ -663,11 +599,6 @@ namespace System.Xml } } - private static void throw_exception (string fmt, params object[] args) - { - throw new Exception (String.Format (fmt, args)); - } - internal struct CatchTag { private Term tag; @@ -694,6 +625,8 @@ namespace System.Xml = new Dictionary (); internal Dictionary functions = new Dictionary (); + internal Dictionary macros + = new Dictionary (); internal Dictionary variables = new Dictionary (); internal Bindings bindings; @@ -709,20 +642,29 @@ namespace System.Xml { termtypes = new Dictionary (parent.termtypes); functions = new Dictionary (parent.functions); + macros = new Dictionary (parent.macros); variables = new Dictionary (parent.variables); this.context = context; } - internal void PushTerm (Term term) + public int CallStackCount { get { return call_stack.Count; } } + + public void CallStackPush (Term term) { call_stack.Push (term); } - internal void PopTerm () + public void CallStackPop () { call_stack.Pop (); } + public void CallStackPop (int count) + { + while (count < call_stack.Count) + call_stack.Pop (); + } + internal void Bind (Variable vari, Term value) { bindings = Bindings.Bind (bindings, vari, value); @@ -813,7 +755,8 @@ namespace System.Xml throw new Exception ("No \"name\" field of string type: " + type); Symbol name = (Symbol) finfo.GetValue (null); if (termtypes.ContainsKey (name) - || functions.ContainsKey (name)) + || functions.ContainsKey (name) + || macros.ContainsKey (name)) throw new Exception ("already defined: " + name); Type[] types = new Type[] { typeof (Domain), typeof (XmlNode) }; MethodInfo minfo = type.GetMethod ("Parser", types); @@ -822,68 +765,46 @@ namespace System.Xml termtypes[name] = minfo; } - public void DefSubr (Builtin builtin, string str, bool setvar, - int min_arg, int max_arg, params string[] aliases) + static void check_func_head (Symbol name, bool with_var, + int min_args, int max_args) { - Symbol name = str; if (termtypes.ContainsKey (name)) - throw new Exception ("already defined as termtype: " + name); - Function func = new Function.Subroutine (builtin, name, setvar, - min_arg, max_arg); - functions[name] = func; - foreach (string a in aliases) - functions[(Symbol) a] = func; - } + throw new Exception (Qtype_conflict, "{0} is a Term type", name); + else if (macros.ContainsKey (name)) + throw new Exception (Qtype_conflict, "{0} is a macro", name); - public void DefSpecial (Builtin builtin, string str, bool setvar, - int min_arg, int max_arg, - params string[] aliases) - { - Symbol name = str; - if (termtypes.ContainsKey (name)) - throw new Exception ("already defined as termtype: " + name); - Function func = new Function.SpecialForm (builtin, name, setvar, - min_arg, max_arg); - functions[name] = func; - foreach (string a in aliases) - functions[(Symbol) a] = func; + Function func; + if (functions.TryGetValue (name, out func) + && (func.with_var != with_var + || func.min_args != min_args + || func.max_args != max_args)) + throw new Exception (Qargument_mismatch, ""); } - public void DefAlias (string alias, string str) + public void DefSubr (Func builtin, + Symbol name, bool with_var, + int min_args, int max_args) { - functions[(Symbol) alias] = functions[(Symbol) str]; + check_func_head (name, with_var, min_args, max_args); + functions[name] = new Function.Subroutine (builtin, name, with_var, + min_args, max_args); } - public void Defun (Symbol name, Symbol[] args, Term[] body, - bool prototype) + public void DefSpecial (Func builtin, + Symbol name, bool with_var, + int min_args, int max_args) { - Function func; - - if (termtypes.ContainsKey (name)) - throw new Exception ("already defined as termtype: " + name); - if (prototype || ! functions.TryGetValue (name, out func)) - { - func = new Function.Lambda (this, name, args); - functions[name] = func; - } - if (! prototype) - ((Function.Lambda) func).SetBody (body); + check_func_head (name, with_var, min_args, max_args); + functions[name] = new Function.SpecialForm (builtin, name, with_var, + min_args, max_args); } - public void Defun (XmlNode node, bool prototype) + public void Defun (Symbol name, int min_arg, int max_arg, + Variable[] args, Term[] body) { - Symbol name = node.Attributes[Qfname].Value; - if (termtypes.ContainsKey (name)) - throw new Exception ("already defined as termtype: " + name); - Function func; - - if (prototype || ! functions.TryGetValue (name, out func)) - { - func = new Function.Lambda (this, node); - functions[name] = func; - } - if (! prototype) - ((Function.Lambda) func).SetBody (this, node); + check_func_head (name, false, min_args, max_args); + functions[name] = new Function.Lambda (name, min_args, max_args, + args, body); } public void Defun (Function func) @@ -891,20 +812,33 @@ namespace System.Xml functions[func.name] = func; } - public void Defmacro (XmlNode node, bool prototype) + public void Defmacro (Symbol name, int min_arg, int max_arg, + Variable[] args, Term[] body) { - Symbol name = node.Attributes[Qfname].Value; if (termtypes.ContainsKey (name)) - throw new Exception ("already defined as termtype: " + name); - Function func; + throw new Exception (Qtype_conflict, "{0} is a Term type", name); + else if (function.ContainsKey (name)) + throw new Exception (Qtype_conflict, "{0} is a function", name); - if (prototype || ! functions.TryGetValue (name, out func)) - { - func = new Function.Macro (this, node); - functions[name] = func; - } - if (! prototype) - ((Function.Macro) func).SetBody (this, node); + Function.Macro macro; + if (macros.TryGetValue (name, out macro) + && (macro.min_args != min_args + || macro.max_args != max_args)) + throw new Exception (Qargument_mismatch, ""); + + macros[name] = new Function.Macro (name, min_arg, max_arg, args, body); + } + + public void DefAlias (Symbol alias, Symbol fname) + { + Function func; + if (functions.TryGetValue (alias, out func)) + throw new Exception (Qalready_defined, + "Alread defined: {0}", alias); + if (! functions.TryGetValue (fname, out func)) + throw new Exception (Qunknown_function, + "Unknown fucntion: {0}", fname); + functions[alias] = func; } public Variable Defvar (XmlNode node) @@ -926,18 +860,17 @@ namespace System.Xml Symbol type = node.Name; XmlNodeList range_list = null; int nranges = 0; - string val = node.InnerText; + XmlNode n = node.NextSibling; - node = node.NextSibling; - if (node != null) + if (n != null) { - range_list = node.ChildNodes; + range_list = n.ChildNodes; nranges = range_list.Count; } if (type == Qinteger) { - int n = parse_integer (val); + int val = parse_integer (node); int[] range = null; if (range_list != null) { @@ -947,20 +880,17 @@ namespace System.Xml XmlNode nd = range_list[i]; if (nd.Name == Qrange) { - range[i * 2] - = parse_integer (nd.FirstChild.InnerText); - range[i * 2 + 1] - = parse_integer (nd.LastChild.InnerText); + range[i * 2] = parse_integer (nd.FirstChild); + range[i * 2 + 1] = parse_integer (nd.LastChild); } else { - range[i * 2] - = range[i * 2 + 1] - = parse_integer (nd.FirstChild.InnerText); + range[i * 2] = range[i * 2 + 1] + = parse_integer (nd.FirstChild); } } } - vari = DefvarInt (name, n, desc, range); + vari = DefvarInt (name, val, desc, range); } else if (type == Qstring) { @@ -971,7 +901,7 @@ namespace System.Xml for (int i = 0; i < nranges; i++) range[i] = range_list[i].FirstChild.InnerText; } - vari = DefvarStr (name, val, desc, range); + vari = DefvarStr (name, node.InnerText, desc, range); } else if (type == Qsymbol) { @@ -982,10 +912,10 @@ namespace System.Xml for (int i = 0; i < nranges; i++) range[i] = range_list[i].FirstChild.InnerText; } - vari = DefvarSym (name, (Symbol) val, desc, range); + vari = DefvarSym (name, (Symbol) node.InnerText, desc, range); } else - throw new Exception ("Unknown type: " + type); + Exception.Throw (Qparse_error, node, "Unknown type: {0}", type); } else { @@ -1097,7 +1027,7 @@ namespace System.Xml Function func; if (! functions.TryGetValue (name, out func)) - throw new Exception ("Unknown function: " + name); + throw new Exception (Qunknown_function, (strign) name); return func; } @@ -1172,8 +1102,6 @@ namespace System.Xml } } - public delegate Term Builtin (Domain domain, Variable vari, Term[] args); - private static Domain basic = new Domain ("basic"); static Xexpression () @@ -1182,23 +1110,41 @@ namespace System.Xml basic.DefType (typeof (Funcall)); basic.DefType (typeof (ErrorTerm)); - basic.DefSubr (Fset, "set", true, 1, 1, "="); - basic.DefSubr (Fnot, "not", false, 1, 1, "!"); - basic.DefSubr (Fadd, "add", true, 1, -1, "+"); - basic.DefSubr (Fmul, "mul", true, 1, -1, "*"); - basic.DefSubr (Fsub, "sub", true, 1, -1, "-"); - basic.DefSubr (Fdiv, "div", true, 1, -1, "/"); - basic.DefSubr (Fmod, "mod", true, 1, 2, "%"); - basic.DefSubr (Flogior, "logior", true, 1, -1, "|"); - basic.DefSubr (Flogand, "logand", true, 1, -1, "&"); - basic.DefSubr (Flsh, "lsh", true, 1, 2, "<<"); - basic.DefSubr (Frsh, "rsh", true, 1, 2, ">>"); - basic.DefSubr (Feq, "eq", false, 2, -1, "=="); - basic.DefSubr (Fnoteq, "noteq", false, 2, 2, "!="); - basic.DefSubr (Flt, "lt", false, 2, -1, "<"); - basic.DefSubr (Fle, "le", false, 2, -1, "<="); - basic.DefSubr (Fgt, "gt", false, 2, -1, ">"); - basic.DefSubr (Fge, "ge", false, 2, -1, ">="); + basic.DefSubr (Fset, "set", true, 1, 1); + basic.DefAlias ("=", "set"); + basic.DefSubr (Fnot, "not", false, 1, 1); + basic.DefAlias ("!", "not"); + basic.DefSubr (Fadd, "add", true, 1, -1); + basic.DefAlias ("+", "add"); + basic.DefSubr (Fmul, "mul", true, 1, -1); + basic.DefAlias ("*", "mul"); + basic.DefSubr (Fsub, "sub", true, 1, -1); + basic.DefAlias ("-", "sub"); + basic.DefSubr (Fdiv, "div", true, 1, -1); + basic.DefAlias ("/", "div"); + basic.DefSubr (Fmod, "mod", true, 1, 2); + basic.DefAlias ("%", "mod"); + basic.DefSubr (Flogior, "logior", true, 1, -1); + basic.DefAlias ("|", "logior"); + basic.DefSubr (Flogand, "logand", true, 1, -1); + basic.DefAlias ("&", "logand"); + basic.DefSubr (Flsh, "lsh", true, 1, 2); + basic.DefAlias ("<<", "lsh"); + basic.DefSubr (Frsh, "rsh", true, 1, 2); + basic.DefAlias (">>", "rsh"); + basic.DefSubr (Feq, "eq", false, 2, -1); + basic.DefAlias ("==", "eq"); + basic.DefSubr (Fnoteq, "noteq", false, 2, 2); + basic.DefAlias ("!=", "noteq"); + basic.DefSubr (Flt, "lt", false, 2, -1); + basic.DefAlias ("<", "lt"); + basic.DefSubr (Fle, "le", false, 2, -1); + basic.DefAlias ("<=", "le"); + basic.DefSubr (Fgt, "gt", false, 2, -1); + basic.DefAlias (">", "gt"); + basic.DefSubr (Fge, "ge", false, 2, -1); + basic.DefAlias (">=", "ge"); + basic.DefSubr (Fappend, "append", true, 0, -1); basic.DefSubr (Fconcat, "concat", true, 0, -1); basic.DefSubr (Fnth, "nth", false, 2, 2); @@ -1210,9 +1156,12 @@ namespace System.Xml basic.DefSubr (Freturn, "return", false, 0, 1); basic.DefSubr (Fthrow, "throw", false, 1, 2); - basic.DefSpecial (Fand, "and", false, 1, -1, "&&"); - basic.DefSpecial (For, "or", false, 1, -1, "||"); - basic.DefSpecial (Fprogn, "progn", false, 0, -1, "expr"); + basic.DefSpecial (Fand, "and", false, 1, -1); + basic.DefAlias ("&&", "and"); + basic.DefSpecial (For, "or", false, 1, -1); + basic.DefAlias ("||", "or"); + basic.DefSpecial (Fprogn, "progn", false, 0, -1); + basic.DefAlias ("expr", "progn"); basic.DefSpecial (Fif, "if", false, 2, 3); basic.DefSpecial (Fwhen, "when", false, 1, -1); basic.DefSpecial (Floop, "loop", false, 1, -1); @@ -1485,7 +1434,8 @@ namespace System.Xml result = ((List) args[1].objval)[args[0].Intval]; } else - throw new Exception ("Term is not enumelable: " + args[1]); + throw new Exception (Qwrong_type, + "{0} is not a string nor a list", args[1]); return result; } @@ -1500,8 +1450,9 @@ namespace System.Xml private static Term Fins (Domain domain, Variable vari, Term[] args) { + if (vari == null) + throw new Exception (Qwrong_calling_format, "No vname attribute"); Term term = vari.Value; - if (term.IsStr) { string str = term.Strval.Insert (args[0].Intval, args[1].Strval); @@ -1510,12 +1461,15 @@ namespace System.Xml else if (vari.Value.IsList) vari.Value.Listval.InsertRange (args[0].Intval, args[1].Listval); else - throw new Exception ("term is not collection: " + vari.Value); + throw new Exception (Qwrong_type, "{0} is not a string nor list", + vari.Value); return vari.Value; } private static Term Fdel (Domain domain, Variable vari, Term[] args) { + if (vari == null) + throw new Exception (Qwrong_calling_format, "No vname attribute"); if (vari.Value.IsStr) { string str @@ -1527,7 +1481,8 @@ namespace System.Xml vari.Value.Listval.RemoveRange (args[0].Intval, args[1].Intval - args[0].Intval); else - throw new Exception ("term is not collection: " + vari.Value); + throw new Exception (Qwrong_type, "{0} is not a string nor a list", + vari.Value); return vari.Value; } @@ -1700,28 +1655,51 @@ namespace System.Xml return TermTerm; } - public static Term Fcatch (Domain domain, Variable vari, Term[] args) + private static Term eval_terms (Domain domain, Term[] terms, int idx, + out bool caught) { - Term result = Zero; - bool caught; + Term result= Zero; - domain.Catch (new CatchTag (args[0].Symval)); - for (int i = 1; i < args.Length; i++) + caught = false; + for (int i = idx; i < args.Length; i++) { result = args[i].Eval (domain); if (domain.Thrown (out caught)) - { - domain.Uncatch (); - if (caught) - { - if (vari != null) - vari.Value = result; - return One; - } - return result; - } + break; + } + return result; + } + + public static Term Fcatch (Domain domain, Variable vari, Term[] args) + { + Term result; + bool caught = false; + + if (args.Length < 2) + throw new Exception (Error.WrongCallingFormat, + "too fee arguments: {0}", args.Length); + if (args[0].IsError) + { + int count = domain.CallStackCount; + + try { + result = evaluate_terms (domain, args, 1, out caught); + } catch (Exception e) { + if (! args[0].Matches (e.ErrorTerm)) + throw e; + ((ErrorTerm) e.ErrorTerm.objval). + if (vari != null) + vari.Value = e.ErrorTerm; + domain.CallStackPop (count); + return One; + } + } + else if (args[0].IsSymbol) + { + domain.Catch (new CatchTag (args[0].Symval)); + result = evaluate_terms (domain, args, 1, out caught); + domain.Uncatch (); } - domain.Uncatch (); if (vari != null) vari.Value = result; return Zero; @@ -1742,7 +1720,7 @@ namespace System.Xml public abstract class TermValue { - public virtual Term Eval (Domain domain) { return new Term (this); } + public virtual Term Eval (Domain domain) { return this; } public virtual TermValue Clone () { return this; } public virtual bool IsTrue { get { return true; } } public virtual bool Matches (TermValue other) { return Equals (other); } @@ -1805,12 +1783,15 @@ namespace System.Xml { int nargs = args.Length; - if (nargs < func.min_arg - || (func.max_arg >= 0 && nargs > func.max_arg)) - throw_exception ("Invalid number of arguments to {0}: ", - func.name, nargs); - if (! func.setvar && vari != null) - throw_exception ("vname attribute not acceptable: ", func.name); + if (nargs < func.min_args + || (func.max_args >= 0 && nargs > func.max_args)) + throw new Exception (Qwrong_calling_format, + "Wrong number of arguments to {0}: {1}", + func.name, nargs); + if (! func.with_var && vari != null) + throw new Exception (Qwrong_calling_format, + "Wrong vname attribute to {0}: {1}", + func.name, vari.name); this.func = func; this.vari = vari; this.args = args; @@ -1983,7 +1964,7 @@ namespace System.Xml if (name == Qinteger) { - intval = parse_integer (node.InnerText); + intval = parse_integer (node); objval = null; } else @@ -2055,7 +2036,8 @@ namespace System.Xml public object Objval { get { if (objval == null) - throw new Exception ("term is an integer: " + this); + throw new Exception (Qwrong_type, + "term is an integer: {0}", this); return objval; } } @@ -2063,7 +2045,7 @@ namespace System.Xml public int Intval { get { if (objval != null) - throw new Exception ("term is not integer: " + this); + throw new Exception (Qwrong_type, "{0} is not an integer", this); return intval; } } @@ -2071,7 +2053,7 @@ namespace System.Xml public string Strval { get { if (! IsStr) - throw new Exception ("term is not a string: " + this); + throw new Exception (Qwrong_type, "{0} is not a string", this); return (string) objval; } } @@ -2079,7 +2061,7 @@ namespace System.Xml public string Symval { get { if (! IsSymbol) - throw new Exception ("term is not a symbol: " + this); + throw new Exception (Qwrong_type, "{0} is not a symbol", this); return (Symbol) objval; } } @@ -2087,7 +2069,7 @@ namespace System.Xml public List Listval { get { if (! IsList) - throw new Exception ("term is not a list: " + this); + throw new Exception (Qwrong_type, "{0} is not a list"); return (List) objval; } } @@ -2110,17 +2092,15 @@ namespace System.Xml public bool IsList { get { return (objval is List); } } public bool IsError { get { return (objval is ErrorTerm); } } - public Term Eval (Domain domain) + private Term Eval (Domain domain) { - try { - domain.PushTerm (this); - if (objval == null || objval is Symbol || objval is string - || objval is List) - return this; - return ((TermValue) objval).Eval (domain); - } finally { - domain.PopTerm (); - } + if (objval == null || objval is Symbol || objval is string + || objval is List) + return this; + domain.CallStackPush (this); + Term result = ((TermValue) objval).Eval (domain); + domain.CallStackPop (); + return result; } public Term Clone () @@ -2182,8 +2162,19 @@ namespace System.Xml if (IsSymbol) return term.IsSymbol && term.Symval == Symval; if (IsList) - return (((List) objval).Count > 0 - && ((List) objval)[0].Matches (term)); + { + if (! term.IsList) + return false; + List l1 = objval as List; + List l2 = term.objval as List; + + if (l1.Count > l2.Count) + return false; + for (int i = 0; i < l1.Count; i++) + if (! l1[i].Equals (l2[i])) + return false; + return true; + } return ((TermValue) objval).Matches ((TermValue) term.objval); } @@ -2213,122 +2204,214 @@ namespace System.Xml } else if (objval is Term) str = "" + objval + ""; - else if (objval is TermValue) - str = ((TermValue) objval).ToString (detail); else - throw new Exception ("invalid Term object: " + objval); + str = ((TermValue) objval).ToString (detail); return str; } } - static private Term Zero = new Term (0); - static private Term One = new Term (1); - static private Term TermInt = new Term (Qinteger); - static private Term TermStr = new Term (Qstring); - static private Term TermSymbol = new Term (Qsymbol); - static private Term TermList = new Term (Qlist); - static private Term TermTerm = new Term ((Symbol) "term"); + private static Term Zero = new Term (0); + private static Term One = new Term (1); + private static Term TermInt = new Term (Qinteger); + private static Term TermStr = new Term (Qstring); + private static Term TermSymbol = new Term (Qsymbol); + private static Term TermList = new Term (Qlist); + private static Term TermTerm = new Term ((Symbol) "term"); - internal static int parse_integer (string str) + private static int parse_integer (XmlNode node) { + str = node.InnerText; int len = str.Length; bool negative = false; + int i, c; - if (len <= 1) - return (len == 0 ? 0 : str[0] - '0'); + if (len == 1 && str[0] == '?') + return str[1]; + if (len > 2 + && str[1] == 'x' && ((c = str[0]) == '0' || c == '#')) + { + if (! int.TryParse (str.SubString (2), NumberStyles.HexNumber, null, + out i)) + Exception.Throw (Qparse_error, node, "Invalid integer"); + return i; + } + if (! int.TryParse (str, out i)) + Exception.Throw (Qparse_error, node, "Invalid integer"); + return i; + } - int c = str[0]; - int i; + static bool parse_error (XmlNode node, out Term term) + { + try { + term = new Term (node.OuterXml); + } catch { + term = memory_error; + } + return false; + } - if (c == '?') - return str[1]; - if ((c == '0' || c == '#') && str[1] == 'x') + static bool parse_error (XmlNode node, out Term[] terms) + { + try { + terms = new Term[] { new Term (node.OuterXml) }; + } catch { + terms = new Term[] { memory_error }; + } + return false; + } + + private static Variable[] parse_args (Domain domain, XmlNode node, + out int min_args, out int max_args) + { + int nfixed = 0; + int noptional = 0; + int nrest = 0; + Variable[] args; + + node = node.FirstChild; + if (node != null && node.Name == Qargs) { - i = 0; - for (int idx = 2; idx < len; idx++) + XmlNode n; + for (n = node.FirstChild; n != null; n = n.NextSibling) { - c = str[idx]; - if (c < '0') - break; - else if (c <= '9') - i = i * 16 + (c - '0'); - else if (c < 'A') - break; - else if (c <= 'F') - i = i * 16 + (c - 'A'); - else if (c < 'a') - break; - else if (c <= 'f') - i = i * 16 + (c - 'a'); + if (n.Name == Qfixed) + nfixed += n.ChildNodes.Count; + else if (n.Name == Qoptional) + noptional += n.ChildNodes.Count; + else if (n.Name == Qrest) + nrest++; else - break; + throw new Exception (Qinvalid_argument_type, n); } - return i; + min_args = nfixed; + max_args = nfixed + noptional + nrest; + args = new Variable[max_args]; + n = node.FirstChild; + for (int i = 0; i < max_args; n = n.NextSibling) + { + for (XmlNode nn = n.FirstChild; nn != null; nn = nn.NextSibling) + args[i++] = domain.Defvar ((Symbol) nn.Attributes[0].Value); + } + if (nrest == 1) + max_args = - max_args; } - if (c == '-') - negative = true; - i = c - '0'; - for (int idx = 1; idx < len; idx++) + else { - c = str[idx]; - if (c < '0' || c > '9') - break; - i = i * 10 + (c - '0'); + min_args = max_args = 0; + args = new Variable[0]; } - return negative ? - i : i; + return args; } - private Term[] terms; + public static Symbol parse_defun_head (Domain domain, XmlNode node) + { + Symbol name = node.Attributes[Qfname].Value; + int min_args, max_args; + Variable[] args; - public static Term[] ParseTerms (Domain domain, XmlNode node) + try { + args = parse_head (domain, node, out min_args, out max_args); + if (node.Name == Qdefun) + domain.Defun (name, min_args, max_args, args, null); + else + domain.Defmacro (name, min_args, max_args, args, null); + } catch (Exception e) { + if (e.ErrorNode == null) + e.ErrorNode = node; + throw e; + } catch (System.Exception e) { + throw new Exception (node, e); + } + return name; + } + + public static void parse_defun_body (Domain domain, XmlNode node) { - int nterms = 0; - for (XmlNode n = node; n != null; n = n.NextSibling) - if (n.NodeType == XmlNodeType.Element) - { - if (n.Name == Qdefun) - domain.Defun (n, true); - else if (n.Name == Qdefmacro) - domain.Defmacro (n, true); - else if (n.Name == Qdefvar) - domain.Defvar (n); - else - nterms++; - } - Term[] terms = new Term[nterms]; - int i = 0; - for (XmlNode n = node; n != null; n = n.NextSibling) - if (n.NodeType == XmlNodeType.Element) - { - if (n.Name == Qdefun) - domain.Defun (n, false); - else if (n.Name == Qdefmacro) - domain.Defmacro (n, false); - else if (n.Name != Qdefvar) - terms[i++]= new Term (domain, n); - } - return terms; + Symbol name = node.Attributes[Qfname].Value; + Function func = domain.GetFunc (name); + + for (node = node.FirstChild; node != null; node = node.NextSibling) + if (node.Name != Qdescription + && node.Name != Qargs) + break; + Term[] body = Parse (domain, node, null); + if (node.Name == Qdefun) + ((Function.Lambda) func).SetBody (body); + else + ((Function.Macro) func).SetBody (body); } - public static Term Eval (Domain domain, Term[] terms) + public static Term Parse (Domain domain, XmlNode node) { - Term result = new Term (0); - foreach (Term term in terms) + Symbol name = node.Name; + + if (name == Qinteger) + return new Term (parse_integer (node)); + if (name == Qsymbol) + return new Term ((Symbol) node.InnerText); + if (name == Qstring) + return new Term (node.InnerText); + if (name == Qlist) { - result = term; - if (result.Objval is Funcall) - while ((result = result.Eval (domain)).Objval is Funcall); + List list = new List (); + for (node = node.FirstChild; node != null; + node = node.NextSibling) + list.Add (Parse (domain, node)); + return new Term (list); } - return result; + if (name == Qdefun || name == Qdefmacro) + { + name = parse_defun_head (domain, node); + parse_defun_body (domain, node); + return new Term (name); + } + if (name == Qdefvar) + { + name = parse_defvar (domain, node); + return new Term (name); + } + MethodInfo minfo; + if (domain.termtypes.TryGetValue (name, out minfo)) + { + object[] arg = new object[] { domain, node }; + return new Term ((TermValue) minfo.Invoke (null, arg)); + } + + Funcall funcall = (Funcall) Funcall.Parser (domain, node); + if (funcall.func is Function.Macro) + return funcall.Eval (domain); + return new Term (funcall); } + private static bool default_stop (XmlNode n) { return n == null; } - public Xexpression (Domain domain, XmlNode node) + public static List Parse (Domain domain, XmlNode node, + Func stop) { - terms = ParseTerms (domain, node); + if (stop == null) + stop = default_stop; + + XmlNode n; + for (n = node; ! stop (n); n = n.NextSibling) + if (n.NodeType == XmlNodeType.Element + && (n.Name == Qdefun || n.Name == Qdefmacro)) + parse_defun_head (domain, n); + + List terms = new List (); + for (; node != n; node = node.NextSibling) + if (node.NodeType == XmlNodeType.Element) + { + if (node.Name == Qdefun || node.Name == Qdefmacro) + parse_defun_body (domain, node); + else if (node.Name == Qdefvar) + parse_defvar (domain, node); + else + terms.Add (Parse (domain, node)); + } + return terms; } - public Xexpression (Domain domain, string url) + public static List Parse (Domain domain, string url) { XmlDocument doc = new XmlDocument (Symbol.NameTable); XmlNode node; @@ -2344,34 +2427,20 @@ namespace System.Xml throw new Exception ("Node not found"); node = doc.ReadNode (reader); } - terms = ParseTerms (domain, node.FirstChild); + return Parse (domain, node.FirstChild, null); } - public Term Eval (Domain domain) + public static Term Eval (Domain domain, List terms) { Term result = Zero; - domain.depth = 0; - try { - domain.Catch (CatchTag.Return); - foreach (Term term in terms) - { - result = term.Eval (domain); - if (domain.Thrown ()) - return result; - } - } finally { - domain.Uncatch (); - } + foreach (Term term in terms) + { + result = term.Eval (domain); + if (domain.Thrown) + return result; + } return result; } - - public override string ToString () - { - string str = ""; - for (int i = 0; i < terms.Length; i++) - str += terms[i]; - return str; - } } } diff --git a/xex.xml b/xex.xml index 90d0430..1ad4c16 100644 --- a/xex.xml +++ b/xex.xml @@ -16,7 +16,6 @@ - XXX @@ -70,5 +69,4 @@ 100 5 - -- 1.7.10.4