From: handa Date: Sat, 19 Sep 2009 11:17:33 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=62a15d3e7eca5370344e8d55c38c1e8943b371f7;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/XmlExpr.cs b/XmlExpr.cs index 3c1594b..806ccc0 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -103,7 +103,6 @@ namespace System.Xml.Expression private static Name Ndescription = "description"; private static Name Nrange = "range"; - private static Name Nprogn = "progn"; private static Name Nbreak = "break"; private static Name Nreturn = "return"; @@ -147,7 +146,7 @@ namespace System.Xml.Expression for (int i = 0; i < args.Length; i++) { newargs[i] = args[i].Eval (domain); - if (domain.Catched) + if (domain.Thrown) return newargs[i]; } return builtin (domain, vari, newargs); @@ -253,15 +252,17 @@ namespace System.Xml.Expression if (body != null) { try { - domain.Catch (Nreturn); + if (args_evalled) + domain.Catch (Nreturn); foreach (Term term in body) { result = term.Eval (domain); - if (domain.Catched) + if (domain.Thrown) return result; } } finally { - domain.Uncatch (); + if (args_evalled) + domain.Uncatch (); } } } finally { @@ -272,7 +273,7 @@ namespace System.Xml.Expression } } - public abstract class Variable + public class Variable : Object { public readonly Name name; internal Term val; @@ -280,10 +281,10 @@ namespace System.Xml.Expression public Variable (Name name, Term term) { this.name = name; - Value = term; + val = term; } - public Term Value + public virtual Term Value { get { return val; } set @@ -294,7 +295,9 @@ namespace System.Xml.Expression } } - public abstract bool ValueP (Term term); + public override Term Eval (Domain domain) { return val; } + + public virtual bool ValueP (Term term) { return true; } public override string ToString () { return name + "(" + val + ")"; } @@ -344,13 +347,6 @@ namespace System.Xml.Expression return false; } } - - internal class Misc : Variable - { - public Misc (Name name, Term term) : base (name, term) { } - - public override bool ValueP (Term term) { return true; } - } } internal class Bindings @@ -413,7 +409,7 @@ namespace System.Xml.Expression internal Dictionary variables; internal Bindings bindings; private Stack catch_stack = new Stack (); - private int catch_count = -1; + private int catch_count = 0; internal Domain () { @@ -447,41 +443,34 @@ namespace System.Xml.Expression { catch_stack.Push (name); catch_count++; - Console.WriteLine ("catch {0}", catch_count); } - internal bool Catched { - get { - Console.WriteLine ("catched {0} {1}", - catch_stack.Count, - catch_count); - return catch_stack.Count == catch_count; } + internal void Uncatch () + { + catch_stack.Pop (); + if (catch_count > catch_stack.Count) + catch_count--; + } + + internal bool Thrown { + get { return catch_count < catch_stack.Count; } } internal void ThrowReturn () { - Name tag = catch_stack.Peek (); - while (tag != Nreturn) + foreach (Name tag in catch_stack) { - catch_stack.Pop (); - tag = catch_stack.Peek (); + catch_count--; + if (tag == Nreturn) + break; } - catch_count = catch_stack.Count - 1; } internal void ThrowBreak () { - Name tag = catch_stack.Peek (); - if (tag != Nbreak) + if (catch_stack.Peek () != Nbreak) throw new Exception ("No outer loop to break"); - catch_count = catch_stack.Count - 1; - } - - internal void Uncatch () - { - catch_stack.Pop (); - catch_count = catch_stack.Count; - Console.WriteLine ("uncatch {0}", catch_count); + catch_count--; } public void DefSubr (Builtin builtin, string str, bool setvar, @@ -593,13 +582,13 @@ namespace System.Xml.Expression throw new Exception ("Unknown type: " + type); } else - vari = new Variable.Misc (name, Zero); + vari = new Variable (name, Zero); variables[name] = vari; } internal Variable Defvar (Name name) { - Variable vari = new Variable.Misc (name, Zero); + Variable vari = new Variable (name, Zero); variables[name] = vari; return vari; } @@ -632,7 +621,7 @@ namespace System.Xml.Expression Variable vari; if (! variables.TryGetValue (name, out vari)) - variables[name] = vari = new Variable.Misc (name, Zero); + variables[name] = vari = new Variable (name, Zero); return vari; } @@ -672,63 +661,59 @@ namespace System.Xml.Expression private static Domain basic = new Domain (); - internal static Function Fprogn; - static Xexpression () { - basic.DefSubr (set_value, "set", true, 1, 1, "="); - basic.DefSubr (not, "not", false, 1, 1, "!"); - basic.DefSubr (add, "add", true, 1, -1, "+"); - basic.DefSubr (mul, "mul", true, 1, -1, "*"); - basic.DefSubr (sub, "sub", true, 1, -1, "-"); - basic.DefSubr (div, "div", true, 1, -1, "/"); - basic.DefSubr (mod, "mod", true, 1, 2, "%"); - basic.DefSubr (logior, "logior", true, 1, -1, "|"); - basic.DefSubr (logand, "logand", true, 1, -1, "&"); - basic.DefSubr (lsh, "lsh", true, 1, 2, "<<"); - basic.DefSubr (rsh, "rsh", true, 1, 2, ">>"); - basic.DefSubr (eq, "eq", false, 2, -1, "=="); - basic.DefSubr (noteq, "noteq", false, 2, 2, "!="); - basic.DefSubr (less_than, "lt", false, 2, -1, "<"); - basic.DefSubr (less_eq, "le", false, 2, -1, "<="); - basic.DefSubr (greater_than, "gt", false, 2, -1, ">"); - basic.DefSubr (greater_eq, "ge", false, 2, -1, ">="); - basic.DefSubr (append, "append", true, 0, -1); - basic.DefSubr (concat, "concat", true, 0, -1); - basic.DefSubr (nth, "nth", false, 2, 2); - basic.DefSubr (copy, "copy", false, 1, 1); - basic.DefSubr (ins, "ins", true, 2, 2); - basic.DefSubr (del, "del", true, 2, 2); - basic.DefSubr (eval_clause, "eval", false, 1, 1); - basic.DefSubr (break_clause, "break", false, 0, 1); - basic.DefSubr (return_clause, "return", false, 1, 1); - basic.DefSpecial (and, "and", 1, -1, "&&"); - basic.DefSpecial (or, "or", 1, -1, "||"); - basic.DefSpecial (progn_clause, "progn", 0, -1, "expr"); - basic.DefSpecial (if_clause, "if", 2, 3); - basic.DefSpecial (when_clause, "when", 1, -1); - basic.DefSpecial (loop_clause, "loop", 1, -1); - basic.DefSpecial (while_clause, "while", 1, -1); - basic.DefSpecial (cond_clause, "cond", 1, -1); - basic.DefSpecial (foreach_clause, "foreach", 2, -1); - basic.DefSpecial (quote_clause, "quote", 1, 1); - basic.DefSpecial (type_clause, "type", 1, 1); - - Fprogn = basic.GetFunc (Nprogn); + 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 (Fappend, "append", true, 0, -1); + basic.DefSubr (Fconcat, "concat", true, 0, -1); + basic.DefSubr (Fnth, "nth", false, 2, 2); + basic.DefSubr (Fcopy, "copy", false, 1, 1); + basic.DefSubr (Fins, "ins", true, 2, 2); + basic.DefSubr (Fdel, "del", true, 2, 2); + basic.DefSubr (Feval, "eval", false, 1, 1); + basic.DefSubr (Fbreak, "break", false, 0, 1); + basic.DefSubr (Freturn, "return", false, 0, 1); + basic.DefSpecial (Fand, "and", 1, -1, "&&"); + basic.DefSpecial (For, "or", 1, -1, "||"); + basic.DefSpecial (Fprogn, "progn", 0, -1, "expr"); + basic.DefSpecial (Fif, "if", 2, 3); + basic.DefSpecial (Fwhen, "when", 1, -1); + basic.DefSpecial (Floop, "loop", 1, -1); + basic.DefSpecial (Fwhile, "while", 1, -1); + basic.DefSpecial (Fcond, "cond", 1, -1); + basic.DefSpecial (Fforeach, "foreach", 2, -1); + basic.DefSpecial (Fquote, "quote", 1, 1); + basic.DefSpecial (Ftype, "type", 1, 1); } - private static Term set_value (Domain domain, Variable vari, Term[] args) + private static Term Fset (Domain domain, Variable vari, Term[] args) { vari.Value = args[0]; return vari.val; } - private static Term not (Domain domain, Variable vari, Term[] args) + private static Term Fnot (Domain domain, Variable vari, Term[] args) { return args[0].IsTrue ? Zero : One; } - private static Term add (Domain domain, Variable vari, Term[] args) + private static Term Fadd (Domain domain, Variable vari, Term[] args) { int n = vari == null ? 0 : vari.val.Intval; @@ -740,7 +725,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term mul (Domain domain, Variable vari, Term[] args) + private static Term Fmul (Domain domain, Variable vari, Term[] args) { int n = vari == null ? 1 : vari.val.Intval; foreach (Term arg in args) @@ -751,7 +736,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term sub (Domain domain, Variable vari, Term[] args) + private static Term Fsub (Domain domain, Variable vari, Term[] args) { int n, i; @@ -773,7 +758,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term div (Domain domain, Variable vari, Term[] args) + private static Term Fdiv (Domain domain, Variable vari, Term[] args) { int n, i; @@ -795,7 +780,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term mod (Domain domain, Variable vari, Term[] args) + private static Term Fmod (Domain domain, Variable vari, Term[] args) { if (vari == null) return (args[0].Intval % args[1].Intval); @@ -803,7 +788,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term logior (Domain domain, Variable vari, Term[] args) + private static Term Flogior (Domain domain, Variable vari, Term[] args) { int n = vari == null ? 0 : vari.val.Intval; foreach (Term arg in args) @@ -814,7 +799,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term logand (Domain domain, Variable vari, Term[] args) + private static Term Flogand (Domain domain, Variable vari, Term[] args) { int n, i; @@ -836,7 +821,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term lsh (Domain domain, Variable vari, Term[] args) + private static Term Flsh (Domain domain, Variable vari, Term[] args) { if (vari == null) return args[0].Intval << args[1].Intval; @@ -844,7 +829,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term rsh (Domain domain, Variable vari, Term[] args) + private static Term Frsh (Domain domain, Variable vari, Term[] args) { if (vari == null) return args[0].Intval >> args[1].Intval; @@ -852,7 +837,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term eq (Domain domain, Variable vari, Term[] args) + private static Term Feq (Domain domain, Variable vari, Term[] args) { Term o = args[0]; @@ -871,12 +856,12 @@ namespace System.Xml.Expression return One; } - private static Term noteq (Domain domain, Variable vari, Term[] args) + private static Term Fnoteq (Domain domain, Variable vari, Term[] args) { - return eq (domain, vari, args); + return Feq (domain, vari, args); } - private static Term less_than (Domain domain, Variable vari, Term[] args) + private static Term Flt (Domain domain, Variable vari, Term[] args) { int n = (int) args[0]; @@ -890,7 +875,7 @@ namespace System.Xml.Expression return One; } - private static Term less_eq (Domain domain, Variable vari, Term[] args) + private static Term Fle (Domain domain, Variable vari, Term[] args) { int n = (int) args[0]; for (int i = 1; i < args.Length; i++) @@ -903,7 +888,7 @@ namespace System.Xml.Expression return One; } - private static Term greater_than (Domain domain, Variable vari, Term[] args) + private static Term Fgt (Domain domain, Variable vari, Term[] args) { int n = (int) args[0]; for (int i = 1; i < args.Length; i++) @@ -916,7 +901,7 @@ namespace System.Xml.Expression return One; } - private static Term greater_eq (Domain domain, Variable vari, Term[] args) + private static Term Fge (Domain domain, Variable vari, Term[] args) { int n = (int) args[0]; for (int i = 1; i < args.Length; i++) @@ -929,7 +914,7 @@ namespace System.Xml.Expression return One; } - private static Term append (Domain domain, Variable vari, Term[] args) + private static Term Fappend (Domain domain, Variable vari, Term[] args) { List list; @@ -956,7 +941,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term concat (Domain domain, Variable vari, Term[] args) + private static Term Fconcat (Domain domain, Variable vari, Term[] args) { string str; @@ -986,7 +971,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term nth (Domain domain, Variable vari, Term[] args) + private static Term Fnth (Domain domain, Variable vari, Term[] args) { Term result; @@ -1004,7 +989,7 @@ namespace System.Xml.Expression return result; } - private static Term copy (Domain domain, Variable vari, Term[] args) + private static Term Fcopy (Domain domain, Variable vari, Term[] args) { Term result; @@ -1013,7 +998,7 @@ namespace System.Xml.Expression return result; } - private static Term ins (Domain domain, Variable vari, Term[] args) + private static Term Fins (Domain domain, Variable vari, Term[] args) { if (vari.val.IsStr) vari.val.objval @@ -1025,7 +1010,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term del (Domain domain, Variable vari, Term[] args) + private static Term Fdel (Domain domain, Variable vari, Term[] args) { if (vari.val.IsStr) vari.val.objval @@ -1040,7 +1025,7 @@ namespace System.Xml.Expression return vari.val; } - private static Term and (Domain domain, Variable vari, Term[] args) + private static Term Fand (Domain domain, Variable vari, Term[] args) { foreach (Term arg in args) if (! arg.Eval (domain).IsTrue) @@ -1048,7 +1033,7 @@ namespace System.Xml.Expression return One; } - private static Term or (Domain domain, Variable vari, Term[] args) + private static Term For (Domain domain, Variable vari, Term[] args) { foreach (Term arg in args) if (arg.Eval (domain).IsTrue) @@ -1056,12 +1041,12 @@ namespace System.Xml.Expression return Zero; } - private static Term eval_clause (Domain domain, Variable vari, Term[] args) + private static Term Feval (Domain domain, Variable vari, Term[] args) { return (args[0].Eval (domain)); } - private static Term progn_clause (Domain domain, Variable vari, Term[] args) + private static Term Fprogn (Domain domain, Variable vari, Term[] args) { Term result = One; @@ -1070,7 +1055,7 @@ namespace System.Xml.Expression return result; } - private static Term if_clause (Domain domain, Variable vari, Term[] args) + private static Term Fif (Domain domain, Variable vari, Term[] args) { if (args[0].Eval (domain).IsTrue) return args[1].Eval (domain); @@ -1079,7 +1064,7 @@ namespace System.Xml.Expression return args[2].Eval (domain); } - private static Term when_clause (Domain domain, Variable vari, Term[] args) + private static Term Fwhen (Domain domain, Variable vari, Term[] args) { if (! args[0].Eval (domain).IsTrue) return Zero; @@ -1089,29 +1074,29 @@ namespace System.Xml.Expression return result; } - private static Term return_clause (Domain domain, Variable vari, Term[] args) + private static Term Freturn (Domain domain, Variable vari, Term[] args) { domain.ThrowReturn (); return args.Length == 0 ? Zero : args[0]; } - private static Term break_clause (Domain domain, Variable vari, Term[] args) + private static Term Fbreak (Domain domain, Variable vari, Term[] args) { domain.ThrowBreak (); return args.Length == 0 ? Zero : args[0]; } - private static Term loop_clause (Domain domain, Variable vari, Term[] args) + private static Term Floop (Domain domain, Variable vari, Term[] args) { Term result = Zero; try { domain.Catch (Nbreak); - while (! domain.Catched) + while (! domain.Thrown) foreach (Term arg in args) { result = arg.Eval (domain); - if (domain.Catched) - break; + if (domain.Thrown) + return result; } } finally { domain.Uncatch (); @@ -1119,17 +1104,17 @@ namespace System.Xml.Expression return result; } - private static Term while_clause (Domain domain, Variable vari, Term[] args) + private static Term Fwhile (Domain domain, Variable vari, Term[] args) { Term result = Zero; try { domain.Catch (Nbreak); - while (! domain.Catched && args[0].Eval (domain).IsTrue) + while (! domain.Thrown && args[0].Eval (domain).IsTrue) for (int i = 1; i < args.Length; i++) { result = args[i].Eval (domain); - if (domain.Catched) - break; + if (domain.Thrown) + return result; } } finally { domain.Uncatch (); @@ -1137,7 +1122,7 @@ namespace System.Xml.Expression return result; } - private static Term cond_clause (Domain domain, Variable vari, Term[] args) + private static Term Fcond (Domain domain, Variable vari, Term[] args) { foreach (Term arg in args) { @@ -1154,12 +1139,14 @@ namespace System.Xml.Expression return Zero; } - private static Term foreach_clause (Domain domain, Variable vari, + private static Term Fforeach (Domain domain, Variable vari, Term[] args) { - List list = args[0].Eval (domain).Listval; + Term result = args[0].Eval (domain); + if (domain.Thrown) + return result; + List list = result.Listval; Bindings current = domain.bindings; - Term result = Zero; try { domain.Catch (Nbreak); @@ -1170,14 +1157,12 @@ namespace System.Xml.Expression for (int i = 1; i < args.Length; i++) { result = args[i].Eval (domain); - if (domain.Catched) - break; + if (domain.Thrown) + return result; } } finally { domain.UnboundTo (current); } - if (domain.Catched) - break; } } finally { domain.Uncatch (); @@ -1185,12 +1170,12 @@ namespace System.Xml.Expression return result; } - private static Term quote_clause (Domain domain, Variable vari, Term[] args) + private static Term Fquote (Domain domain, Variable vari, Term[] args) { return new Term (args[0]); } - private static Term type_clause (Domain domain, Variable vari, Term[] args) + private static Term Ftype (Domain domain, Variable vari, Term[] args) { if (args[0].IsInt) return TermInt; @@ -1203,6 +1188,49 @@ namespace System.Xml.Expression return TermTerm; } + public abstract class Object + { + public abstract Term Eval (Domain domain); + } + + private class Funcall : Object + { + internal Function func; + internal Variable vari; + internal Term[] args; + + public Funcall (Function func, Variable vari, Term[] args) + { + this.func = func; + this.vari = vari; + this.args = args; + } + + public override Term Eval (Domain domain) + { + domain.DebugWrite (true, "(({0}", func.name); + for (int i = 0; i < args.Length; i++) + domain.DebugWrite (false, " {0}", args[i].ToString ()); + domain.DebugWrite (false, ")"); + domain.depth++; + Term result = func.Call (domain, vari, args); + domain.depth--; + domain.DebugWrite (true, " ==> {0})", result.ToString ()); + return result; + } + + public override string ToString () + { + string str = ""; + str += "\">"; + foreach (Term e in args) + str += e; + return (str + ""); + } + } + public struct Term { public int intval; @@ -1213,6 +1241,7 @@ namespace System.Xml.Expression public Term (string str) { intval = 0; objval = str; } public Term (List list) { intval = 0; objval = list; } public Term (Term term) { intval = 0; objval = term; } + public Term (Object obj) { intval = 0; objval = obj; } public Term (XmlNode node, Domain domain) { @@ -1266,44 +1295,6 @@ namespace System.Xml.Expression } } - private class Funcall - { - internal Function func; - internal Variable vari; - internal Term[] args; - - public Funcall (Function func, Variable vari, Term[] args) - { - this.func = func; - this.vari = vari; - this.args = args; - } - - public Term Eval (Domain domain) - { - domain.DebugWrite (true, "(({0}", func.name); - for (int i = 0; i < args.Length; i++) - domain.DebugWrite (false, " {0}", args[i].ToString ()); - domain.DebugWrite (false, ")"); - domain.depth++; - Term result = func.Call (domain, vari, args); - domain.depth--; - domain.DebugWrite (true, " ==> {0})", result.ToString ()); - return result; - } - - public override string ToString () - { - string str = ""; - str += "\">"; - foreach (Term e in args) - str += e; - return (str + ""); - } - } - public int Intval { get { if (objval != null) @@ -1348,8 +1339,8 @@ namespace System.Xml.Expression return this; if (objval is List) return new Term ((List) objval); - if (objval is Funcall) - return ((Funcall) objval).Eval (domain); + if (objval is Object) + return ((Object) objval).Eval (domain); if (objval is Variable) return ((Variable) objval).val; if (objval is Term) @@ -1422,6 +1413,8 @@ namespace System.Xml.Expression int c = str[0]; int i; + if (c == '?') + return str[1]; if (c == '0' && str[1] == 'x') { i = 0; @@ -1511,8 +1504,8 @@ namespace System.Xml.Expression foreach (Term term in terms) { result = term.Eval (domain); - if (domain.Catched) - break; + if (domain.Thrown) + return result; } } finally { domain.Uncatch ();