本文整理汇总了C#中SymbolTable.PushVar方法的典型用法代码示例。如果您正苦于以下问题:C# SymbolTable.PushVar方法的具体用法?C# SymbolTable.PushVar怎么用?C# SymbolTable.PushVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.PushVar方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenStmt
private void GenStmt(ParseTreeNode stmt, ILGenerator il, SymbolTable symbolTable, Label? exitScope = null)
{
if (stmt.Term.Name == "program")
{
if (stmt.ChildNodes.Count > 0)
{
this.GenStmt(stmt.ChildNodes[0].ChildNodes[0], il, symbolTable);
this.GenStmt(stmt.ChildNodes[1], il, symbolTable);
}
}
else if (stmt.Term.Name == "variableDeclaration")
{
Type localType;
// declare a local
if (stmt.ChildNodes[2].Term.Name == "typeSpecifier")
{
localType = this.TypeOfTypeDeclaration(stmt.ChildNodes[2].ChildNodes[0]);
}
else
{
localType = TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable);
}
Action<string> generateAssign = null;
ParseTreeNode assign = stmt.ChildNodes.Where(x => x.Term.Name == "setEqual").SingleOrDefault();
// set the initial value
if (assign != null)
{
generateAssign = new Action<string>(name =>
{
this.GenExpr(assign.ChildNodes[1], symbolTable.Locals[name].LocalType, il, symbolTable);
symbolTable.Store(name, TypeOfExpr(assign.ChildNodes[1], symbolTable), il);
});
}
var variableIden = stmt.ChildNodes[1];
while (true)
{
string name = variableIden.ChildNodes[0].Token.ValueString;
symbolTable.AddLocal(name, il.DeclareLocal(localType));
if (generateAssign != null)
generateAssign(name);
if (variableIden.ChildNodes.Count < 2)
break;
variableIden = variableIden.ChildNodes[1];
}
}
else if (stmt.Term.Name == "io")
{
if (stmt.ChildNodes[0].Token.ValueString == "put")
{
//the first argument is always there, until we can build a proper AST this'll have to do
ParseTreeNode argItem = stmt.ChildNodes[1];
this.GenExpr(argItem.ChildNodes[0], typeof(string), il, symbolTable);
il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new System.Type[] { typeof(string) }));
argItem = stmt.ChildNodes[2];
while (true)
{
if (argItem.ChildNodes.Count == 0)
break;
this.GenExpr(argItem.ChildNodes[0].ChildNodes[0], typeof(string), il, symbolTable);
il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new System.Type[] { typeof(string) }));
argItem = argItem.ChildNodes[1];
}
if (stmt.ChildNodes[3].ChildNodes.Count == 0)//put a newline character if there is no ...
il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { }));
}
else if (stmt.ChildNodes[0].Token.ValueString == "get")
{
foreach (var argument in stmt.ChildNodes[1].ChildNodes)
{
//switch(symbolTable.TypeOfVar(
il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("ReadLine", new System.Type[] { }));
symbolTable.Store(argument.Token.ValueString, typeof(string), il);
}
}
}
else if (stmt.Term.Name == "assignment")
{
if (stmt.ChildNodes[0].Term.Name == "functionCall")//if we see this as a function call, we know that's not true, and it's actually an array (which is kinda the same thing in turing)
{
string arrayName = stmt.ChildNodes[0].ChildNodes[0].Token.ValueString;
if (symbolTable.TypeOfVar(arrayName).IsArray)
{
symbolTable.PushVar(arrayName, il);
if (stmt.ChildNodes[0].ChildNodes[1].ChildNodes.Count > 1)
throw new NotImplementedException("Multi-Dimensional arrays are not yet supported");
this.GenExpr(stmt.ChildNodes[0].ChildNodes[1].ChildNodes[0], typeof(int), il, symbolTable);
this.GenExpr(stmt.ChildNodes[1].ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il, symbolTable);
il.Emit(OpCodes.Stelem, symbolTable.TypeOfVar(arrayName).GetElementType());
}
else
throw new NotSupportedException(String.Format("Non-array identifier used like an array: {0}", arrayName));
}
else
{
this.GenExpr(stmt.ChildNodes[1].ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il, symbolTable);
string ident = stmt.ChildNodes[0].Token.ValueString;
//.........这里部分代码省略.........
示例2: GenExpr
/*private void Store(string name, System.Type type, ref ILGenerator il, SymbolTable symbolTable)
{
if (symbolTable.locals.ContainsKey(name))
{
LocalBuilder locb = symbolTable.locals[name];
if (locb.LocalType == type)
{
il.Emit(OpCodes.Stloc, symbolTable.locals[name]);
}
else
{
throw new System.Exception("'" + name + "' is of type " + locb.LocalType.Name + " but attempted to store value of type " + type.Name);
}
}
else
{
throw new System.Exception("undeclared variable '" + name + "'");
}
}*/
private void GenExpr(ParseTreeNode expr, System.Type expectedType, ILGenerator il, SymbolTable symbolTable)
{
Type deliveredType;
if (expr.Term.Name == "stringLiteral")
{
deliveredType = typeof(string);
il.Emit(OpCodes.Ldstr, expr.Token.ValueString);
}
else if (expr.Term.Name == "number")
{
if (expr.Token.Value is int)
{
deliveredType = typeof(int);
il.Emit(OpCodes.Ldc_I4, (int)expr.Token.Value);
}
else
{
deliveredType = typeof(float);
il.Emit(OpCodes.Ldc_R4, float.Parse(expr.Token.ValueString));
}
}
else if (expr.Term.Name == "binExpr")
{
deliveredType = TypeOfExpr(expr.ChildNodes[0], symbolTable);
GenExpr(expr.ChildNodes[0], deliveredType, il, symbolTable);
GenExpr(expr.ChildNodes[2], deliveredType, il, symbolTable);
switch (expr.ChildNodes[1].Term.Name)
{
case "+":
il.Emit(OpCodes.Add);
break;
case "*":
il.Emit(OpCodes.Mul);
break;
case "-":
il.Emit(OpCodes.Sub);
break;
case "/":
il.Emit(OpCodes.Div);
break;
case "mod":
il.Emit(OpCodes.Rem);
break;
default:
throw new Exception("Unrecognized operator " + expr.ChildNodes[1].Term.Name);
}
}
else if (expr.Term.Name == "identifier")
{
string ident = expr.Token.ValueString;
symbolTable.PushVar(ident, ref il);
deliveredType = this.TypeOfExpr(expr, symbolTable);
}
else if (expr.Term.Name == "functionCall")
{
deliveredType = TypeOfExpr(expr, symbolTable);
string funcName = expr.ChildNodes[0].Token.ValueString;
if (!this.functionTable.ContainsKey(funcName))
{
throw new System.Exception("undeclared function or procedure '" + funcName+ "'");
}
var parameters = this.functionTable[funcName].arguments;
int currentArgument = 0;
if (expr.ChildNodes[1].ChildNodes.Count > 0)
{//push all the arguments onto the stack
ParseTreeNode argItem = expr.ChildNodes[1];
while (true)
{
this.GenExpr(argItem.ChildNodes[0], parameters[currentArgument].argType, il, symbolTable);
if (argItem.ChildNodes.Count == 1)
break;
argItem = argItem.ChildNodes[1];
currentArgument++;
}
}
il.Emit(OpCodes.Call, this.functionTable[funcName].methodDefinition);
}
//.........这里部分代码省略.........
示例3: GenExpr
//.........这里部分代码省略.........
}
}
else
{
switch (expr.ChildNodes[1].Term.Name)
{
case "+":
il.Emit(OpCodes.Add);
break;
case "*":
il.Emit(OpCodes.Mul);
break;
case "-":
il.Emit(OpCodes.Sub);
break;
case "/":
il.Emit(OpCodes.Div);
break;
case "div":
il.Emit(OpCodes.Div);
expectedType = typeof(int);
break;
case "mod":
il.Emit(OpCodes.Rem);
break;
default:
throw new Exception("Unrecognized operator " + expr.ChildNodes[1].Term.Name);
}
}
}
else if (expr.Term.Name == "identifier")
{
string ident = expr.Token.ValueString;
symbolTable.PushVar(ident, il);
deliveredType = TypeOfExpr(expr, symbolTable);
if (deliveredType == typeof(float))
{
throw new NotImplementedException();
}
}
else if (expr.Term.Name == "functionCall"|expr.Term.Name=="memberCall")
{
deliveredType = TypeOfExpr(expr, symbolTable);
if (deliveredType == typeof(float))
{
throw new NotImplementedException();
}
string funcName = GetIdentifier(expr);
if (!symbolTable.functionTable.ContainsKey(funcName))
{
if (symbolTable.HasVar(funcName) && symbolTable.TypeOfVar(funcName).IsArray)
{
//this is an array, return the appropriate value
symbolTable.PushVar(funcName, il);
if (expr.ChildNodes[1].ChildNodes.Count > 1)
throw new NotImplementedException("Multi-Dimensional arrays are not yet supported");
this.GenExpr(expr.ChildNodes[1].ChildNodes[0], typeof(int), il, symbolTable);
il.Emit(OpCodes.Stelem, symbolTable.TypeOfVar(funcName).GetElementType());
}
else
throw new System.Exception("undeclared function or procedure '" + funcName + "'");
}
else