本文整理汇总了C#中Env.Lookup方法的典型用法代码示例。如果您正苦于以下问题:C# Env.Lookup方法的具体用法?C# Env.Lookup怎么用?C# Env.Lookup使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Env
的用法示例。
在下文中一共展示了Env.Lookup方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}));
}));
}
}
}
示例2: 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;
}
}
}