using System.IO;
using System.Reflection;
using System.Text;
+using System.Text.RegularExpressions;
using System.Xml;
namespace System.Xml
public abstract bool CheckValue (Term val);
}
- public class Int : Typed
+ public class IntType : Typed
{
private int[] range;
return true;
}
- public Int (Domain domain, Symbol name, int n, string desc, int[] range)
+ public IntType (Domain domain, Symbol name, int n, string desc,
+ int[] range)
: base (domain, name, new Term (n), desc)
{
if (range != null && range.Length % 2 == 1)
public override Variable Clone (Domain domain)
{
- return new Int (domain, name, val.Intval, desc, range);
+ return new IntType (domain, name, val.Intval, desc, range);
}
public int[] Range { get { return range; } set { range = value; } }
}
- public class Str : Typed
+ public class StrType : Typed
{
private string[] range;
return true;
}
- public Str (Domain domain, Symbol name, string str, string desc,
+ public StrType (Domain domain, Symbol name, string str, string desc,
string[] range)
: base (domain, name, new Term (str), desc)
{
{
if (range == null)
return true;
- string str = val.Strval;
- foreach (string s in range)
- if (str == s)
+ Str str = val.Strval;
+ foreach (Str s in range)
+ if (str.Equals (s))
return true;
}
return false;
public override Variable Clone (Domain domain)
{
- return new Str (domain, name, val.Strval, desc, range);
+ return new StrType (domain, name, val.Strval, desc, range);
}
public string[] Range { get { return range; } set { range = value; } }
}
- public class Sym : Typed
+ public class SymType : Typed
{
public Symbol[] range;
return true;
}
- public Sym (Domain domain, Symbol name, Symbol sym, string desc,
- Symbol[] range)
+ public SymType (Domain domain, Symbol name, Symbol sym, string desc,
+ Symbol[] range)
: base (domain, name, new Term (sym), desc)
{
this.range = range;
public override Variable Clone (Domain domain)
{
- return new Sym (domain, name, val.Symval, desc, range);
+ return new SymType (domain, name, val.Symval, desc, range);
}
public Symbol[] Range { get { return range; } set { range = value; } }
if (variables.TryGetValue (name, out vari))
{
- Variable.Int intvari = vari as Variable.Int;
+ Variable.IntType intvari = vari as Variable.IntType;
if (intvari == null)
throw new Error (Error.VariableTypeConflict,
"Not an integer variable: {0}", name);
}
else
{
- vari = new Variable.Int (this, name, n, desc, range);
+ vari = new Variable.IntType (this, name, n, desc, range);
variables[name] = vari;
}
return vari;
if (variables.TryGetValue (name, out vari))
{
- Variable.Str strvari = vari as Variable.Str;
+ Variable.StrType strvari = vari as Variable.StrType;
if (strvari == null)
throw new Error (Error.VariableTypeConflict,
"Not a string variable: {0}", name);
}
else
{
- vari = new Variable.Str (this, name, str, desc, range);
+ vari = new Variable.StrType (this, name, str, desc, range);
variables[name] = vari;
}
return vari;
if (variables.TryGetValue (name, out vari))
{
- Variable.Sym symvari = vari as Variable.Sym;
+ Variable.SymType symvari = vari as Variable.SymType;
if (symvari == null)
throw new Error (Error.VariableTypeConflict,
"Not a symbol variable: {0}", name);
}
else
{
- vari = new Variable.Sym (this, name, sym, desc, range);
+ vari = new Variable.SymType (this, name, sym, desc, range);
variables[name] = vari;
}
return vari;
private static Term Fconcat (Domain domain, Variable vari, Term[] args)
{
- string str;
-
- if (vari == null)
- str = "";
- else
- str = vari.Value.Strval;
+ Str str = vari == null ? new Str ("") : vari.Value.Strval;
foreach (Term arg in args)
{
if (arg.IsStr)
- str += (string) arg.objval;
+ str.Insert (-1, arg.Strval);
else if (arg.IsList)
- foreach (Term term in (List<Term>) arg.objval)
- str += (char) term.Intval;
+ foreach (Term term in arg.Listval)
+ str.Insert (-1, (char) term.Intval);
else
- str += (char) arg.Intval;
+ str.Insert (-1, (char) arg.Intval);
}
-
- Term strterm = new Term (str);
- if (vari != null)
- vari.SetValue (strterm);
- return strterm;
+ return vari == null ? new Term (str) : vari.Value;
}
private static Term Fnth (Domain domain, Variable vari, Term[] args)
Term result;
if (args[1].IsStr)
- {
- result.intval = ((string) args[1].objval)[args[0].Intval];
- result.objval = null;
- }
- else if (args[1].IsList)
- {
- result = ((List<Term>) args[1].objval)[args[0].Intval];
- }
- else
- throw new Error (Error.WrongType,
- "Not a string nor a list: {0}", args[1]);
- return result;
+ return new Term (args[1].Strval.Nth (args[0].Intval));
+ if (args[1].IsList)
+ return args[1].Listval[args[0].Intval];
+ throw new Error (Error.WrongType,
+ "Not a string nor a list: {0}", args[1]);
}
private static Term Fcopy (Domain domain, Variable vari, Term[] args)
{
- Term result;
-
- result.intval = 0;
- result.objval = new List<Term> (args[0].Listval);
- return result;
+ return args[0].Clone ();
}
private static Term Fins (Domain domain, Variable vari, Term[] args)
if (vari == null)
throw new Error (Error.NoVariableName, "No variable name to set");
if (vari.Value.IsStr)
- vari.Value
- = vari.Value.Strval.Insert (args[0].Intval, args[1].Strval);
+ vari.Value.Strval.Insert (args[0].Intval, args[1].Strval);
else if (vari.Value.IsList)
vari.Value.Listval.InsertRange (args[0].Intval, args[1].Listval);
else
{
if (vari == null)
throw new Error (Error.NoVariableName, "No variable name to set");
- Term term = vari.Value;
- if (term.IsStr)
- term.objval = term.Strval.Remove (args[0].Intval,
- args[1].Intval - args[0].Intval);
+ if (vari.Value.IsStr)
+ vari.Value.Strval.Delete (args[0].Intval, args[1].Intval);
else if (vari.Value.IsList)
vari.Value.Listval.RemoveRange (args[0].Intval,
- args[1].Intval - args[0].Intval);
+ args[1].Intval - args[0].Intval);
else
throw new Error (Error.WrongType, "Not a string nor a list: {0}",
vari.Value);
- return term;
+ return vari.Value;
}
private static Term Fand (Domain domain, Variable vari, Term[] args)
}
}
- internal class CharSeq
+ public class Str
{
private string str;
private Regex regex;
- public CharSeq (string str) { this.str = str; }
- public Insert (int index, string str)
+ public Str (string str) { this.str = str; }
+
+ public Str Clone (Str str) { return new Str (str.str); }
+
+ public bool Equals (Str str) { return this.str == str.str; }
+
+ public bool Matches (Str str)
{
- this.str = this.str.Insert (index, str);
+ if (regex = null)
+ regex = new Regex (this.str);
+ return regex.IsMatch (str.str);
}
- public Delete (int from, int to)
+
+ public void Insert (int index, Str str)
+ {
+ if (index < 0)
+ this.str = this.str + str.str;
+ else
+ this.str = this.str.Insert (index, str.str);
+ regex = null;
+ }
+
+ public void Insert (int index, char c)
+ {
+ if (index < 0)
+ this.str = this.str + c;
+ else
+ this.str = this.str.Insert (index, new string (c, 1));
+ regex = null;
+ }
+
+ public void Delete (int from, int to)
{
this.str = this.str.Remove (from, to - from);
+ regex = null;
}
}
}
}
- public string Strval {
+ public Str Strval {
get {
if (! IsStr)
throw new Error (Error.WrongType, "{0} is not a string", this);
- return (StringBuilder) objval;
+ return (Str) objval;
}
}
if (IsSymbol)
return term.IsSymbol && term.Symval == Symval;
if (IsStr)
- return (term.IsStr
- && term.Strval.Length = Strval.Length
- && (Strval.Length == 0
- || (term.Strval.Chars[0] == Strval.Chars[0]
- && term.Strval.ToString () == Strval.ToString ())));
+ return (term.IsStr && term.Strval.Equals (Strval));
if (IsList)
{
if (! term.IsList)
public bool Matches (Term term)
{
if (IsInt)
- return term.IsInt && term.Intval == Intval;
+ return term.IsInt && Intval == term.Intval;
if (IsSymbol)
- return term.IsSymbol && term.Symval == Symval;
+ return term.IsSymbol && Symval == term.Symval;
if (IsStr)
- return term.IsStr && term.Strval == Strval;
+ return term.IsStr && Strval.Matches (term.Strval);
if (IsList)
{
if (! term.IsList)