- public Lambda (XmlNode node, Domain domain)
- {
- Set (node, domain);
- }
-
- public void SetArgs (MPlist args)
- {
- int len = args == null ? 0 : args.Count;
-
- if (this.args == null)
- this.args = new MSymbol[len];
- for (int i = 0; i < len; i++, args = args.next)
- this.args[i] = args.Symbol;
- }
-
- public void SetBody (MPlist body, Domain domain)
- {
- int len = body == null ? 0 : body.Count;
- if (this.body == null)
- this.body = new MExpression[len];
- for (int i = 0; i < len; i++, body = body.next)
- {
- domain.Translate (body);
- this.body[i] = new MExpression (body.key, body.val, domain);
- }
- }
-
- public void Set (XmlNode node, Domain domain)
- {
- XmlNodeList body = node.ChildNodes;
- int idx = 0;
-
- if (body[0].Name == "args")
- {
- XmlNodeList args = body[0].ChildNodes;
- if (this.args == null)
- this.args = new MSymbol[args.Count];
- for (int i = 0; i < args.Count; i++)
- this.args[i] = args[i].InnerText;
- idx++;
- }
- else if (this.args == null)
- this.args = new MSymbol[0];
- if (this.body == null)
- this.body = new MExpression[body.Count - idx];
- for (int i = 0; idx < body.Count; i++, idx++)
- this.body[i] = new MExpression (body[idx], domain);
- }
- }
-
- public readonly MSymbol Name;
- public Builtin builtin;
- public int min_arg, max_arg;
- internal Lambda lambda;
- public bool specialp = false;
-
- internal static Function ignore, varref, block;
-
- public Function (MSymbol name, Builtin builtin,
- int min_arg, int max_arg, bool specialp)
- {
- Name = name;
- this.builtin = builtin;
- this.min_arg = min_arg;
- this.max_arg = max_arg;
- this.specialp = specialp;
- }
-
- internal Function (MSymbol name, MPlist args, MPlist body,
- Domain domain)
- {
- Name = name;
- lambda = new Lambda (args, body, domain);
- this.min_arg = this.max_arg = lambda.args.Length;
- }
-
- internal Function (MSymbol name, XmlNode node, Domain domain)
- {
- Name = name;
- lambda = new Lambda (node, domain);
- this.min_arg = this.max_arg = lambda.args.Length;
- }
-
- private Function ()
- {
- Name = MSymbol.nil;
- }
-
- static Function ()
- {
- ignore = new Function ();
- varref = new Function (Mvarref, get_value, 1, 1, true);
- block = new Function (Mprogn, progn, 0, -1, true);
- }
-
- private static object get_value (MExpression[] args, Domain domain)
- {
- return domain.GetValue ((MSymbol) args[0].val);
- }
-
- public object Call (MExpression[] args, Domain domain)
- {
- if (builtin != null)
- {
- if (! specialp)
- foreach (MExpression e in args)
- e.Eval (domain);
- return builtin (args, domain);
- }
- if (lambda == null)
- return null;
- MPlist orig_bindings = domain.bindings;
- object result = false;
- try {
- int i = 0;
- foreach (MSymbol arg in lambda.args)
- domain.Bind (arg, args[i++].Eval (domain));
- foreach (MExpression e in lambda.body)
- result = e.Eval (domain);
- } finally {
- domain.bindings = orig_bindings;
- }
- return result;
- }
- }
-
- private static Domain basic;
-
- static MExpression ()