{
public class Xexpression
{
- private static int debug_depth = 0;
+ private static int trace_depth = 0;
- public static int DebugDepth {
- get { return debug_depth; }
- set { debug_depth = value; }
+ public static int TraceDepth {
+ get { return trace_depth; }
+ set { trace_depth = value; }
}
public struct Symbol : IEquatable<Symbol>
public override bool Equals (object obj)
{
- return Object.ReferenceEquals (this.name, obj);
+ return (obj is Symbol ? this == (Symbol) obj
+ : obj is string ? this.name == (string) obj
+ : false);
}
public override int GetHashCode ()
private static Symbol Qdescription = "description";
private static Symbol Qrange = "range";
+ 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;
public Function () { }
- public Function (Symbol name, int min_arg, int max_arg)
+ public Function (Symbol name, int min_arg, int max_arg, bool setvar)
{
this.name = name;
this.min_arg = min_arg;
this.max_arg = max_arg;
+ this.setvar = setvar;
}
public abstract Term Call (Domain domain, Variable vari, Term[] args);
internal class Subroutine : Function
{
public Builtin builtin;
- public bool setvar;
public Subroutine (Builtin builtin, Symbol name, bool setvar,
int min_arg, int max_arg)
- : base (name, min_arg, max_arg)
+ : base (name, min_arg, max_arg, setvar)
{
this.builtin = builtin;
- this.setvar = setvar;
}
public override Term Call (Domain domain, Variable vari, Term[] args)
{
- if (args != null)
+ if (args.Length > 0)
{
args = (Term[]) args.Clone ();
for (int i = 0; i < args.Length; i++)
{
public Builtin builtin;
- public SpecialForm (Builtin builtin, Symbol name,
+ public SpecialForm (Builtin builtin, Symbol name, bool setvar,
int min_arg, int max_arg)
- : base (name, min_arg, max_arg)
+ : base (name, min_arg, max_arg, setvar)
{
this.builtin = builtin;
}
for (n = node.FirstChild; n != null; n = n.NextSibling)
{
if (n.Name == Qfixed)
- nfixed++;
+ nfixed += n.ChildNodes.Count;
else if (n.Name == Qoptional)
- noptional++;
+ noptional += n.ChildNodes.Count;
else if (n.Name == Qrest)
nrest++;
else
args = new Variable[max_arg];
n = node.FirstChild;
for (int i = 0; i < max_arg; n = n.NextSibling)
- args[i++] = domain.Defvar ((Symbol) n.Attributes[0].Value);
+ {
+ 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 = null;
+ args = new Variable[0];
}
}
{
parse_head (domain, node, out name, out min_arg, out max_arg,
out args);
+ setvar = false;
}
public void SetBody (Domain domain, XmlNode node)
{
parse_head (domain, node, out name, out min_arg, out max_arg,
out args);
+ setvar = false;
}
public Lambda (Domain domain, Symbol name, Symbol[] args)
int nrest = 0;
this.name = name;
- if (args != null)
+ if (args.Length > 0)
{
int i = 0;
for (i = 0; i < args.Length; i++, nfixed++)
{
min_arg = max_arg = 0;
}
+ setvar = false;
}
public void SetBody (Domain domain, XmlNode node)
try {
int i;
- if (args != null)
+ if (args.Length > 0)
{
Term[] newargs = new Term[args.Length];
for (i = 0; i < min_arg; i++)
}
}
- private static void throw_exception (string msg)
- {
- throw new Exception (msg);
- }
-
private static void throw_exception (string fmt, params object[] args)
{
throw new Exception (String.Format (fmt, args));
}
- internal class CatchTag : IEquatable<CatchTag>
+ internal struct CatchTag
{
- private object val;
+ private Term tag;
+
+ public CatchTag (Symbol sym) { tag = new Term (sym); }
+ public CatchTag (Symbol sym, string msg) { tag = new Term (sym, msg); }
+ public CatchTag (ErrorTerm e) { tag = new Term (e); }
+ private CatchTag (int i) { tag = new Term (i); }
- public CatchTag (Symbol sym) { val = (string) sym; }
- private CatchTag (int i) { val = i; }
+ public Term Tag { get { return tag; } }
public static CatchTag Return = new CatchTag (0);
public static CatchTag Break = new CatchTag (1);
-
- public static bool operator== (CatchTag t1, CatchTag t2)
- { return t1.val == t2.val; }
-
- public static bool operator!= (CatchTag t1, CatchTag t2)
- { return t1.val != t2.val; }
-
- public bool Equals (CatchTag tag) { return this.val == tag.val; }
- public override bool Equals (object val) { return this.val == val; }
-
- public override int GetHashCode () { return val.GetHashCode (); }
}
public class Domain
public Symbol name;
public object context;
public int depth = 0;
+ private Stack<Term> call_stack = new Stack<Term> ();
internal Dictionary<Symbol, MethodInfo> termtypes
= new Dictionary<Symbol, MethodInfo> ();
this.context = context;
}
+ internal void PushTerm (Term term)
+ {
+ call_stack.Push (term);
+ }
+
+ internal void PopTerm ()
+ {
+ call_stack.Pop ();
+ }
+
internal void Bind (Variable vari, Term value)
{
bindings = Bindings.Bind (bindings, vari, value);
catch_count--;
}
- public bool Thrown {
- get { return catch_count < catch_stack.Count; }
- }
+ public bool Thrown { get { return catch_count < catch_stack.Count; } }
internal void ThrowReturn ()
{
- foreach (CatchTag tag in catch_stack)
+ foreach (CatchTag elt in catch_stack)
{
catch_count--;
- if (tag == CatchTag.Return)
+ if (elt.Tag == CatchTag.Return.Tag)
break;
}
}
- internal void ThrowBreak ()
+ internal bool ThrowBreak ()
{
- if (catch_stack.Peek () != CatchTag.Break)
- throw_exception ("No outer loop to break");
+ if (catch_stack.Peek ().Tag != CatchTag.Break.Tag)
+ return false;
catch_count--;
+ return true;
}
- internal void ThrowTag (CatchTag tag)
+ internal void ThrowSymbol (Term tag)
{
foreach (CatchTag elt in catch_stack)
{
catch_count--;
- if (elt == tag)
+ if (elt.Tag.Matches (tag))
break;
}
}
+ internal Term ThrowError (Term tag)
+ {
+ tag = tag.Clone ();
+ ErrorTerm e = tag.objval as ErrorTerm;
+ e.CallStack = call_stack.ToArray ();
+
+ foreach (CatchTag elt in catch_stack)
+ {
+ catch_count--;
+ if (elt.Tag.Matches (tag))
+ break;
+ }
+ return tag;
+ }
+
public void DefType (Type type)
{
if (! type.IsSubclassOf (typeof (TermValue)))
functions[(Symbol) a] = func;
}
- public void DefSpecial (Builtin builtin, string str,
+ 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,
+ Function func = new Function.SpecialForm (builtin, name, setvar,
min_arg, max_arg);
functions[name] = func;
foreach (string a in aliases)
return str;
}
- internal void DebugWrite (string fmt, params string[] arg)
+ internal void TraceWrite (string fmt, params string[] arg)
{
- if (debug_depth > depth)
+ if (trace_depth > depth)
{
for (int i = 0; i < depth; i++)
Console.Write (" ");
{
basic.DefType (typeof (Varref));
basic.DefType (typeof (Funcall));
+ basic.DefType (typeof (ErrorTerm));
basic.DefSubr (Fset, "set", true, 1, 1, "=");
basic.DefSubr (Fnot, "not", false, 1, 1, "!");
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);
- basic.DefSpecial (Fcatch, "catch", 2, 2);
- basic.DefSpecial (Fthrow, "throw", 1, 2);
+ 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 (Fif, "if", false, 2, 3);
+ basic.DefSpecial (Fwhen, "when", false, 1, -1);
+ basic.DefSpecial (Floop, "loop", false, 1, -1);
+ basic.DefSpecial (Fwhile, "while", false, 1, -1);
+ basic.DefSpecial (Fcond, "cond", false, 1, -1);
+ basic.DefSpecial (Fforeach, "foreach", true, 2, -1);
+ basic.DefSpecial (Fquote, "quote", false, 1, 1);
+ basic.DefSpecial (Ftype, "type", false, 1, 1);
+ basic.DefSpecial (Fcatch, "catch", true, 2, -1);
}
private static Term Fset (Domain domain, Variable vari, Term[] args)
{
+ if (vari == null)
+ return new Term (new ErrorTerm (Error.NoVariable, ""));
vari.Value = args[0];
return args[0];
}
private static Term Fbreak (Domain domain, Variable vari, Term[] args)
{
- domain.ThrowBreak ();
+ if (! domain.ThrowBreak ())
+ return domain.ThrowError (new Term (Error.NoOuterLoop, null));
return args.Length == 0 ? Zero : args[0];
}
return Zero;
}
- private static Term Fforeach (Domain domain, Variable vari,
- Term[] args)
+ private static Term Fforeach (Domain domain, Variable vari, Term[] args)
{
Term result = args[0].Eval (domain);
if (domain.Thrown)
private static Term Fquote (Domain domain, Variable vari, Term[] args)
{
- return new Term (args[0]);
+ return args[0];
}
private static Term Ftype (Domain domain, Variable vari, Term[] args)
public static Term Fcatch (Domain domain, Variable vari, Term[] args)
{
Term result = Zero;
+
try {
domain.Catch (new CatchTag (args[0].Symval));
- result = args[1].Eval (domain);
+ for (int i = 1; i < args.Length; i++)
+ {
+ result = args[i].Eval (domain);
+ if (domain.Thrown)
+ return result;
+ }
} finally {
domain.Uncatch ();
}
public static Term Fthrow (Domain domain, Variable vari, Term[] args)
{
- domain.ThrowTag (new CatchTag (args[0].Symval));
- return (args.Length == 1 ? Zero : args[1]);
+ if (args[0].IsSymbol)
+ {
+ domain.ThrowSymbol (args[0]);
+ return (args[args.Length - 1]);
+ }
+ if (args[0].IsError)
+ return domain.ThrowError (args[0]);
+ return domain.ThrowError (new Term (Error.InvalidArgumentType,
+ args[0].ToString ()));
}
- public delegate TermValue TermParser (Domain domain, XmlNode node);
-
public abstract class TermValue
{
public virtual Term Eval (Domain domain) { return new Term (this); }
public virtual TermValue Clone () { return this; }
+ public virtual bool IsTrue { get { return true; } }
+ public virtual bool Matches (TermValue other) { return Equals (other); }
+ public override abstract bool Equals (object obj);
+ public override abstract int GetHashCode ();
+ public abstract string ToString (bool detail);
}
private class Varref : TermValue
return new Varref ((Symbol) node.Attributes[Qvname].Value);
}
- public override string ToString ()
+ public override bool Equals (object obj)
+ {
+ Varref varref = obj as Varref;
+ return (varref != null && varref.vari.name == vari.name);
+ }
+
+ public override int GetHashCode ()
+ {
+ return vari.name.GetHashCode ();
+ }
+
+ public override string ToString () { return ToString (true); }
+
+ public override string ToString (bool detail)
{
return "<varref vname=\"" + vname + "\"/>";
}
private class Funcall : TermValue
{
private static Symbol name = "funcall";
-
public static Symbol Name { get { return name; } }
internal Function func;
public Funcall (Function func, Variable vari, Term[] args)
{
- if (args != null)
- {
- 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);
- }
+ 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);
this.func = func;
this.vari = vari;
this.args = args;
public override Term Eval (Domain domain)
{
- domain.DebugWrite (ToString ());
+ domain.TraceWrite (ToString ());
domain.depth++;
Term result = func.Call (domain, vari, args);
domain.depth--;
- domain.DebugWrite ("=> {0}", result.ToString ());
+ domain.TraceWrite ("=> {0}", result.ToString ());
return result;
}
return new Funcall (func, vari, args);
}
- public override string ToString ()
+ public override bool Equals (object obj)
+ {
+ Funcall funcall = obj as Funcall;
+ return (obj != null
+ && funcall.func == func
+ && funcall.vari == vari
+ && funcall.args.Length == args.Length);
+ }
+
+ public override int GetHashCode ()
+ {
+ return func.GetHashCode () ^ args.Length;
+ }
+
+ public override string ToString () { return ToString (true); }
+
+ public override string ToString (bool detail)
{
string str = "<" + func.name;
if (vari != null)
str += " vname=\"" + vari.name + "\"";
- if (args == null)
+ if (args.Length == 0 || ! detail)
return str + "/>";
str += ">";
if (func is Function.SpecialForm)
}
}
+ internal class ErrorTerm : TermValue
+ {
+ private static Symbol name = "error";
+ public static Symbol Name { get { return name; } }
+
+ private readonly Symbol ename;
+ private string message;
+ private Term[] stack;
+
+ public ErrorTerm (Symbol ename, string message)
+ {
+ this.ename = ename;
+ this.message = message;
+ }
+
+ public static TermValue Parser (Domain domain, XmlNode node)
+ {
+ XmlAttribute attr = node.Attributes[Qename];
+ Symbol ename = attr == null ? Qnull : (Symbol) attr.Value;
+ string message = node.InnerText;
+ return new ErrorTerm (ename, message);
+ }
+
+ public Term[] CallStack { get { return stack; } set { stack = value; } }
+
+ public override TermValue Clone ()
+ {
+ return new ErrorTerm (ename, message);
+ }
+
+ public override bool Equals (object obj)
+ {
+ ErrorTerm e = obj as ErrorTerm;
+ if (e == null || e.ename != ename || e.message != message
+ || (e.stack == null ? stack != null
+ : (stack == null || e.stack.Length != stack.Length)))
+ return false;
+ if (stack != null)
+ for (int i = 0; i < stack.Length; i++)
+ if (! stack[i].Equals (e.stack[i]))
+ return false;
+ return true;
+ }
+
+ public override int GetHashCode ()
+ {
+ return ename.GetHashCode ();
+ }
+
+ public override bool Matches (TermValue term)
+ {
+ ErrorTerm e = term as ErrorTerm;
+ return (e != null && (ename == Qnull || e.ename == ename));
+ }
+
+ public override string ToString () { return ToString (true); }
+
+ public override string ToString (bool detail)
+ {
+ string str = "<error type=\"" + ename + "\">";
+ if (message != null)
+ str += message;
+ if (stack != null)
+ for (int i = 0; i < stack.Length; i++)
+ str += stack[i].ToString (false);
+ return str + "</error>";
+ }
+ }
+
public struct Term
{
public int intval;
public Term (string str) { intval = 0; objval = str; }
// <list>...</list>
public Term (List<Term> list) { intval = 0; objval = list; }
+ // <error ename="ERROR-NAME">ERROR-MESSASGE</error>
+ public Term (Symbol name, string message)
+ {
+ intval = 0;
+ objval = new ErrorTerm (name, message);
+ }
- public Term (Term term) { intval = term.intval; objval = term.objval; }
public Term (TermValue obj) { intval = 0; objval = obj; }
public Term (Domain domain, XmlNode node)
public string Strval {
get {
if (! IsStr)
- throw new Exception ("term is not string: " + this);
+ throw new Exception ("term is not a string: " + this);
return (string) objval;
}
}
public string Symval {
get {
if (! IsSymbol)
- throw new Exception ("term is not symbol: " + this);
+ throw new Exception ("term is not a symbol: " + this);
return (Symbol) objval;
}
}
public List<Term> Listval {
get {
if (! IsList)
- throw new Exception ("term is not list: " + this);
+ throw new Exception ("term is not a list: " + this);
return (List<Term>) objval;
}
}
? (intval != 0)
: objval is List<Term>
? (((List<Term>) objval).Count != 0)
+ : objval is TermValue
+ ? ((TermValue) objval).IsTrue
: true);
}
}
+
public bool IsInt { get { return (objval == null); } }
public bool IsStr { get { return (objval is string); } }
public bool IsSymbol { get { return (objval is Symbol); } }
public bool IsList { get { return (objval is List<Term>); } }
-
- public bool IsType (Type type)
- {
- return (objval == null ? type == typeof (int)
- : type == objval.GetType ());
- }
+ public bool IsError { get { return (objval is ErrorTerm); } }
public Term Eval (Domain domain)
{
- if (objval == null || objval is Symbol || objval is string)
- return this;
- if (objval is List<Term>)
- return new Term ((List<Term>) objval);
- return ((TermValue) objval).Eval (domain);
+ try {
+ domain.PushTerm (this);
+ if (objval == null || objval is Symbol || objval is string
+ || objval is List<Term>)
+ return this;
+ return ((TermValue) objval).Eval (domain);
+ } finally {
+ domain.PopTerm ();
+ }
}
public Term Clone ()
return new Term (((TermValue) objval).Clone ());
}
- public override string ToString ()
+ public static bool operator== (Term t1, Term t2)
+ {
+ return t1.Equals (t2);
+ }
+
+ public static bool operator!= (Term t1, Term t2) { return ! (t1 == t2); }
+
+ public override bool Equals (object obj)
+ {
+ if (! (obj is Term))
+ return false;
+ Term term = (Term) obj;
+ if (IsInt)
+ return term.IsInt && term.Intval == Intval;
+ if (IsStr)
+ return term.IsStr && term.Strval == Strval;
+ if (IsSymbol)
+ return term.IsSymbol && term.Symval == Symval;
+ if (IsList)
+ {
+ if (! term.IsList)
+ return false;
+ List<Term> l1 = objval as List<Term>;
+ List<Term> l2 = term.objval as List<Term>;
+
+ 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).Equals (obj);
+ }
+
+ public override int GetHashCode () { return intval; }
+
+ public bool Matches (Term term)
+ {
+ if (IsInt)
+ return term.IsInt && term.Intval == Intval;
+ if (IsStr)
+ return term.IsStr && term.Strval == Strval;
+ if (IsSymbol)
+ return term.IsSymbol && term.Symval == Symval;
+ if (IsList)
+ return (((List<Term>) objval).Count > 0
+ && ((List<Term>) objval)[0].Matches (term));
+ return ((TermValue) objval).Matches ((TermValue) term.objval);
+ }
+
+ public override string ToString () { return ToString (true); }
+
+ public string ToString (bool detail)
{
string str;
str = "<string>" + objval + "</string>";
else if (objval is List<Term>)
{
- str = "<list>";
- foreach (Term e in (List<Term>) objval)
- str += e;
- str += "</list>";
+ if (detail)
+ {
+ str = "<list>";
+ foreach (Term e in (List<Term>) objval)
+ str += e;
+ str += "</list>";
+ }
+ else
+ str = "<list/>";
}
else if (objval is Term)
str = "<quote>" + objval + "</quote>";
else if (objval is TermValue)
- str = ((TermValue) objval).ToString ();
+ str = ((TermValue) objval).ToString (detail);
else
throw new Exception ("invalid Term object: " + objval);
return str;