for (int i = 0; i < args.Length; i++)
{
args[i] = args[i].Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return args[i];
}
}
{
Bindings current = domain.bindings;
Term result = Zero;
+ int rest_idx = max_idx < 0 ? - max_idx : args.Length;
- try {
- for (int i = 0; i < min_arg; i++)
- domain.Bind (this.args[i], args[i]);
- if (body != null)
- {
- try {
- domain.Catch (CatchTag.Return);
- foreach (Term term in body)
- {
- result = term.Eval (domain);
- if (domain.Thrown)
- return result;
- }
- } finally {
- domain.Uncatch ();
+ for (int i = 0; i < rest_idx; i++)
+ domain.Bind (this.args[i], args[i]);
+ if (i < args.Length)
+ {
+ List<Term> rest = new List<Term> ();
+
+ for (; i < args.Length; i++)
+ rest.Add (args[i]);
+ domain.Bind (this.args[rest_idx], rest);
+ }
+
+ if (body != null)
+ {
+ domain.Catch (CatchTag.Return);
+ foreach (Term term in body)
+ {
+ result = term.Eval (domain);
+ if (domain.Thrown ())
+ break;
}
- }
- } finally {
- domain.UnboundTo (current);
- }
+ domain.Uncatch ();
+ }
+ domain.UnboundTo (current);
return result;
}
}
foreach (Term term in body)
{
result = term.Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
}
} finally {
catch_count--;
}
- public bool Thrown { get { return catch_count < catch_stack.Count; } }
+ public bool Thrown () { return catch_count < catch_stack.Count; }
+
+ public bool Thrown (out bool caught)
+ {
+ if (catch_count < catch_stack.Count)
+ {
+ caught = catch_count == catch_stack.Count - 1;
+ return true;
+ }
+ caught = false;
+ return false;
+ }
internal void ThrowReturn ()
{
Term result = Zero;
try {
domain.Catch (CatchTag.Break);
- while (! domain.Thrown)
+ while (! domain.Thrown ())
foreach (Term arg in args)
{
result = arg.Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
}
} finally {
Term result = Zero;
try {
domain.Catch (CatchTag.Break);
- while (! domain.Thrown && 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.Thrown)
+ if (domain.Thrown ())
return result;
}
} finally {
domain.depth++;
result = list[i].Eval (domain);
domain.depth--;
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
}
return result;
private static Term Fforeach (Domain domain, Variable vari, Term[] args)
{
Term result = args[0].Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
List<Term> list = result.Listval;
Bindings current = domain.bindings;
for (int i = 1; i < args.Length; i++)
{
result = args[i].Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
}
} finally {
public static Term Fcatch (Domain domain, Variable vari, Term[] args)
{
Term result = Zero;
+ bool caught;
- try {
- domain.Catch (new CatchTag (args[0].Symval));
- for (int i = 1; i < args.Length; i++)
- {
- result = args[i].Eval (domain);
- if (domain.Thrown)
+ domain.Catch (new CatchTag (args[0].Symval));
+ for (int i = 1; i < args.Length; i++)
+ {
+ result = args[i].Eval (domain);
+ if (domain.Thrown (out caught))
+ {
+ domain.Uncatch ();
+ if (caught)
+ {
+ if (vari != null)
+ vari.Value = result;
+ return One;
+ }
return result;
- }
- } finally {
- domain.Uncatch ();
- }
- return result;
+ }
+ }
+ domain.Uncatch ();
+ if (vari != null)
+ vari.Value = result;
+ return Zero;
}
public static Term Fthrow (Domain domain, Variable vari, Term[] args)
foreach (Term term in terms)
{
result = term.Eval (domain);
- if (domain.Thrown)
+ if (domain.Thrown ())
return result;
}
} finally {