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";
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);
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 {
}
}
- public abstract class Variable
+ public class Variable : Object
{
public readonly Name name;
internal Term val;
public Variable (Name name, Term term)
{
this.name = name;
- Value = term;
+ val = term;
}
- public Term Value
+ public virtual Term Value
{
get { return val; }
set
}
}
- 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 + ")"; }
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
internal Dictionary<Name, Variable> variables;
internal Bindings bindings;
private Stack<Name> catch_stack = new Stack<Name> ();
- private int catch_count = -1;
+ private int catch_count = 0;
internal Domain ()
{
{
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,
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;
}
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;
}
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;
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)
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;
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;
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);
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)
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;
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;
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;
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];
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];
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++)
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++)
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++)
return One;
}
- private static Term append (Domain domain, Variable vari, Term[] args)
+ private static Term Fappend (Domain domain, Variable vari, Term[] args)
{
List<Term> list;
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;
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;
return result;
}
- private static Term copy (Domain domain, Variable vari, Term[] args)
+ private static Term Fcopy (Domain domain, Variable vari, Term[] args)
{
Term result;
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
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
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)
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)
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;
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);
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;
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 ();
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 ();
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)
{
return Zero;
}
- private static Term foreach_clause (Domain domain, Variable vari,
+ private static Term Fforeach (Domain domain, Variable vari,
Term[] args)
{
- List<Term> list = args[0].Eval (domain).Listval;
+ Term result = args[0].Eval (domain);
+ if (domain.Thrown)
+ return result;
+ List<Term> list = result.Listval;
Bindings current = domain.bindings;
- Term result = Zero;
try {
domain.Catch (Nbreak);
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 ();
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;
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 = "<funcall fname=\"" + func.name;
+ if (args == null)
+ return str + "\"/>";
+ str += "\">";
+ foreach (Term e in args)
+ str += e;
+ return (str + "</funcall>");
+ }
+ }
+
public struct Term
{
public int intval;
public Term (string str) { intval = 0; objval = str; }
public Term (List<Term> 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)
{
}
}
- 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 = "<funcall fname=\"" + func.name;
- if (args == null)
- return str + "\"/>";
- str += "\">";
- foreach (Term e in args)
- str += e;
- return (str + "</funcall>");
- }
- }
-
public int Intval {
get {
if (objval != null)
return this;
if (objval is List<Term>)
return new Term ((List<Term>) 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)
int c = str[0];
int i;
+ if (c == '?')
+ return str[1];
if (c == '0' && str[1] == 'x')
{
i = 0;
foreach (Term term in terms)
{
result = term.Eval (domain);
- if (domain.Catched)
- break;
+ if (domain.Thrown)
+ return result;
}
} finally {
domain.Uncatch ();