当前位置: 首页>>代码示例>>C#>>正文


C# Env.Define方法代码示例

本文整理汇总了C#中Env.Define方法的典型用法代码示例。如果您正苦于以下问题:C# Env.Define方法的具体用法?C# Env.Define怎么用?C# Env.Define使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Env的用法示例。


在下文中一共展示了Env.Define方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetupGlobalEnv

        static Env SetupGlobalEnv()
        {
            Env G = new Env(null, new List<string>(), new List<object>());

            G.Define("true", true);
            G.Define("false", false);
            G.Define("else", true);

            G.DefineScriptProcedure("not", (args, k) => k(!(bool)args[0]));
            G.DefineScriptProcedure("identity", (args, k) => k(args[0]));
            G.DefineScriptProcedure("sqr", (args, k) => {
                if (args[0] is BigInteger) {
                    var a = (BigInteger)args[0]; return k(a * a);
                } else {
                    var a = (decimal)args[0]; return k(a * a);
                }
            });

            G.DefineScriptProcedure("+", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k(castToDecimal(args[0]) + castToDecimal(args[1]));
                return k((BigInteger)args[0] + (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("-", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k(castToDecimal(args[0]) - castToDecimal(args[1]));
                return k((BigInteger)args[0] - (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("*", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k(castToDecimal(args[0]) * castToDecimal(args[1]));
                return k((BigInteger)args[0] * (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("/", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k(castToDecimal(args[0]) / castToDecimal(args[1]));
                return k((BigInteger)args[0] / (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("quotient", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k((BigInteger)(castToDecimal(args[0]) / castToDecimal(args[1])));
                return k((BigInteger)args[0] / (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("remainder", (args, k) => {
                if (args[0] is decimal || args[1] is decimal) return k(castToDecimal(args[0]) % castToDecimal(args[1]));
                return k((BigInteger)args[0] % (BigInteger)args[1]);
            });
            G.DefineScriptProcedure("=", (args, k) => k(args[0].Equals(args[1])));
            G.DefineScriptProcedure("<", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) < 0));
            G.DefineScriptProcedure("<=", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) <= 0));
            G.DefineScriptProcedure(">", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) > 0));
            G.DefineScriptProcedure(">=", (args, k) => k((args[0] as IComparable).CompareTo(args[1]) >= 0));
            G.DefineScriptProcedure("eq?", (args, k) => k(object.ReferenceEquals(args[0], args[1])));

            G.DefineScriptProcedure("cons", (args, k) => k(new Pair() {
                Car = args[0], Cdr = args[1]
            }));
            G.DefineScriptProcedure("car", (args, k) => k(((Pair)args[0]).Car));
            G.DefineScriptProcedure("cdr", (args, k) => k(((Pair)args[0]).Cdr));
            G.DefineScriptProcedure("drop", (args, k) => {
                Pair l = (Pair)args[0]; int n = (int)(BigInteger)args[1];
                for (; n > 0; --n) {
                    l = (Pair)l.Cdr;
                }
                return k(l);
            });
            G.DefineScriptProcedure("length", (args, k) => {
                int n = 0;
                for (Pair l = (Pair)args[0]; l != null; ++n, l = (Pair)l.Cdr) ;
                return k(n);
            });
            G.DefineScriptProcedure("append", (args, k) => {
                var l = PairToList((Pair)args[0]);
                l.InsertRange(l.Count, PairToList((Pair)args[1]));
                return k(ListToPair(l));
            });
            G.Define("empty", null);
            G.DefineScriptProcedure("empty?", (args, k) => k(args[0] == null));

            G.DefineScriptProcedure("pretty-print", (args, k) => {
                PrintPairExp(args[0]);
                return k(null);
            });
            G.DefineScriptProcedure("display", (args, k) => {
                PrintListExp(PairExpToListExp(args[0]));
                return k(null);
            });
            G.DefineScriptProcedure("current-inexact-milliseconds", (args, k) => {
                long now;
                QueryPerformanceCounter(out now);
                return k((decimal)(now - sTimerStart) * 1000 / sTimerFreq);
            });
            G.DefineScriptProcedure("exit", (args, k) => {
                Environment.Exit(0);
                return k(null);
            });
            G.DefineScriptProcedure("random", (args, k) => k((BigInteger)sRandom.Next((int)(BigInteger)args[0])));
            G.DefineScriptProcedure("eval", (args, k) => k(ForceEval(G, PairExpToListExp(args[0]))));
            G.DefineScriptProcedure("call/cc", (args, k) => {
                return ((Procedure)args[0])(new List<object>() {
                    (Procedure)((args2, k2)=> new KeyValuePair<object, Continue>(args2[0], k)),
                }, k);
            });

            return G;
//.........这里部分代码省略.........
开发者ID:GHScan,项目名称:DailyProjects,代码行数:101,代码来源:Program.cs

示例2: Eval

        static KeyValuePair<object, Continue> Eval(Env env, object exp, Continue k)
        {
            if (exp is string) {
                return k(env.Lookup((string)exp));
            } else if (!(exp is List<object>)) {
                return k(exp);
            }

            List<object> l = (List<object>)exp;
            switch (l[0] as string) {
                case "quote":
                    return k(ListExpToPairExp(l[1]));
                case "if":
                    return Eval(env, l[1], (b => {
                        if ((bool)b) return Eval(env, l[2], k);
                        else return Eval(env, l[3], k);
                    }));
                case "lambda":
                    return k(MakeScriptProcedure(env, (List<object>)l[1], 0, l, 2));
                case "begin":
                    return EvalSequentially(env, l, 1, k);
                case "cond":
                    if (l.Count == 2) {
                        return EvalSequentially(env, (List<object>)l[1], 0, k);
                    } else {
                        List<object> caseExps = (List<object>)l[1];
                        List<object> newExp = new List<object>() {
                            "if", caseExps[0],
                            new List<object>(){"begin"}.Concat(caseExps.Skip(1)).ToList(),
                            new List<object>(){"cond"}.Concat(l.Skip(2)).ToList(),
                        };
                        return Eval(env, newExp, k);
                    }
                case "define":
                    if (l[1] is string) {
                        return Eval(env, l[2], (v => {
                            env.Define((string)l[1], v);
                            return k(null);
                        }));
                    } else {
                        env.Define((string)((List<object>)l[1])[0], MakeScriptProcedure(env, (List<object>)l[1], 1, l, 2));
                        return k(null);
                    }
                case "set!":
                    return Eval(env, l[2], (v => {
                        env.Set((string)l[1], v);
                        return k(null);
                    }));
                case "let": {
                        List<object> nameValues = (List<object>)l[1];
                        var names = nameValues.Select(a => ((List<object>)a)[0]);
                        var values = nameValues.Select(a => ((List<object>)a)[1]);
                        List<object> lambda = new List<object> { "lambda", names.ToList() }.Concat(l.Skip(2)).ToList();
                        List<object> newExp = new List<object> { lambda }.Concat(values).ToList();
                        return Eval(env, newExp, k);
                    }
                default: {
                        return Eval(env, l[0], (p => {
                            return EvalList(env, l, 1, l.Skip(1).ToList(), 0, (actuals => {
                                return ((Procedure)p)((List<object>)actuals, k);
                            }));
                        }));
                    }
            }
        }
开发者ID:GHScan,项目名称:DailyProjects,代码行数:65,代码来源:Program.cs

示例3: Eval

        static object Eval(Env env, object exp)
        {
            while (true) {
                if (exp is string) {
                    return env.Lookup((string)exp);
                } else if (!(exp is List<object>)) {
                    return exp;
                }

                List<object> l = (List<object>)exp;
                switch (l[0] as string) {
                    case "quote":
                        return ListExpToPairExp(l[1]);
                    case "if":
                        if ((bool)Eval(env, l[1])) {
                            exp = l[2];
                        } else {
                            exp = l[3];
                        }
                        break;
                    case "lambda":
                        return MakeScriptProcedure(env, (List<object>)l[1], 0, l, 2);
                    case "begin":
                        for (int i = 1; i < l.Count - 1; ++i) Eval(env, l[i]);
                        exp = l[l.Count - 1];
                        break;
                    case "cond":
                        for (int i = 1; i < l.Count; ++i) {
                            List<object> caseExps = (List<object>)l[i];
                            if ((bool)Eval(env, caseExps[0])) {
                                for (int j = 1; j < caseExps.Count - 1; ++j) Eval(env, caseExps[j]);
                                exp = caseExps[caseExps.Count - 1];
                                break;
                            }
                        }
                        break;
                    case "define":
                        if (l[1] is string) {
                            env.Define((string)l[1], Eval(env, l[2]));
                        } else {
                            env.Define((string)((List<object>)l[1])[0], MakeScriptProcedure(env, (List<object>)l[1], 1, l, 2));
                        }
                        return null;
                    case "set!":
                        env.Set((string)l[1], Eval(env, l[2]));
                        return null;
                    case "let": {
                            List<object> nameValues = (List<object>)l[1];
                            var names = nameValues.Select(a => ((List<object>)a)[0]);
                            var values = nameValues.Select(a => ((List<object>)a)[1]);
                            List<object> lambda = new List<object> { "lambda", names.ToList() }.Concat(l.Skip(2)).ToList();
                            List<object> newExp = new List<object> { lambda }.Concat(values).ToList();
                            exp = newExp;
                        }
                        break;
                    default: {
                            object p = Eval(env, l[0]);
                            List<object> actuals = l.Skip(1).Select(e => Eval(env, e)).ToList();
                            if (p is Procedure) {
                                return ((Procedure)p)(actuals);
                            } else {
                                Tuple<Env, List<string>, List<object>> lambda = (Tuple<Env, List<string>, List<object>>)p;
                                env = new Env(lambda.Item1, lambda.Item2, actuals);
                                for (int i = 0; i < lambda.Item3.Count - 1; ++i) Eval(env, lambda.Item3[i]);
                                exp = lambda.Item3[lambda.Item3.Count - 1];
                            }
                        }
                        break;
                }
            }
        }
开发者ID:GHScan,项目名称:DailyProjects,代码行数:71,代码来源:Program.cs


注:本文中的Env.Define方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。