From: handa Date: Fri, 18 Sep 2009 00:03:02 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6e7c78626156216ba804f51fe818edbc63d4955;p=m17n%2Fm17n-lib-cs.git *** empty log message *** --- diff --git a/XmlExpr.cs b/XmlExpr.cs index ea2ebd3..2ab34f2 100644 --- a/XmlExpr.cs +++ b/XmlExpr.cs @@ -242,8 +242,8 @@ namespace System.Xml.Expression } for (i = 0; i < min_arg; i++) { - Variable var = domain.GetVar ((Name) this.args[i]); - domain.Bind (var, args[i]); + Variable vari = domain.GetVar ((Name) this.args[i]); + domain.Bind (vari, args[i]); } if (body != null) foreach (Term term in body) @@ -587,7 +587,7 @@ namespace System.Xml.Expression return str; } - public void DebugWrite (bool head, string fmt, params string[] arg) + internal void DebugWrite (bool head, string fmt, params string[] arg) { if (debug_level > depth) { @@ -638,16 +638,20 @@ namespace System.Xml.Expression basic.DefSubr (less_eq, "le", 2, -1, "<="); basic.DefSubr (greater_than, "gt", 2, -1, ">"); basic.DefSubr (greater_eq, "ge", 2, -1, ">="); - basic.DefSubr (copy, "copy", 1, 1); basic.DefSubr (append, "append", 0, -1); basic.DefSubr (concat, "concat", 0, -1); basic.DefSubr (nth, "nth", 2, 2); + basic.DefSubr (copy, "copy", 1, 1); + basic.DefSubr (ins, "ins", 3, 3); + basic.DefSubr (del, "del", 3, 3); basic.DefSubr (eval_clause, "eval", 1, 1); basic.DefSpecial (quote_clause, "quote", 1, 1); basic.DefSpecial (progn_clause, "progn", 0, -1, "expr"); - basic.DefSpecial (if_clause, "if", 2, -1); + basic.DefSpecial (if_clause, "if", 2, 3); basic.DefSpecial (when_clause, "when", 1, -1); basic.DefSpecial (while_clause, "while", 1, -1); + basic.DefSpecial (cond_clause, "cond", 1, -1); + basic.DefSpecial (foreach_clause, "foreach", 3, -1); Fprogn = basic.GetFunc (Nprogn); } @@ -920,15 +924,6 @@ namespace System.Xml.Expression return One; } - private static Term copy (Domain domain, Term[] args) - { - object obj = args[0].objval; - - if (! (obj is List)) - throw new Exception ("Invalid arg to copy: " + args[0]); - return new Term (new List ((List) obj)); - } - private static Term append (Domain domain, Term[] args) { List list = new List (); @@ -977,14 +972,57 @@ namespace System.Xml.Expression } else if (args[1].IsList) { - result.intval = 0; - result.objval = ((List) args[1].objval)[args[0].Intval]; + result = ((List) args[1].objval)[args[0].Intval]; } else throw new Exception ("Term is not enumelable: " + args[1]); return result; } + private static Term copy (Domain domain, Term[] args) + { + Term result; + + result.intval = 0; + result.objval = new List (args[0].Listval); + return result; + } + + private static Term ins (Domain domain, Term[] args) + { + if (args[0].IsStr) + { + string str = args[0].Strval.Insert (args[1].Intval, args[2].Strval); + args[0].objval = str; + } + else if (args[0].IsList) + { + args[0].Listval.InsertRange (args[1].Intval, args[2].Listval); + } + else + throw new Exception ("term is not collection: " + args[0]); + return args[0]; + } + + + private static Term del (Domain domain, Term[] args) + { + if (args[0].IsStr) + { + string str = args[0].Strval.Remove (args[1].Intval, + args[2].Intval - args[1].Intval); + args[0].objval = str; + } + else if (args[0].IsList) + { + args[0].Listval.RemoveRange (args[1].Intval, + args[2].Intval - args[1].Intval); + } + else + throw new Exception ("term is not collection: " + args[0]); + return args[0]; + } + private static Term quote_clause (Domain domain, Term[] args) { return new Term (args[0]); @@ -1008,18 +1046,15 @@ namespace System.Xml.Expression { if (args[0].Eval (domain).IsTrue) return args[1].Eval (domain); - - Term result = Zero; - for (int i = 2; i < args.Length; i++) - result = args[i].Eval (domain); - return result; + if (args.Length == 2) + return Zero; + return args[2].Eval (domain); } private static Term when_clause (Domain domain, Term[] args) { if (! args[0].Eval (domain).IsTrue) return Zero; - Term result = One; for (int i = 1; i < args.Length; i++) result = args[i].Eval (domain); @@ -1034,6 +1069,43 @@ namespace System.Xml.Expression return Zero; } + private static Term cond_clause (Domain domain, Term[] args) + { + foreach (Term arg in args) + { + List list = arg.Listval; + Term result = list[0].Eval (domain); + + if (result.IsTrue) + { + for (int i = 1; i < list.Count; i++) + result = list[i].Eval (domain); + return result; + } + } + return Zero; + } + + private static Term foreach_clause (Domain domain, Term[] args) + { + Variable vari = domain.GetVar ((Name) args[0]); + List list = args[1].Listval; + Bindings current = domain.bindings; + + foreach (Term term in list) + { + domain.Bind (vari, term); + try { + for (int i = 2; i < args.Length; i++) + args[i].Eval (domain); + } finally { + domain.UnboundTo (current); + } + } + return Zero; + } + + public struct Term { public int intval; @@ -1136,6 +1208,22 @@ namespace System.Xml.Expression } } + public string Strval { + get { + if (! IsStr) + throw new Exception ("term is not string: " + this); + return (string) objval; + } + } + + public List Listval { + get { + if (! IsList) + throw new Exception ("term is not list: " + this); + return (List) objval; + } + } + public bool IsTrue { get { return (objval == null diff --git a/xex.txt b/xex.txt index 7e947af..5e58f74 100644 --- a/xex.txt +++ b/xex.txt @@ -10,14 +10,14 @@ DEFUN = '' ARGS = - '' SYMBOL * OPTIONAL ? REST ? '' - | '' SYMBOL * OPTIONAL ? REST ? '' + '' VAR * OPTIONAL ? REST ? '' + | '' VAR * OPTIONAL ? REST ? '' OPTIONAL = - '' SYMBOL * + '' VAR * REST = - '' SYMBOL + '' VAR BODY = '' TERM * '' @@ -69,9 +69,9 @@ PREDEFINED-FUNC-SYMBOL = | 'logand' | 'logior' | 'logxor' | 'lsh' | 'logand-set' | 'logior-set' | 'logxor-set' | 'lsh-set' | 'append' | 'concat' | 'nth' | 'copy' | 'ins' | 'del' - | 'case' | 'cond' | 'if' | 'progn' | 'when' + | 'progn' | 'if' | 'when' | 'cond' | 'while' | 'for' | 'foreach' - | 'type' + | 'eval' | 'type' PREDEFINED-FUNC-NAME = PREDEFINED-FUNC-SYMBOL @@ -88,7 +88,7 @@ TRUE = FALSE = '1' -;; TERM can be evaluated and the value is TERM. +;; TERM is evaluated to a TERM. ;; INT is evaluated to itself. ;; STRING is evaluated to itself. ;; SYMBOL is evaluated to itself. diff --git a/xex.xml b/xex.xml index 956c2ed..49f71cb 100644 --- a/xex.xml +++ b/xex.xml @@ -1,44 +1,52 @@ - x + 2 + - 1 + 1 - - x - y - rest + + + - x10 - strABC + 10 + ABC - - sym - sym - sym - - 1 - - - 0x400x41 - - + 1123 + 24 + + 01 + 1st + 1 + 2nd + + + 1011 + + + + + + + 1 + +