}
public void DefSubr (Builtin builtin, string str,
- int min_arg, int max_arg)
+ int min_arg, int max_arg, params string[] aliases)
{
Name name = str;
- functions[name]
- = new Function.Subroutine (builtin, name, min_arg, max_arg);
+ Function func = new Function.Subroutine (builtin, name,
+ min_arg, max_arg);
+ functions[name] = func;
+ foreach (string a in aliases)
+ functions[(Name) a] = func;
}
public void DefSpecial (Builtin builtin, string str,
- int min_arg, int max_arg)
+ int min_arg, int max_arg,
+ params string[] aliases)
{
Name name = str;
- functions[name]
- = new Function.SpecialForm (builtin, name, min_arg, max_arg);
+ Function func = new Function.SpecialForm (builtin, name,
+ min_arg, max_arg);
+ functions[name] = func;
+ foreach (string a in aliases)
+ functions[(Name) a] = func;
+ }
+
+ public void DefAlias (string alias, string str)
+ {
+ functions[(Name) alias] = functions[(Name) str];
}
internal Function.Lambda RegisterFunction (XmlNode node)
static Xexpression ()
{
- basic.DefSubr (set_value, "set", 2, 2);
- basic.DefSubr (set_value, "=", 2, 2);
- basic.DefSpecial (and, "and", 1, -1);
- basic.DefSpecial (and, "&&", 1, -1);
- basic.DefSpecial (or, "or", 1, -1);
- basic.DefSpecial (or, "||", 1, -1);
- basic.DefSubr (not, "not", 1, 1);
- basic.DefSubr (not, "!", 1, 1);
- basic.DefSubr (add, "add", 2, -1);
- basic.DefSubr (add, "+", 2, -1);
- basic.DefSubr (mul, "mul", 2, -1);
- basic.DefSubr (mul, "*", 2, -1);
- basic.DefSubr (sub, "sub", 1, -1);
- basic.DefSubr (sub, "-", 1, -1);
- basic.DefSubr (div, "div", 2, -1);
- basic.DefSubr (div, "/", 2, -1);
- basic.DefSubr (mod, "mod", 2, 2);
- basic.DefSubr (mod, "%", 2, 2);
- basic.DefSubr (logior, "logior", 2, -1);
- basic.DefSubr (logior, "|", 2, -1);
- basic.DefSubr (logand, "logand", 2, -1);
- basic.DefSubr (logand, "&", 2, -1);
- basic.DefSubr (add_set, "add-set", 2, -1);
- basic.DefSubr (add_set, "+=", 2, -1);
- basic.DefSubr (mul_set, "mul-set", 2, -1);
- basic.DefSubr (mul_set, "*=", 2, -1);
- basic.DefSubr (sub_set, "sub-set", 2, -1);
- basic.DefSubr (sub_set, "-=", 2, -1);
- basic.DefSubr (div_set, "div-set", 2, -1);
- basic.DefSubr (div_set, "/=", 2, -1);
- basic.DefSubr (mod_set, "mod-set", 2, 2);
- basic.DefSubr (mod_set, "%=", 2, 2);
- basic.DefSubr (logior_set, "logior-set", 2, -1);
- basic.DefSubr (logior_set, "|=", 2, -1);
- basic.DefSubr (logand_set, "logand-set", 2, -1);
- basic.DefSubr (logand_set, "&=", 2, -1);
- basic.DefSubr (lsh, "lsh", 2, 2);
- basic.DefSubr (lsh, "<<", 2, 2);
- basic.DefSubr (rsh, "rsh", 2, 2);
- basic.DefSubr (rsh, ">>", 2, 2);
- basic.DefSubr (lsh_set, "lsh-set", 2, 2);
- basic.DefSubr (lsh_set, "<<=", 2, 2);
- basic.DefSubr (rsh_set, "rsh-set", 2, 2);
- basic.DefSubr (rsh_set, ">>=", 2, 2);
- basic.DefSubr (eq, "eq", 2, -1);
- basic.DefSubr (eq, "==", 2, -1);
- basic.DefSubr (noteq, "noteq", 2, 2);
- basic.DefSubr (noteq, "!=", 2, 2);
- basic.DefSubr (less_than, "lt", 2, -1);
- basic.DefSubr (less_than, "<", 2, -1);
- basic.DefSubr (less_eq, "le", 2, -1);
- basic.DefSubr (less_eq, "<=", 2, -1);
- basic.DefSubr (greater_than, "gt", 2, -1);
- basic.DefSubr (greater_than, ">", 2, -1);
- basic.DefSubr (greater_eq, "ge", 2, -1);
- basic.DefSubr (greater_eq, ">=", 2, -1);
+ basic.DefSubr (set_value, "set", 2, 2, "=");
+ basic.DefSpecial (and, "and", 1, -1, "&&");
+ basic.DefSpecial (or, "or", 1, -1, "||");
+ basic.DefSubr (not, "not", 1, 1, "!");
+ basic.DefSubr (add, "add", 2, -1, "+");
+ basic.DefSubr (mul, "mul", 2, -1, "*");
+ basic.DefSubr (sub, "sub", 1, -1, "-");
+ basic.DefSubr (div, "div", 2, -1, "/");
+ basic.DefSubr (mod, "mod", 2, 2, "%");
+ basic.DefSubr (logior, "logior", 2, -1, "|");
+ basic.DefSubr (logand, "logand", 2, -1, "&");
+ basic.DefSubr (add_set, "add-set", 2, -1, "+=");
+ basic.DefSubr (mul_set, "mul-set", 2, -1, "*=");
+ basic.DefSubr (sub_set, "sub-set", 2, -1, "-=");
+ basic.DefSubr (div_set, "div-set", 2, -1, "/=");
+ basic.DefSubr (mod_set, "mod-set", 2, 2, "%=");
+ basic.DefSubr (logior_set, "logior-set", 2, -1, "|=");
+ basic.DefSubr (logand_set, "logand-set", 2, -1, "&=");
+ basic.DefSubr (lsh, "lsh", 2, 2, "<<");
+ basic.DefSubr (rsh, "rsh", 2, 2, ">>");
+ basic.DefSubr (lsh_set, "lsh-set", 2, 2, "<<=");
+ basic.DefSubr (rsh_set, "rsh-set", 2, 2, ">>=");
+ basic.DefSubr (eq, "eq", 2, -1, "==");
+ basic.DefSubr (noteq, "noteq", 2, 2, "!=");
+ basic.DefSubr (less_than, "lt", 2, -1, "<");
+ 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", 1, -1);
- basic.DefSpecial (quote_clause, "quote", 1, 1);
+ basic.DefSubr (append, "append", 0, -1);
+ basic.DefSubr (concat, "concat", 0, -1);
+ basic.DefSubr (nth, "nth", 2, 2);
basic.DefSubr (eval_clause, "eval", 1, 1);
- basic.DefSpecial (progn_clause, "progn", 0, -1);
- basic.DefSpecial (progn_clause, "expr", 0, -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 (when_clause, "when", 1, -1);
basic.DefSpecial (while_clause, "while", 1, -1);
private static Term append (Domain domain, Term[] args)
{
- object obj = args[0].objval;
- if (obj is string)
+ List<Term> list = new List<Term> ();
+ Term result;
+
+ foreach (Term arg in args)
{
- string str = "";
- foreach (Term arg in args)
- str += arg;
- return new Term (str);
+ if (arg.IsList)
+ list.AddRange ((List<Term>) arg.objval);
+ else
+ list.Add (arg);
}
- else if (obj is List<Term>)
+ result.intval = 0;
+ result.objval = list;
+ return result;
+ }
+
+ private static Term concat (Domain domain, Term[] args)
+ {
+ string str = "";
+ Term result;
+
+ foreach (Term arg in args)
{
- List<Term> list = new List<Term> ();
- foreach (Term arg in args)
- list.AddRange ((List<Term>) arg.objval);
- return new Term (list);
+ if (arg.IsStr)
+ str += (string) arg.objval;
+ else if (arg.IsList)
+ foreach (Term term in (List<Term>) arg.objval)
+ str += (char) term.Intval;
+ else
+ str += (char) arg.Intval;
+ }
+ result.intval = 0;
+ result.objval = str;
+ return result;
+ }
+
+ private static Term nth (Domain domain, 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.intval = 0;
+ result.objval = ((List<Term>) args[1].objval)[args[0].Intval];
}
- throw new Exception ("Invalid term to append: " + obj);
+ else
+ throw new Exception ("Term is not enumelable: " + args[1]);
+ return result;
}
private static Term quote_clause (Domain domain, Term[] args)
}
}
+ public int Intval {
+ get {
+ if (objval != null)
+ throw new Exception ("term is not integer: " + this);
+ return intval;
+ }
+ }
+
public bool IsTrue {
get {
return (objval == null
: true);
}
}
+ public bool IsInt { get { return (objval == null); } }
+ public bool IsStr { get { return (objval is string); } }
+ public bool IsList { get { return (objval is List<Term>); } }
public Term Eval (Domain domain)
{
if (n.Name == Ndefun)
domain.Defun (n);
else if (n.Name != Ndefvar)
- {
- terms[i]= new Term (n, domain);
- Console.WriteLine ("term="+terms[i]);
- }
+ terms[i++]= new Term (n, domain);
}
}
Term result = Zero;
domain.depth = 0;
- for (int i = 0; i < terms.Length; i++)
- Console.WriteLine (terms[i]);
-
foreach (Term term in terms)
- {
- Console.WriteLine (term);
- result = term.Eval (domain);
- Console.WriteLine ("=>" + result);
- }
+ result = term.Eval (domain);
return result;
}
+
+ public override string ToString ()
+ {
+ string str = "";
+ for (int i = 0; i < terms.Length; i++)
+ str += terms[i];
+ return str;
+ }
}
}