return c | (uint) modifiers;
}
- public Key (uint c)
- {
- key = c;
- }
+ public Key (uint c) { key = c; }
+ public Key (int c) { key = (uint) c; }
public Key (uint c, KeyModifier modifiers)
{
return 0;
}
- public KeySeq (XmlNode node)
+ public KeySeq (Xex.Term arg)
{
- XmlAttributeCollection acol = node.Attributes;
- XmlNode n;
-
- if (acol != null && (n = acol["keys"]) != null)
+ if (arg.IsStr)
{
- foreach (char c in n.Value)
- keyseq.Add (new Key ((uint) c));
- }
+ string str = arg.Strval;
- for (node = node.FirstChild; node != null; node = node.NextSibling)
+ for (int i = 0; i < str.Length; i++)
+ keyseq.Add (new Key (str[i]));
+ }
+ else if (arg.IsList)
{
- if (node.Name == "key-event")
- keyseq.Add (new Key ((MSymbol) node.InnerText));
- else
- keyseq.Add (new Key (parse_integer (node.InnerText)));
+ List<Xex.Term> list = arg.Listval;
+ int len = list.Count;
+
+ for (int i = 0; i < len; i++)
+ {
+ if (list[i].IsInt)
+ keyseq.Add (new Key (list[i].Intval));
+ else if (list[i].IsStr)
+ keyseq.Add (new Key (list[i].Strval));
+ else if (list[i].IsSymbol)
+ keyseq.Add (new Key ((string) list[i].Symval));
+ else
+ throw new Exception ("Invalid key: " + list[i]);
+ }
}
}
- public KeySeq (Xex.Term[] args)
+ public static Xex.TermValue New (Xex.Domain domain, XmlNode node)
{
- foreach (Xex.Term term in args)
- {
- if (term.IsSymbol)
- keyseq.Add (new Key ((MSymbol) ((string) term.Symval)));
- else
- keyseq.Add (new Key (term.Strval));
- }
+ return new KeySeq (new Xex.Term (domain, node.FirstChild));
}
public override string ToString ()
if (node.Name == "description")
description = parse_description (node);
else if (node.Name == "keyseq")
- keys.Add (new KeySeq (node));
+ {
+ KeySeq ks = MInputMethod.KeySeq.New (domain, node);
+ keys.Add (ks);
+ }
}
}
static MInputMethod ()
{
+ im_domain.DefType ("keyseq", KeySeq.New);
+
im_domain.DefSubr (Finsert, "insert", true, 1, 1);
im_domain.DefSubr (Finsert_candidates, "candidates", true, 1, -1);
im_domain.DefSubr (Fdelete, "delete", true, 1, 1);
im_domain.DefSubr (Fshift, "shift", true, 1, 1);
im_domain.DefSubr (Fmarker, "marker", true, 1, 1);
im_domain.DefSubr (Fchar_at, "char-at", true, 1, 1);
- im_domain.DefSubr (Fkeyseq, "keyseq", true, 1, -1);
MDatabase.Tag tag = new MDatabase.Tag (Minput_method, "*", "*", "*");
List<MDatabase> list = MDatabase.List (tag);
return args[0];
}
- private static Xex.Term Fkeyseq (Xex.Domain domain, Xex.Variable vari,
- Xex.Term[] args)
- {
- return new Xex.Term (new KeySeq (args));
- }
-
private static Xex.Term Fpushback (Xex.Domain domain, Xex.Variable vari,
Xex.Term[] args)
{
private static Symbol Nexpr = "expr";
+ private static Symbol Nnull = "";
private static Symbol Nfuncall = "funcall";
private static Symbol Nvarref = "varref";
private static Symbol Ninteger = "integer";
{
body = new Term[nterms];
for (nterms = 0; node != null; node = node.NextSibling, nterms++)
- body[nterms] = new Term (node, domain);
+ body[nterms] = new Term (domain, node);
}
}
public object context;
public int depth = 0;
+ internal Dictionary<Symbol, TermType> termtypes
+ = new Dictionary<Symbol, TermType> ();
internal Dictionary<Symbol, Function> functions
= new Dictionary<Symbol, Function> ();
internal Dictionary<Symbol, Variable> variables
}
}
+ public void DefType (Symbol name, NewObject new_object)
+ {
+ termtypes[name] = new TermType (name, new_object);
+ }
+
public void DefSubr (Builtin builtin, string str, bool setvar,
int min_arg, int max_arg, params string[] aliases)
{
desc = null;
if (node != null)
{
- XmlNode nn;
- if (node.Name == "value")
- nn = node.FirstChild;
- else
- nn = node;
- Symbol type = nn.Name;
+ Symbol type = node.Name;
XmlNodeList range_list = null;
int nranges = 0;
- string val = nn.InnerText;
+ string val = node.InnerText;
node = node.NextSibling;
if (node != null)
static Xexpression ()
{
+ basic.DefType ("funcall", Funcall.New);
+
basic.DefSubr (Fset, "set", true, 1, 1, "=");
basic.DefSubr (Fnot, "not", false, 1, 1, "!");
basic.DefSubr (Fadd, "add", true, 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");
return (args.Length == 1 ? Zero : args[1]);
}
+ public delegate TermValue NewObject (Domain domain, XmlNode node);
+
+ public class TermType
+ {
+ public readonly Symbol type;
+ internal readonly NewObject New;
+
+ public TermType (Symbol type, NewObject new_object)
+ {
+ this.type = type;
+ New = new_object;
+ }
+ }
+
public abstract class TermValue
{
public virtual Term Eval (Domain domain) { return new Term (this); }
this.args = args;
}
- public Funcall (XmlNode node, Domain domain)
- {
- vari = null;
- Symbol name = node.Name;
-
- if (name == Nfuncall)
- name = node.Attributes[0].Value;
- else if (node.Attributes[Nvname] != null)
- vari = domain.GetVar (node.Attributes[Nvname].Value, true);
- func = domain.GetFunc (name);
- XmlNodeList nlist = node.ChildNodes;
- int nargs = nlist.Count;
-
- if (nargs < func.min_arg
- || (func.max_arg >= 0 && nargs > func.max_arg))
- throw new Exception ("Invalid number of arguments to: "
- + name + " " + nargs);
- args = new Term[nargs];
- for (int i = 0; i < nlist.Count; i++)
- args[i] = new Term (nlist[i], domain);
- }
-
public Funcall (Domain domain, Symbol fname, Term[] args)
{
func = domain.GetFunc (fname);
public Funcall (Domain domain, Symbol fname, Symbol vname, Term[] args)
{
func = domain.GetFunc (fname);
- vari = domain.GetVar (vname, true);
+ int nargs = args.Length;
+ if (nargs < func.min_arg
+ || (func.max_arg >= 0 && nargs > func.max_arg))
+ throw new Exception ("Invalid number of arguments to: "
+ + fname + " " + nargs);
this.args = args;
+ if (vname != Nnull)
+ vari = domain.GetVar (vname, true);
+ }
+
+ internal static TermValue New (Domain domain, XmlNode node)
+ {
+ Symbol fname = node.Name;
+ Symbol vname = Nnull;
+ XmlAttribute attr;
+
+ if (fname == Nfuncall)
+ fname = node.Attributes[Nfname].Value;
+ attr = node.Attributes[Nvname];
+ if (attr != null)
+ vname = attr.Value;
+
+ XmlNodeList nlist = node.ChildNodes;
+ int nargs = nlist.Count;
+ Term[] args = new Term[nargs];
+ for (int i = 0; i < nargs; i++)
+ args[i] = new Term (domain, nlist[i]);
+ return new Funcall (domain, fname, vname, args);
}
public override Term Eval (Domain domain)
public Term (Term term) { intval = term.intval; objval = term.objval; }
public Term (TermValue obj) { intval = 0; objval = obj; }
- public Term (XmlNode node, Domain domain)
+ public Term (Domain domain, XmlNode node)
{
Symbol name = node.Name;
List<Term> list = new List<Term> ();
for (node = node.FirstChild; node != null;
node = node.NextSibling)
- list.Add (new Term (node, domain));
+ list.Add (new Term (domain, node));
objval = list;
}
else
- objval = new Funcall (node, domain);
+ {
+ TermType term_type;
+
+ if (domain.termtypes.TryGetValue (name, out term_type))
+ objval = term_type.New (domain, node);
+ else
+ objval = Funcall.New (domain, node);
+ }
}
}
if (n.Name == Ndefun)
domain.Defun (n, false);
else if (n.Name != Ndefvar)
- terms[i++]= new Term (n, domain);
+ terms[i++]= new Term (domain, n);
}
}