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


C# SymbolTable.AddParameter方法代码示例

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


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

示例1: GenStmt

        private void GenStmt(ParseTreeNode stmt, ILGenerator il, SymbolTable symbolTable)
        {
            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[1]);
            }
            else
            {
                localType = this.TypeOfExpr(stmt.ChildNodes[2].ChildNodes[1], symbolTable);
                //symbolTable.locals[stmt.ChildNodes[1].ChildNodes[0].Token.ValueString] = il.DeclareLocal(this.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, this.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")
            {
                ParseTreeNode argItem = stmt.ChildNodes[1];
                while (true)
                {
                    this.GenExpr(argItem.ChildNodes[0], typeof(string), il, symbolTable);
                    il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new System.Type[] { typeof(string) }));
                    if (argItem.ChildNodes.Count > 1)
                        argItem = argItem.ChildNodes[1];
                    else
                        break;
                }
                il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { }));
            }
            }
            else if (stmt.Term.Name == "assignment")
            {
            string ident = stmt.ChildNodes[0].Token.ValueString;
            this.GenExpr(stmt.ChildNodes[1].ChildNodes[1], this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il, symbolTable);
            symbolTable.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il);
            //this.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), ref il, symbolTable);
            }
            else if (stmt.Term.Name == "functionDefinition")
            {
            string functionName = stmt.ChildNodes[1].Token.ValueString;
            if (functionTable.ContainsKey(functionName))
            {
                throw new Exception(functionName + " has already been defined");
            }
            var parameterList = new List<FunctionDefinition.Argument>();
            List<Type> types = new List<Type>();
            SymbolTable localSymbols = new SymbolTable();
            if (stmt.ChildNodes[2].ChildNodes.Count > 0)
            {
                var currParam = stmt.ChildNodes[2].ChildNodes[0];
                while (true)
                {
                    var parameterType = TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable);
                    var paramIdentifier = currParam.ChildNodes[0].ChildNodes[0];
                    while (true)
                    {
                        var parameterName = paramIdentifier.ChildNodes[0].Token.ValueString;
                        parameterList.Add(new FunctionDefinition.Argument() { argName = parameterName, argType = parameterType });
                        localSymbols.AddParameter(parameterName, parameterType);
                        types.Add(parameterType);
                        if (paramIdentifier.ChildNodes.Count == 1)
                            break;
                        paramIdentifier = paramIdentifier.ChildNodes[1];
                    }

                    if (currParam.ChildNodes.Count == 1)
//.........这里部分代码省略.........
开发者ID:mirhagk,项目名称:IronTuring,代码行数:101,代码来源:CodeGenOld.cs

示例2: GenStmt


//.........这里部分代码省略.........
            {

                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;
                    symbolTable.Store(ident, TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), il);
                }
            }
            else if (stmt.Term.Name == "functionDefinition")
            {
                string functionName = stmt.ChildNodes[0].ChildNodes[1].Token.ValueString;

                SymbolTable localSymbols = new SymbolTable(symbolTable);
                foreach (var parameter in symbolTable.functionTable[functionName].arguments)
                {
                    localSymbols.AddParameter(parameter.argName, parameter.argType);
                }

                var ilMeth = symbolTable.functionTable[functionName].GetILGenerator();
                GenStmt(stmt.ChildNodes[1], ilMeth, localSymbols);
                ilMeth.Emit(OpCodes.Ret);
            }
            else if (stmt.Term.Name == "result")
            {
                GenExpr(stmt.ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1], symbolTable), il, symbolTable);
                var result = il.DeclareLocal(TypeOfExpr(stmt.ChildNodes[1], symbolTable));
                il.Emit(OpCodes.Stloc, result);
                il.Emit(OpCodes.Ldloc, result);
                il.Emit(OpCodes.Ret, result);
            }
            else if (stmt.Term.Name == "functionCall" | stmt.Term.Name == "memberCall")
            {
                GenExpr(stmt, null, il, symbolTable);
            }
            else if (stmt.Term.Name == "ifBlock")
            {
                Label ifTrue = il.DefineLabel();
                Label ifFalse = il.DefineLabel();
                Label endLabel = il.DefineLabel();

                GenExpr(stmt.ChildNodes[0], typeof(bool), il, symbolTable);//expression to check if true
                il.Emit(OpCodes.Brtrue, ifTrue);//if true then jump to true block
                il.Emit(OpCodes.Br, ifFalse);//otherwise jump to false block

                il.MarkLabel(ifTrue);//true block
                GenStmt(stmt.ChildNodes[1], il, symbolTable);
                il.Emit(OpCodes.Br, endLabel);//jump to after false block
开发者ID:mirhagk,项目名称:IronTuring,代码行数:66,代码来源:CodeGen.cs


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