--- /dev/null
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using M17N;
+using M17N.Core;
+
+namespace M17N.Core
+{
+ public delegate object MEvaluator (MExpression args);
+
+ public class MFunction
+ {
+ internal readonly MSymbol name;
+ internal readonly MEvaluator evaluator;
+ public static Dictionary<MSymbol, MFunction> CommonTable
+ = new Dictionary<MSymbol, MFunction> ();
+
+ public MFunction (MSymbol name, MEvaluator evaluator)
+ {
+ this.evaluator = evaluator;
+ CommonTable[name] = this;
+ }
+
+ public MFunction (MSymbol name, MEvaluator evaluator,
+ Dictionary<MSymbol, MFunction> dict)
+ {
+ this.evaluator = evaluator;
+ dict[name] = this;
+ }
+
+ public object Call (MExpression args)
+ {
+ return evaluator (args);
+ }
+
+ private object plus (MExpression args)
+ {
+ int n = 0;
+ foreach (MExpression expr in args)
+ n += expr.Eval (bindings);
+ return n;
+ }
+
+ private object multiply (MExpression args)
+ {
+ int n = 1;
+ foreach (MExpression expr in args)
+ n *= expr.Eval (bindings);
+ return n;
+ }
+
+#if false
+ if (key == Mminus)
+ {
+ int n = - Plist.Eval (env);
+ foreach (MPlist plist in Plist.next)
+ n -= plist.Eval (env);
+ return n;
+ }
+ if (key == Mslash)
+ {
+ int n = Plist.Eval (env);
+ foreach (MPlist plist in Plist.next)
+ n /= plist.Eval (env);
+ return n;
+ }
+ if (key == Mpercent)
+ {
+ return Plist.Eval (env) % Plist.next.Eval (env);
+ }
+ if (key == Mlogior)
+ {
+ return Plist.Eval (env) | Plist.next.Eval (env);
+ }
+ if (key == Mlogand)
+ {
+ return Plist.Eval (env) & Plist.next.Eval (env);
+ }
+ if (key == Mlshift)
+ {
+ return Plist.Eval (env) << Plist.next.Eval (env);
+ }
+ if (key == Mrshift)
+ {
+ return Plist.Eval (env) >> Plist.next.Eval (env);
+ }
+ if (key == Mset)
+ {
+ MSymbol var = (MSymbol) Plist.val;
+
+ }
+#endif
+ }
+
+ public class MBindings : MPlist
+ {
+ public MBindings () : base () { }
+
+ public new MBindings Push (MSymbol variable, object value)
+ {
+ base.Push (variable, value);
+ }
+
+ public override string ToString ()
+ {
+ string str = "(";
+ foreach (MBindings b in this)
+ {
+ if (b != this)
+ str += ", ";
+ str += b.key + " = " + b.val;
+ }
+ return str + ")";
+ }
+ }
+
+ public class MExpression : MPlist
+ {
+ public MExpression () : base () { }
+
+ public MExpression Append (MFunction func, MExpression args)
+ {
+ base.Add (op, (MPlist) args);
+ return this;
+ }
+
+ public object Eval ()
+ {
+
+
+ }
+ }
+}
\ No newline at end of file
using System;
using System.Collections;
+using System.Collections.Generic;
using System.IO;
using M17N;
using M17N.Core;
{
public class MPlist : IEnumerable
{
- public MSymbol Key;
- public object Val;
+ public MSymbol key;
+ public object val;
private MPlist next;
+ public MSymbol Key { get { return key; } }
+ public object Val { get { return val; } }
+ public MPlist Next { get { return next; } }
+
+ private static List<MSymbol> wf_keys;
+
+ static MPlist ()
+ {
+ wf_keys = new List<MSymbol> ();
+ wf_keys.Add (MSymbol.symbol);
+ wf_keys.Add (MSymbol.mtext);
+ wf_keys.Add (MSymbol.plist);
+ wf_keys.Add (MSymbol.integer);
+ }
+
public MPlist ()
{
- Key = MSymbol.nil;
- Val = null;
+ key = MSymbol.nil;
+ val = null;
}
public MPlist (FileStream stream)
{
MStreamReader reader = new MStreamReader (stream);
- MSymbol key;
- object val;
bool result = reader.ReadElement (out key, out val);
- Key = key;
- Val = val;
if (result)
next = new MPlist (reader);
}
public MPlist (FileStream stream, int count)
{
MStreamReader reader = new MStreamReader (stream);
- MSymbol key;
- object val;
bool result = reader.ReadElement (out key, out val);
- Key = key;
- Val = val;
if (result && --count > 0)
next = new MPlist (reader, count);
else
public MPlist (FileStream stream, MSymbol target, MSymbol stop)
{
MStreamReader reader = new MStreamReader (stream);
- MSymbol key;
- object val;
bool result;
- Key = MSymbol.nil;
- Val = null;
+ key = MSymbol.nil;
+ val = null;
while (true)
{
- result = reader.ReadElement (out key, out val);
+ MSymbol this_key;
+ object this_val;
+
+ result = reader.ReadElement (out this_key, out this_val);
if (! result)
return;
- if (key == MSymbol.plist)
+ if (this_key == MSymbol.plist)
{
- MPlist plist = (MPlist) val;
+ MPlist plist = (MPlist) this_val;
if (plist.IsSymbol)
{
return;
if (plist.Symbol == target)
{
- Key = target;
- Val = val;
+ key = target;
+ val = this_val;
next = new MPlist ();
return;
}
internal MPlist (MStreamReader reader)
{
- MSymbol key;
- object val;
bool result = reader.ReadElement (out key, out val);
- Key = key;
- Val = val;
if (result)
next = new MPlist (reader);
}
private MPlist (MStreamReader reader, int count)
{
- MSymbol key;
- object val;
bool result = reader.ReadElement (out key, out val);
- Key = key;
- Val = val;
if (result && --count > 0)
next = new MPlist (reader, count);
else
private MPlist (MStreamReader reader, MSymbol target, MSymbol stop)
{
- MSymbol key;
- object val;
bool result;
- Key = MSymbol.nil;
- Val = null;
+ key = MSymbol.nil;
+ val = null;
while (true)
{
- result = reader.ReadElement (out key, out val);
+ MSymbol this_key;
+ object this_val;
+
+ result = reader.ReadElement (out this_key, out this_val);
if (! result)
return;
- if (key == MSymbol.plist)
+ if (this_key == MSymbol.plist)
{
- MPlist plist = (MPlist) val;
+ MPlist plist = (MPlist) this_val;
if (plist.IsSymbol)
{
return;
if (plist.Symbol == target)
{
- Key = target;
- Val = val;
+ key = target;
+ val = this_val;
next = new MPlist ();
return;
}
}
}
- private MPlist (MSymbol key, object val)
+ protected MPlist (MSymbol key, object val)
{
- Key = key;
- Val = val;
+ this.key = key;
+ this.val = val;
}
public bool IsEmpty { get { return next == null; } }
- public MPlist Next { get { return next; } }
internal bool IsSymbol { get { return Key == MSymbol.symbol; } }
internal bool IsMText { get { return Key == MSymbol.mtext; } }
internal bool IsPlist { get { return Key == MSymbol.plist; } }
internal bool IsInteger { get { return Key == MSymbol.integer; } }
- internal MSymbol Symbol { get { return (MSymbol) Val; } }
- internal MText Text { get { return (MText) Val; } }
- internal MPlist Plist { get { return (MPlist) Val; } }
- internal int Integer { get { return (int) Val; } }
+ internal MSymbol Symbol { get { return (MSymbol) val; } }
+ internal MText Text { get { return (MText) val; } }
+ internal MPlist Plist { get { return (MPlist) val; } }
+ internal int Integer { get { return (int) val; } }
public int Count
{
MPlist plist = new MPlist (), pl = plist;
for (MPlist p = this; p.next != null; p = p.next)
- pl = pl.Add (p.Key, p.Val);
+ pl = pl.Add (p.key, p.val);
return plist;
}
{
if (p != this)
str += " ";
- if (p.Key != MSymbol.symbol
- && p.Key != MSymbol.integer
- && p.Key != MSymbol.plist
- && p.Key != MSymbol.mtext)
- str += p.Key + ":";
- if (p.Key == MSymbol.mtext)
- str += "\"" + p.Val + "\"";
+ if (! wf_keys.Contains (p.key))
+ str += p.key + ":";
+ if (p.key == MSymbol.mtext)
+ str += "\"" + p.val + "\"";
else
- str += p.Val;
+ str += p.val;
}
return str + ")";
}
MPlist p;
for (p = this; ! p.IsEmpty; p = p.next)
- if (p.Key == key)
+ if (p.key == key)
break;
return p;
}
public object Get (MSymbol key)
{
- return find (key).Val;
+ return find (key).val;
}
internal MPlist Assq (MSymbol key)
if (IsEmpty)
Push (key, val);
else
- Val = val;
+ this.val = val;
return this;
}
public MPlist Push (MSymbol key, object val)
{
- MPlist p = new MPlist (Key, Val);
+ MPlist p = new MPlist (this.key, this.val);
p.next = this.next;
- Key = key;
- Val = val;
+ this.key = key;
+ this.val = val;
next = p;
return this;
}
public object Pop (out MSymbol key)
{
- key = Key;
+ key = this.key;
if (IsEmpty)
return null;
- object val = Val;
+ object this_val = val;
- Key = next.Key;
- Val = next.Val;
+ this.key = next.key;
+ this.val = next.val;
next = next.next;
- return val;
+ return this_val;
}
public object Pop ()
{
- MSymbol key;
- return Pop (out key);
+ MSymbol temp;
+ return Pop (out temp);
}
public MPlist Add (MSymbol key, object val)
public MPlist Clear ()
{
- Key = MSymbol.nil;
- Val = null;
+ key = MSymbol.nil;
+ val = null;
next = null;
return this;
}