From 3c39b282b53d78ca6e76ababf377aec3a081303a Mon Sep 17 00:00:00 2001 From: handa Date: Thu, 15 Oct 2009 00:03:08 +0000 Subject: [PATCH] *** empty log message *** --- MInputMethod.cs | 6 +- XmlExpr.cs | 221 ++++++++++++++++++++++++++++++------------------------- xex.cs | 2 +- 3 files changed, 126 insertions(+), 103 deletions(-) diff --git a/MInputMethod.cs b/MInputMethod.cs index 258c929..be452c1 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -1542,7 +1542,7 @@ namespace M17N.Input } } if (vari == null) - domain.Defvar (new Xex.Variable.Int (name, desc, val, range)); + domain.Defvar (new Xex.Variable.Int (domain, name, desc, val, range)); else { Xex.Term term = new Xex.Term (val); @@ -1571,7 +1571,7 @@ namespace M17N.Input } } if (vari == null) - domain.Defvar (new Xex.Variable.Str (name, desc, (string) val, range)); + domain.Defvar (new Xex.Variable.Str (domain, name, desc, (string) val, range)); else { Xex.Term term = new Xex.Term ((string) val); @@ -1601,7 +1601,7 @@ namespace M17N.Input } } if (vari == null) - domain.Defvar (new Xex.Variable.Sym (name, desc, sym, range)); + domain.Defvar (new Xex.Variable.Sym (domain, name, desc, sym, range)); else { Xex.Term term = new Xex.Term (sym); diff --git a/XmlExpr.cs b/XmlExpr.cs index 73499af..a07ab65 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -85,7 +85,6 @@ namespace System.Xml.Expression private static Symbol Qnull = ""; private static Symbol Qfuncall = "funcall"; - private static Symbol Qvarref = "varref"; private static Symbol Qinteger = "integer"; private static Symbol Qstring = "string"; private static Symbol Qsymbol = "symbol"; @@ -319,26 +318,28 @@ namespace System.Xml.Expression } } - public class Variable : TermValue + public class Variable { + public Domain domain; public readonly Symbol name; public string desc; - internal Term default_val; - internal Term val; + protected Term default_val; + protected Term val; object range; - public Variable (Symbol name, Term value) + public Variable (Domain domain, Symbol name, Term val) { + this.domain = domain; this.name = name; - val = value; + this.val = val; default_val = Zero; } public virtual bool ValueP (Term val) { return true; } - public override TermValue Clone () + public Variable Clone (Domain domain) { - Variable v = new Variable (name, val); + Variable v = new Variable (domain, name, val); v.desc = desc; v.default_val = default_val; v.range = range; @@ -355,6 +356,19 @@ namespace System.Xml.Expression } } + public Term SetValue (int i) + { + val.intval = i; + val.objval = null; + return val; + } + + public Term SetValue (string s) + { + val.objval = s; + return val; + } + public Term DefaultValue { get { return default_val; } @@ -371,8 +385,6 @@ namespace System.Xml.Expression set { range = value; } } - public override Term Eval (Domain domain) { return val; } - public override string ToString () { return name + "(" + val + ")"; } public class Int : Variable @@ -405,8 +417,9 @@ namespace System.Xml.Expression return false; } - public Int (Symbol name, string description, int value, int[] range) - : base (name, new Term (value)) + public Int (Domain domain, Symbol name, string description, + int value, int[] range) + : base (domain, name, new Term (value)) { if (! SubsetP (value, range)) throw new Exception ("Invalid value: " + value); @@ -459,8 +472,9 @@ namespace System.Xml.Expression return false; } - public Str (Symbol name, string description, string value, string[] range) - : base (name, new Term (value)) + public Str (Domain domain, Symbol name, string description, + string value, string[] range) + : base (domain, name, new Term (value)) { if (! SubsetP (value, range)) throw new Exception ("Invalid value: " + value); @@ -513,8 +527,9 @@ namespace System.Xml.Expression return false; } - public Sym (Symbol name, string description, Symbol value, Symbol[] range) - : base (name, new Term (value)) + public Sym (Domain domain, Symbol name, string description, + Symbol value, Symbol[] range) + : base (domain, name, new Term (value)) { if (! SubsetP (value, range)) throw new Exception ("Invalid value: " + value); @@ -553,7 +568,7 @@ namespace System.Xml.Expression private Bindings (Variable vari) { this.vari = vari; - old_value = vari.val; + old_value = vari.Value; } public static Bindings Bind (Bindings bindings, Variable vari, Term val) @@ -825,7 +840,7 @@ namespace System.Xml.Expression { if (! (vari is Variable.Int)) throw new Exception ("Inalid value"); - vari = (Variable) vari.Clone (); + vari = (Variable) vari.Clone (this); Term v = new Term (intval); vari.Value = v; vari.DefaultValue = v; @@ -833,7 +848,7 @@ namespace System.Xml.Expression vari.Range = range; } else - vari = new Variable.Int (name, desc, intval, range); + vari = new Variable.Int (this, name, desc, intval, range); } else if (type == Qstring) { @@ -849,7 +864,7 @@ namespace System.Xml.Expression { if (! (vari is Variable.Str)) throw new Exception ("Invalid value"); - vari = (Variable) vari.Clone (); + vari = (Variable) vari.Clone (this); Term v = new Term (val); vari.Value = v; vari.DefaultValue = v; @@ -857,7 +872,7 @@ namespace System.Xml.Expression vari.Range = range; } else - vari = new Variable.Str (name, desc, val, range); + vari = new Variable.Str (this, name, desc, val, range); } else if (type == Qsymbol) { @@ -873,7 +888,7 @@ namespace System.Xml.Expression { if (! (vari is Variable.Sym)) throw new Exception ("Invalid value"); - vari = (Variable) vari.Clone (); + vari = (Variable) vari.Clone (this); Term v = new Term (val); vari.Value = v; vari.DefaultValue = v; @@ -881,7 +896,7 @@ namespace System.Xml.Expression vari.Range = range; } else - vari = new Variable.Sym (name, desc, val, range); + vari = new Variable.Sym (this, name, desc, val, range); } else throw new Exception ("Unknown type: " + type); @@ -889,9 +904,9 @@ namespace System.Xml.Expression else { if (variables.TryGetValue (name, out vari)) - vari = (Variable) vari.Clone (); + vari = (Variable) vari.Clone (this); else - vari = new Variable (name, Zero); + vari = new Variable (this, name, Zero); } variables[name] = vari; return vari; @@ -899,13 +914,14 @@ namespace System.Xml.Expression public Variable Defvar (Variable vari) { + vari = vari.Clone (this); variables[vari.name] = vari; return vari; } internal Variable Defvar (Symbol name) { - Variable vari = new Variable (name, Zero); + Variable vari = new Variable (this, name, Zero); variables[name] = vari; return vari; } @@ -941,7 +957,7 @@ namespace System.Xml.Expression { if (! create) return null; - variables[name] = vari = new Variable (name, Zero); + variables[name] = vari = new Variable (this, name, Zero); } return vari; } @@ -982,7 +998,7 @@ namespace System.Xml.Expression Dictionary values = new Dictionary (); foreach (KeyValuePair kv in variables) - values[kv.Value] = kv.Value.val.Clone (); + values[kv.Value] = kv.Value.Value.Clone (); return values; } @@ -1001,6 +1017,7 @@ namespace System.Xml.Expression static Xexpression () { basic.DefTerm ("funcall", Funcall.parser); + basic.DefTerm ("varref", Varref.parser); basic.DefSubr (Fset, "set", true, 1, 1, "="); basic.DefSubr (Fnot, "not", false, 1, 1, "!"); @@ -1047,7 +1064,7 @@ namespace System.Xml.Expression private static Term Fset (Domain domain, Variable vari, Term[] args) { vari.Value = args[0]; - return vari.val; + return args[0]; } private static Term Fnot (Domain domain, Variable vari, Term[] args) @@ -1057,25 +1074,19 @@ namespace System.Xml.Expression private static Term Fadd (Domain domain, Variable vari, Term[] args) { - int n = vari == null ? 0 : vari.val.Intval; + int n = vari == null ? 0 : vari.Value.Intval; foreach (Term arg in args) n += arg.Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Fmul (Domain domain, Variable vari, Term[] args) { - int n = vari == null ? 1 : vari.val.Intval; + int n = vari == null ? 1 : vari.Value.Intval; foreach (Term arg in args) n *= arg.Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Fsub (Domain domain, Variable vari, Term[] args) @@ -1089,15 +1100,12 @@ namespace System.Xml.Expression } else { - n = vari.val.Intval; + n = vari.Value.Intval; i = 0; } while (i < args.Length) n -= args[i++].Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Fdiv (Domain domain, Variable vari, Term[] args) @@ -1111,34 +1119,27 @@ namespace System.Xml.Expression } else { - n = vari.val.Intval; + n = vari.Value.Intval; i = 0; } while (i < args.Length) n /= args[i++].Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Fmod (Domain domain, Variable vari, Term[] args) { - if (vari == null) - return new Term (args[0].Intval % args[1].Intval); - vari.val.intval = vari.val.Intval % args[0].Intval; - return vari.val; + int n = args[0].Intval % args[1].Intval; + + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Flogior (Domain domain, Variable vari, Term[] args) { - int n = vari == null ? 0 : vari.val.Intval; + int n = vari == null ? 0 : vari.Value.Intval; foreach (Term arg in args) n |= arg.Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Flogand (Domain domain, Variable vari, Term[] args) @@ -1152,31 +1153,24 @@ namespace System.Xml.Expression } else { - n = vari.val.Intval; + n = vari.Value.Intval; i = 0; } while (i < args.Length) n &= args[i++].Intval; - if (vari == null) - return new Term (n); - vari.val.intval = n; - return vari.val; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Flsh (Domain domain, Variable vari, Term[] args) { - if (vari == null) - return new Term (args[0].Intval << args[1].Intval); - vari.val.intval = vari.val.Intval << args[0].Intval; - return vari.val; + int n = args[0].Intval << args[1].Intval; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Frsh (Domain domain, Variable vari, Term[] args) { - if (vari == null) - return new Term (args[0].Intval >> args[1].Intval); - vari.val.intval = vari.val.Intval >> args[0].Intval; - return vari.val; + int n = args[0].Intval >> args[1].Intval; + return (vari == null ? new Term (n) : vari.SetValue (n)); } private static Term Feq (Domain domain, Variable vari, Term[] args) @@ -1263,7 +1257,7 @@ namespace System.Xml.Expression if (vari == null) list = new List (); else - list = vari.val.Listval; + list = vari.Value.Listval; foreach (Term arg in args) { @@ -1279,7 +1273,7 @@ namespace System.Xml.Expression result.objval = list; return result; } - return vari.val; + return vari.Value; } private static Term Fconcat (Domain domain, Variable vari, Term[] args) @@ -1289,7 +1283,7 @@ namespace System.Xml.Expression if (vari == null) str = ""; else - str = vari.val.Strval; + str = vari.Value.Strval; foreach (Term arg in args) { @@ -1301,15 +1295,15 @@ namespace System.Xml.Expression else str += (char) arg.Intval; } + if (vari == null) { - Term result; - result.intval = 0; - result.objval = str; - return result; + Term term; + term.intval = 0; + term.objval = str; + return term; } - vari.val.objval = str; - return vari.val; + return vari.SetValue (str); } private static Term Fnth (Domain domain, Variable vari, Term[] args) @@ -1341,29 +1335,35 @@ namespace System.Xml.Expression private static Term Fins (Domain domain, Variable vari, Term[] args) { - if (vari.val.IsStr) - vari.val.objval - = vari.val.Strval.Insert (args[0].Intval, args[1].Strval); - else if (vari.val.IsList) - vari.val.Listval.InsertRange (args[0].Intval, args[1].Listval); + Term term = vari.Value; + + if (term.IsStr) + { + string str = term.Strval.Insert (args[0].Intval, args[1].Strval); + vari.SetValue (str); + } + else if (vari.Value.IsList) + vari.Value.Listval.InsertRange (args[0].Intval, args[1].Listval); else - throw new Exception ("term is not collection: " + vari.val); - return vari.val; + throw new Exception ("term is not collection: " + vari.Value); + return vari.Value; } private static Term Fdel (Domain domain, Variable vari, Term[] args) { - if (vari.val.IsStr) - vari.val.objval - = vari.val.Strval.Remove (args[0].Intval, - args[1].Intval - args[0].Intval); - - else if (vari.val.IsList) - vari.val.Listval.RemoveRange (args[0].Intval, + if (vari.Value.IsStr) + { + string str + = vari.Value.Strval.Remove (args[0].Intval, + args[1].Intval - args[0].Intval); + vari.SetValue (str); + } + else if (vari.Value.IsList) + vari.Value.Listval.RemoveRange (args[0].Intval, args[1].Intval - args[0].Intval); else - throw new Exception ("term is not collection: " + vari.val); - return vari.val; + throw new Exception ("term is not collection: " + vari.Value); + return vari.Value; } private static Term Fand (Domain domain, Variable vari, Term[] args) @@ -1567,6 +1567,31 @@ namespace System.Xml.Expression public virtual TermValue Clone () { return this; } } + private class Varref : TermValue + { + private Symbol name; + private Variable vari; + + public Varref (Symbol name) { this.name = name; } + + public override Term Eval (Domain domain) + { + if (vari == null || vari.domain != domain) + vari = domain.GetVar (name, true); + return vari.Value; + } + + internal static TermValue parser (Domain domain, XmlNode node) + { + return new Varref ((Symbol) node.Attributes[Qvname].Value); + } + + public override string ToString () + { + return ""; + } + } + private class Funcall : TermValue { internal Function func; @@ -1695,8 +1720,6 @@ namespace System.Xml.Expression objval = (Symbol) node.InnerText; else if (name == Qstring) objval = node.InnerText.Clone (); - else if (name == Qvarref) - objval = domain.GetVar ((Symbol) node.Attributes[0].Value, true); else if (name == Qlist) { List list = new List (); @@ -1721,7 +1744,7 @@ namespace System.Xml.Expression public Term (Domain domain, Symbol vname) { intval = 0; - objval = domain.GetVar (vname, true); + objval = new Varref (vname); } // ... diff --git a/xex.cs b/xex.cs index 204da36..60be5ed 100644 --- a/xex.cs +++ b/xex.cs @@ -9,7 +9,7 @@ public class Test public static void Main(params string[] args) { Xex.debug_level = 10; - Xex.Domain domain = new Xex.Domain (null); + Xex.Domain domain = new Xex.Domain ("test", null); Xex xex = new Xex (domain, "xex.xml"); if (args.Length >= 2 && args[0] == "-d") -- 1.7.10.4