From d5c9c01eef267c6f7d66de3be097f172c70cff11 Mon Sep 17 00:00:00 2001 From: handa Date: Mon, 10 Aug 2009 13:01:58 +0000 Subject: [PATCH] *** empty log message *** --- MExpression.cs | 102 ++++++++++++-------- MInputMethod.cs | 280 ++++++++++++++++++++++++++++++++++++++++++++++++------- MText.cs | 3 +- eval.txt | 10 +- expr.cs | 2 +- 5 files changed, 320 insertions(+), 77 deletions(-) diff --git a/MExpression.cs b/MExpression.cs index 70bebd8..046fdfb 100644 --- a/MExpression.cs +++ b/MExpression.cs @@ -73,7 +73,7 @@ namespace M17N.Core functions = functions.Cons (sym, func); } - internal void Defun (MSymbol sym, MPlist args, MPlist body) + public void Defun (MSymbol sym, MPlist args, MPlist body) { Function func = new Function (sym, args, body, this); @@ -89,6 +89,21 @@ namespace M17N.Core return func; } + public bool CopyFunc (MSymbol name, Domain domain) + { + Function func = (Function) functions.Get (name); + if (func == null) + return false; + domain.functions = domain.functions.Cons (name, func); + return true; + } + + public void CopyFunc (Domain domain) + { + foreach (MPlist p in functions) + domain.functions = domain.functions.Cons (p.key, p.val); + } + public object GetValue (MSymbol name) { MPlist slot = bindings.Find (name); @@ -155,7 +170,10 @@ namespace M17N.Core len = body.Count; this.body = new MExpression[len]; for (int i = 0; ! body.IsEmpty; i++, body = body.next) - this.body[i] = new MExpression (body.key, body.val, domain); + { + domain.Translate (body); + this.body[i] = new MExpression (body.key, body.val, domain); + } } } @@ -192,8 +210,8 @@ namespace M17N.Core static Function () { ignore = new Function (); - varref = new Function (Mvarref, new Evaluator (get_value), 1, 1, true); - block = new Function (Mprogn, new Evaluator (progn), 0, -1, true); + 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) @@ -233,36 +251,36 @@ namespace M17N.Core { basic = new Domain (); - basic.Defun ("set", new Evaluator (set_value), 2, 2, true); - basic.Defun ("=", new Evaluator (set_value), 2, 2, true); - basic.Defun ("+", new Evaluator (plus), 2, -1, false); - basic.Defun ("*", new Evaluator (multi), 2, -1, false); - basic.Defun ("-", new Evaluator (minus), 1, -1, false); - basic.Defun ("/", new Evaluator (divide), 2, -1, false); - basic.Defun ("%", new Evaluator (percent), 2, -1, false); - basic.Defun ("|", new Evaluator (logior), 2, -1, false); - basic.Defun ("&", new Evaluator (logand), 2, -1, false); - basic.Defun ("+=", new Evaluator (pluseq), 2, -1, true); - basic.Defun ("*=", new Evaluator (multieq), 2, -1, true); - basic.Defun ("-=", new Evaluator (minuseq), 2, -1, true); - basic.Defun ("/=", new Evaluator (divideeq), 2, -1, true); - basic.Defun ("%=", new Evaluator (percenteq), 2, -1, true); - basic.Defun ("|=", new Evaluator (logioreq), 2, -1, true); - basic.Defun ("&=", new Evaluator (logandeq), 2, -1, true); - basic.Defun ("<<", new Evaluator (lshift), 2, 2, false); - basic.Defun (">>", new Evaluator (rshift), 2, 2, false); - basic.Defun ("<<=", new Evaluator (lshifteq), 2, 2, true); - basic.Defun (">>=", new Evaluator (rshifteq), 2, 2, true); - basic.Defun ("==", new Evaluator (eq), 2, -1, false); - basic.Defun ("!=", new Evaluator (noteq), 2, 2, false); - basic.Defun ("<", new Evaluator (less), 2, -1, false); - basic.Defun ("<=", new Evaluator (lesseq), 2, -1, false); - basic.Defun (">", new Evaluator (more), 2, -1, false); - basic.Defun (">=", new Evaluator (moreeq), 2, -1, false); - basic.Defun ("progn", new Evaluator (progn), 0, -1, true); - basic.Defun ("if", new Evaluator (ifclause), 2, -1, true); - basic.Defun ("when", new Evaluator (whenclause), 2, -1, true); - basic.Defun ("while", new Evaluator (whileclause), 1, -1, true); + basic.Defun ("set", set_value, 2, 2, true); + basic.Defun ("=", set_value, 2, 2, true); + basic.Defun ("+", plus, 2, -1, false); + basic.Defun ("*", multi, 2, -1, false); + basic.Defun ("-", minus, 1, -1, false); + basic.Defun ("/", divide, 2, -1, false); + basic.Defun ("%", percent, 2, -1, false); + basic.Defun ("|", logior, 2, -1, false); + basic.Defun ("&", logand, 2, -1, false); + basic.Defun ("+=", pluseq, 2, -1, true); + basic.Defun ("*=", multieq, 2, -1, true); + basic.Defun ("-=", minuseq, 2, -1, true); + basic.Defun ("/=", divideeq, 2, -1, true); + basic.Defun ("%=", percenteq, 2, -1, true); + basic.Defun ("|=", logioreq, 2, -1, true); + basic.Defun ("&=", logandeq, 2, -1, true); + basic.Defun ("<<", lshift, 2, 2, false); + basic.Defun (">>", rshift, 2, 2, false); + basic.Defun ("<<=", lshifteq, 2, 2, true); + basic.Defun (">>=", rshifteq, 2, 2, true); + basic.Defun ("==", eq, 2, -1, false); + basic.Defun ("!=", noteq, 2, 2, false); + basic.Defun ("<", less, 2, -1, false); + basic.Defun ("<=", lesseq, 2, -1, false); + basic.Defun (">", more, 2, -1, false); + basic.Defun (">=", moreeq, 2, -1, false); + basic.Defun ("progn", progn, 0, -1, true); + basic.Defun ("if", ifclause, 2, -1, true); + basic.Defun ("when", whenclause, 2, -1, true); + basic.Defun ("while", whileclause, 1, -1, true); basic.AddTranslator (new Translator (translate_cond)); } @@ -592,6 +610,9 @@ namespace M17N.Core private MExpression[] args; private object val; + public MExpression[] Args { get { return args; } } + public object Val { get { return val; } } + private MExpression (object val) { this.val = val; @@ -646,7 +667,10 @@ namespace M17N.Core + sym + " " + nargs); args = new MExpression[nargs]; for (int i = 0; i < nargs; i++, p = p.next) - args[i] = new MExpression (p.key, p.val, domain); + { + domain.Translate (p); + args[i] = new MExpression (p.key, p.val, domain); + } } } else @@ -663,13 +687,13 @@ namespace M17N.Core { function = Function.block; args = new MExpression[plist.Count]; - domain.Translate (plist); for (int i = 0; ! plist.IsEmpty; i++, plist = plist.next) - args[i] = new MExpression (plist.key, plist.val, domain); + { + domain.Translate (plist); + args[i] = new MExpression (plist.key, plist.val, domain); + } } - public object Val { get { return val; } } - public object Eval (Domain domain) { if (function == null) diff --git a/MInputMethod.cs b/MInputMethod.cs index e8a651a..520ec66 100644 --- a/MInputMethod.cs +++ b/MInputMethod.cs @@ -11,6 +11,7 @@ namespace M17N.Input public class MInputMethod { internal static MExpression.Domain domain = new MExpression.Domain (null); + internal MExpression.Domain local_domain; private static MSymbol Minput_method = MSymbol.Of ("input-method"); private static MSymbol Mdescription = MSymbol.Of ("description"); @@ -30,6 +31,17 @@ namespace M17N.Input private static MSymbol Mat_open_square_bracket = MSymbol.Of ("@["); private static MSymbol Mat_close_square_bracket = MSymbol.Of ("@]"); private static MSymbol Minsert = MSymbol.Of ("insert"); + private static MSymbol Mdelete = MSymbol.Of ("delete"); + private static MSymbol Mmove = MSymbol.Of ("move"); + private static MSymbol Mmark = MSymbol.Of ("mark"); + private static MSymbol Mmarker = MSymbol.Of ("marker"); + private static MSymbol Madd = MSymbol.Of ("add"); + private static MSymbol Msub = MSymbol.Of ("sub"); + private static MSymbol Mmul = MSymbol.Of ("mul"); + private static MSymbol Mdiv = MSymbol.Of ("div"); + private static MSymbol Mif = MSymbol.Of ("if"); + private static MSymbol Mcond = MSymbol.Of ("cond"); + private static MSymbol Mchar_at = MSymbol.Of ("char-at"); internal class Variable { @@ -150,22 +162,23 @@ namespace M17N.Input static MInputMethod () { - domain.Defun ("insert", new MExpression.Evaluator (insert), 1, 1); - domain.Defun ("candidates", new MExpression.Evaluator (insert_candidates), - 1, 1); - domain.Defun ("delete", new MExpression.Evaluator (delete), 1, 1); - domain.Defun ("select", new MExpression.Evaluator (select), 1, 1); - domain.Defun ("show", new MExpression.Evaluator (show), 0, 0); - domain.Defun ("hide", new MExpression.Evaluator (hide), 0, 0); - domain.Defun ("move", new MExpression.Evaluator (move), 1, 1); - domain.Defun ("mark", new MExpression.Evaluator (mark), 1, 1); - domain.Defun ("pushback", new MExpression.Evaluator (pushback), 1, 1); - domain.Defun ("pop", new MExpression.Evaluator (pop), 0, 0); - domain.Defun ("undo", new MExpression.Evaluator (undo), 0, 1); - domain.Defun ("commit", new MExpression.Evaluator (commit), 0, 0); - domain.Defun ("unhandle", new MExpression.Evaluator (unhandle), 0, 0); - domain.Defun ("shift", new MExpression.Evaluator (shift), 1, 1); - domain.Defun ("call", new MExpression.Evaluator (call), 2, -1); + domain.Defun ("insert", insert, 1, 1); + domain.Defun ("candidates", insert_candidates, 1, 1); + domain.Defun ("delete", delete, 1, 1); + domain.Defun ("select", select, 1, 1); + domain.Defun ("show", show, 0, 0); + domain.Defun ("hide", hide, 0, 0); + domain.Defun ("move", move, 1, 1); + domain.Defun ("mark", mark, 1, 1); + domain.Defun ("pushback", pushback, 1, 1); + domain.Defun ("pop", pop, 0, 0); + domain.Defun ("undo", undo, 0, 1); + domain.Defun ("commit", commit, 0, 0); + domain.Defun ("unhandle", unhandle, 0, 0); + domain.Defun ("shift", shift, 1, 1); + domain.Defun ("call", call, 2, -1); + domain.Defun ("marker", marker, 1, 1, true); + domain.Defun ("char-at", char_at, 1, 1, true); } private MInputMethod (MDatabase.Tag tag) @@ -177,6 +190,8 @@ namespace M17N.Input language = tag[1]; name = tag[2]; subname = tag[3]; + local_domain = new MExpression.Domain (domain, null); + MPlist plist = (MPlist) mdb.Load (); if (plist == null) return; @@ -215,6 +230,119 @@ namespace M17N.Input } } + private static void transform (MPlist plist) + { + for (; ! plist.IsEmpty; plist = plist.next) + { + if (plist.IsMText) + { + MPlist p = new MPlist (); + p.Add (MSymbol.symbol, Minsert); + p.Add (MSymbol.mtext, plist.Text); + plist.Set (MSymbol.plist, p); + } + else if (plist.IsInteger) + { + MPlist p = new MPlist (); + p.Add (MSymbol.symbol, Minsert); + p.Add (MSymbol.integer, plist.Integer); + plist.Set (MSymbol.plist, p); + } + else if (plist.IsPlist) + { + MPlist pl = plist.Plist; + + if (pl.IsSymbol) + { + if (pl.Symbol == Madd) + pl.Set (MSymbol.symbol, MSymbol.Of ("+=")); + else if (pl.Symbol == Msub) + pl.Set (MSymbol.symbol, MSymbol.Of ("-=")); + else if (pl.Symbol == Mmul) + pl.Set (MSymbol.symbol, MSymbol.Of ("*=")); + else if (pl.Symbol == Mdiv) + pl.Set (MSymbol.symbol, MSymbol.Of ("/=")); + else if (pl.Symbol == Minsert) + { + // (insert (CANDIDATES ...)) + // => (candidates CANDIDATES ...) + if (pl.next.IsPlist) + { + pl.Set (MSymbol.symbol, Mcandidates); + pl = pl.next; + MPlist p = pl.Plist; + pl.Set (p.key, p.val); + for (p = p.next; ! p.IsEmpty; p = p.next); + pl.Add (p.key, p.val); + } + } + else if (pl.Symbol == Mif) + { + pl = pl.next; + if (! pl.IsEmpty) + transform (pl.next); + } + else if (pl.Symbol == Mcond) + { + for (pl = pl.next; ! pl.IsEmpty; pl = pl.next) + if (pl.IsPlist) + { + MPlist p = pl.Plist; + + if (p.IsPlist) + transform (p); + else + transform (p.next); + } + } + else if (pl.Symbol == Mdelete + || pl.Symbol == Mmove + || pl.Symbol == Mmark) + { + pl = pl.next; + if (pl.IsSymbol) + { + MSymbol sym = pl.Symbol; + MPlist p = new MPlist (); + p.Add (MSymbol.symbol, Mmarker); + p.Add (MSymbol.symbol, sym); + pl.Set (MSymbol.plist, p); + } + } + else if (pl.Symbol == Mstate) + { + pl = pl.next; + if (pl.IsSymbol) + { + MSymbol sym = pl.Symbol; + MPlist p = new MPlist (); + p.Add (MSymbol.symbol, Mstate); + p.Add (MSymbol.symbol, sym); + pl.Set (MSymbol.plist, p); + } + } + } + else if (pl.IsMText) + { + // (CANDIDATES ...) => (candidates CANDIDATES ...) + pl.Push (MSymbol.symbol, Mcandidates); + } + } + else if (plist.IsSymbol) + { + MSymbol sym = plist.Symbol; + + if (sym.Name[0] == '@') + { + MPlist p = new MPlist (); + p.Add (MSymbol.symbol, Mchar_at); + p.Add (MSymbol.symbol, sym); + plist.Set (MSymbol.plist, p); + } + } + } + } + private static MText parse_description (MPlist plist) { return (plist.IsMText ? plist.Text @@ -312,9 +440,9 @@ namespace M17N.Input if (target_type == Mmacro) { if (target_name == MSymbol.nil) - im.domain.Copy (local_table); + im.local_domain.CopyFunc (local_domain); else - im.local_table.Copy (target_name, local_table); + im.local_domain.CopyFunc (target_name, local_domain); } else if (target_type == Mmap) { @@ -355,9 +483,8 @@ namespace M17N.Input if (! pl.IsSymbol) continue; - MSymbol name = pl.Symbol; - MExpression expr = new MExpression (pl.next, local_table); - MExpression.Defmacro (local_table, name, expr); + transform (pl.next); + local_domain.Defun (pl.Symbol, new MPlist (), pl.next); } } @@ -391,7 +518,8 @@ namespace M17N.Input p = p.next; if (p.IsEmpty) continue; - MExpression expr = new MExpression (p, local_table); + transform (p); + MExpression expr = new MExpression (p, local_domain); map.Add (keys, 0, expr); } } @@ -428,7 +556,8 @@ namespace M17N.Input continue; MSymbol map_name = p.Symbol; p = p.next; - state.branches[map_name] = new MExpression (p, local_table); + transform (p); + state.branches[map_name] = new MExpression (p, local_domain); } } } @@ -494,7 +623,7 @@ namespace M17N.Input MExpression.Domain domain) { MInputContext ic = (MInputContext) domain.context; - object arg = args[0].val; + object arg = args[0].Val; if (arg is int) preedit_replace (ic, ic.cursor_pos, ic.cursor_pos, (int) arg); @@ -767,7 +896,7 @@ namespace M17N.Input MExpression.Domain domain) { MInputContext ic = (MInputContext) domain.context; - MPlist list = (MPlist) args[0]; + MPlist list = (MPlist) args[0].Val; int column = 0; if (domain.IsBound (Mcandidates_group_size)) @@ -781,11 +910,103 @@ namespace M17N.Input return 1; } + private static object state (MExpression[] args, + MExpression.Domain domain) + { + MInputContext ic = (MInputContext) domain.context; + MSymbol sym = (MSymbol) args[0].Args[0].Val; + + if (sym == MSymbol.t) + return MSymbol.t; + return ic.im.states[sym]; + } + + private static object marker (MExpression[] args, + MExpression.Domain domain) + { + MInputContext ic = (MInputContext) domain.context; + MSymbol sym = (MSymbol) args[0].Args[0].Val; + int pos = ic.cursor_pos; + + if (sym.Name.Length == 2 && sym.Name[0] == '@') + { + switch (sym.Name[0]) + { + case '<': pos = 0; break; + case '>': pos = ic.preedit.Length; break; + case '-': pos = ic.cursor_pos - 1; break; + case '+': pos = ic.cursor_pos + 1; break; + case '[': + if (pos > 0) + { + int to; + ic.preedit.FindProp (Mcandidates, pos - 1, out pos, out to); + } + else + pos = 0; + break; + case ']': + if (ic.cursor_pos < ic.preedit.Length - 1) + { + int from; + ic.preedit.FindProp (Mcandidates, pos, out from, out pos); + } + else + pos = ic.preedit.Length; + break; + default: + if (sym.Name[0] >= '0' && sym.Name[0] <= '9') + pos = sym.Name[0]; + break; + } + } + else if (sym.Name.Length >= 3 && sym.Name[0] == '@') + { + + + } + else + { + object val = ic.markers.Get (sym); + + if (val is int) + pos = (int) val; + } + return pos; + } + + private static object char_at (MExpression[] args, + MExpression.Domain domain) + { + MInputContext ic = (MInputContext) domain.context; + MSymbol sym = (MSymbol) args[0].Args[0].Val; + + if ( + + + return 0; + } + + private static object delete (MExpression[] args, + MExpression.Domain domain) + { + MInputContext ic = (MInputContext) domain.context; + int pos = (int) args[0].Val; + + if (pos < ic.cursor_pos) + preedit_replace (ic, pos, ic.cursor_pos, null); + else + preedit_replace (ic, ic.cursor_pos, pos, null); + ic.preedit_changed = true; + ic.cursor_pos_changed = true; + return true; + } + private static object select (MExpression[] args, MExpression.Domain domain) { MInputContext ic = (MInputContext) domain.context; - object arg = args[0].val; + object arg = args[0].Val; if (ic.candidates == null) return 0; @@ -814,11 +1035,6 @@ namespace M17N.Input return 0; } - private static object delete (MExpression[] args, MExpression.Domain domain) - { - return 1; - } - private static object show (MExpression[] args, MExpression.Domain domain) { return 1; } private static object hide (MExpression[] args, MExpression.Domain domain) @@ -882,7 +1098,7 @@ namespace M17N.Input public MInputContext (MInputMethod im) { this.im = im; - domain = new MExpression (this); + domain = new MExpression.Domain (im.local_domain, this); } internal object GetCandidates (out int column) diff --git a/MText.cs b/MText.cs index 9180c62..a48b556 100644 --- a/MText.cs +++ b/MText.cs @@ -621,7 +621,8 @@ namespace M17N.Core public object FindProp (MSymbol key, int pos, out int from, out int to) { - from = to = pos; + from = 0; + to = Length; check_pos (pos, false); MInterval i = (MInterval) intervals.Get (key); diff --git a/eval.txt b/eval.txt index d1517ed..a224999 100644 --- a/eval.txt +++ b/eval.txt @@ -1,8 +1,9 @@ ;; -*- lisp -*- (defun temp (a b c) - (+= a 1) - (+ a b c)) + (cond ((> a 0) + (+= a 1) + (+ a b c)))) (cond (0 (+ X 2 (* 3 4) (- 3 4)) (set X 4)) @@ -14,6 +15,7 @@ (+= Y "def")) (while (> X 7) (/= X 2) (+= X 1)) - (set Y "ABC") - (set Z (+ Y "def"))) + (cond ((> X 1) + (set Y "ABC") + (set Z (+ Y "def"))))) (insert "kkk") diff --git a/expr.cs b/expr.cs index d73ce59..3ad5f76 100644 --- a/expr.cs +++ b/expr.cs @@ -24,7 +24,7 @@ public class Test { MExpression.Domain domain = new MExpression.Domain (null); - domain.Defun ("insert", new MExpression.Evaluator (insert), 1, 1, false); + domain.Defun ("insert", insert, 1, 1, false); domain.Bind (MSymbol.Of ("X"), 10); domain.Bind (Mpreedit, new MText ("PREEDIT TEXT")); -- 1.7.10.4