本文整理汇总了C#中SymbolTable.Define方法的典型用法代码示例。如果您正苦于以下问题:C# SymbolTable.Define方法的具体用法?C# SymbolTable.Define怎么用?C# SymbolTable.Define使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.Define方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
static IASTNode Compile(SymbolTable symTable, object exp)
{
if (exp is string) {
string name = (string)exp;
if (symTable != null && symTable.Lookup(name) != -1) {
return new ASTNode_GetLocalVar(){Index=symTable.Lookup(name)};
}
int envIndex = 0;
for (; symTable != null && symTable.Lookup(name) == -1; symTable = symTable.PrevTalbe, ++envIndex);
if (symTable == null) {
return new ASTNode_GetGlobalVar(){Index=GlobalSymbolTable.Instance().LookupOrDefine(name)};
} else {
return new ASTNode_GetFreeVar(){EnvIndex=envIndex, Index=symTable.Lookup(name)};
}
} else if (!(exp is List<object>)) {
return new ASTNode_Literal(){Value=exp};
}
List<object> l = (List<object>)exp;
switch (l[0] as string) {
case "quote":
return new ASTNode_Literal() { Value = ListExpToPairExp(l[1]) };
case "if":
return new ASTNode_If() { PredNode = Compile(symTable, l[1]), ThenNode = Compile(symTable, l[2]), ElseNode = Compile(symTable, l[3]) };
case "lambda": {
SymbolTable newSymTable = new SymbolTable(symTable);
foreach (string name in ((List<object>)l[1])) {
newSymTable.Define(name);
}
List<string> defines = new List<string>();
FindDefinition(defines, (List<object>)l[2], 1);
foreach (string name in defines) {
newSymTable.Define(name);
}
return new ASTNode_Lambda() { LocalVarCount = newSymTable.GetSymbolCount(), BodyNode = Compile(newSymTable, l[2]) };
}
case "begin":
return new ASTNode_Begin() { Nodes = l.Skip(1).Select(e => Compile(symTable, e)).ToArray() };
case "define":
return Compile(symTable, new List<object>() { "set!", l[1], l[2] });
case "set!": {
IASTNode right = Compile(symTable, l[2]);
string name = (string)l[1];
if (symTable != null && symTable.Lookup(name) != -1) {
return new ASTNode_SetLocalVar() { Index = symTable.Lookup(name), RightNode = right };
}
int envIndex = 0;
for (; symTable != null && symTable.Lookup(name) == -1; symTable = symTable.PrevTalbe, ++envIndex) ;
if (symTable == null) {
return new ASTNode_SetGlobalVar() { Index = GlobalSymbolTable.Instance().LookupOrDefine(name), RightNode = right };
} else {
return new ASTNode_SetFreeVar() { EnvIndex = envIndex, Index = symTable.Lookup(name), RightNode = right };
}
}
default: {
return new ASTNode_Application() { ProcedureNode = Compile(symTable, l[0]), ActualNodes = l.Skip(1).Select(e => Compile(symTable, e)).ToArray() };
}
}
}