3c437477772bc66243f72b87b34778360022d757
[m17n/m17n-lib-cs.git] / MExpression.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using M17N;
6 using M17N.Core;
7
8 namespace M17N.Core
9 {
10   public delegate object MEvaluator (MExpression args);
11
12   public class MFunction
13   {
14     internal readonly MSymbol name;
15     internal readonly MEvaluator evaluator;
16     public static Dictionary<MSymbol, MFunction> CommonTable
17       = new Dictionary<MSymbol, MFunction> ();
18
19     public MFunction (MSymbol name, MEvaluator evaluator)
20     {
21       this.evaluator = evaluator;
22       CommonTable[name] = this;
23     }
24
25     public MFunction (MSymbol name, MEvaluator evaluator,
26                       Dictionary<MSymbol, MFunction> dict)
27     {
28       this.evaluator = evaluator;
29       dict[name] = this;
30     }
31
32     public object Call (MExpression args)
33     {
34       return evaluator (args);
35     }
36
37     private object plus (MExpression args)
38     {
39       int n = 0;
40       foreach (MExpression expr in args)
41         n += expr.Eval (bindings);
42       return n;
43     }
44
45     private object multiply (MExpression args)
46     {
47       int n = 1;
48       foreach (MExpression expr in args)
49         n *= expr.Eval (bindings);
50       return n;
51     }
52
53 #if false
54       if (key == Mminus)
55         {
56           int n = - Plist.Eval (env);
57           foreach (MPlist plist in Plist.next)
58             n -= plist.Eval (env);
59           return n;
60         }
61       if (key == Mslash)
62         {
63           int n = Plist.Eval (env);
64           foreach (MPlist plist in Plist.next)
65             n /= plist.Eval (env);
66           return n;
67         }
68       if (key == Mpercent)
69         {
70           return Plist.Eval (env) % Plist.next.Eval (env);
71         }
72       if (key == Mlogior)
73         {
74           return Plist.Eval (env) | Plist.next.Eval (env);
75         }
76       if (key == Mlogand)
77         {
78           return Plist.Eval (env) & Plist.next.Eval (env);
79         }
80       if (key == Mlshift)
81         {
82           return Plist.Eval (env) << Plist.next.Eval (env);
83         }
84       if (key == Mrshift)
85         {
86           return Plist.Eval (env) >> Plist.next.Eval (env);
87         }
88       if (key == Mset)
89         {
90           MSymbol var = (MSymbol) Plist.val;
91
92         }
93 #endif
94   }
95
96   public class MBindings : MPlist
97   {
98     public MBindings () : base () { }
99
100     public new MBindings Push (MSymbol variable, object value)
101     {
102       base.Push (variable, value);
103     }
104
105     public override string ToString ()
106       {
107         string str = "(";
108         foreach (MBindings b in this)
109           {
110             if (b != this)
111               str += ", ";
112             str += b.key + " = " + b.val;
113           }
114         return str + ")";
115       }
116   }
117
118   public class MExpression : MPlist
119   {
120     public MExpression () : base () { }
121
122     public MExpression Append (MFunction func, MExpression args)
123     {
124       base.Add (op, (MPlist) args);
125       return this;
126     }
127
128     public object Eval ()
129     {
130       
131
132     }
133   }
134 }