Пример #1
0
        /*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, ref 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, ref il, symbolTable);
                GenExpr(expr.ChildNodes[2], deliveredType, ref 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;

                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 + "'");
                }
                //il.Emit(OpCodes.Call, mainProgram.GetMethod(funcName));
                var parameters = this.functionTable[funcName].arguments;
                int curParam   = 0;
                if (expr.ChildNodes[1].ChildNodes.Count > 0)
                {//push all the arguments onto the stack
                    ParseTreeNode argItem = expr.ChildNodes[1].ChildNodes[0];
                    while (true)
                    {
                        this.GenExpr(argItem.ChildNodes[0], parameters[curParam].argType, ref il, symbolTable);
                        if (argItem.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        argItem = argItem.ChildNodes[1];
                        curParam++;
                    }
                }
                il.Emit(OpCodes.Call, this.functionTable[funcName].methodDefinition);
            }
            else
            {
                throw new System.Exception("don't know how to generate " + expr.GetType().Name);
            }

            if (deliveredType != expectedType)
            {
                if (deliveredType == typeof(int) &&
                    expectedType == typeof(string))
                {
                    il.Emit(OpCodes.Box, typeof(int));
                    il.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
                }
                else if (deliveredType == typeof(float) && expectedType == typeof(string))
                {
                    il.Emit(OpCodes.Box, typeof(float));
                    il.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
                }
                else
                {
                    throw new System.Exception("can't coerce a " + deliveredType.Name + " to a " + expectedType.Name);
                }
            }
        }
Пример #2
0
        private void GenStmt(ParseTreeNode stmt, ref ILGenerator il, SymbolTable symbolTable)
        {
            if (stmt.Term.Name == "program")
            {
                if (stmt.ChildNodes.Count > 0)
                {
                    this.GenStmt(stmt.ChildNodes[0].ChildNodes[0], ref il, symbolTable);
                    this.GenStmt(stmt.ChildNodes[1], ref 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, ref il, symbolTable);
                        symbolTable.Store(name, this.TypeOfExpr(assign.ChildNodes[1], symbolTable), ref il);
                    }
                                                         );
                    //this.Store(stmt.ChildNodes[1].Token.ValueString, this.TypeOfExpr(assign.ChildNodes[1], symbolTable), ref il, symbolTable);
                }
            }
            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), ref 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), ref il, symbolTable);
                symbolTable.Store(ident, this.TypeOfExpr(stmt.ChildNodes[1].ChildNodes[1], symbolTable), ref 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         argumentList = new List <FunctionDefinition.Argument>();
                List <Type> types        = new List <Type>();
                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);
                        types.Add(parameterType);
                        argumentList.Add(new FunctionDefinition.Argument()
                        {
                            argName = currParam.ChildNodes[0].ChildNodes[0].Token.ValueString, argType = parameterType
                        });
                        if (currParam.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        currParam = currParam.ChildNodes[1];
                    }
                }
                var         methodDeclaration = mainProgram.DefineMethod(functionName, MethodAttributes.Static, TypeOfExpr(stmt.ChildNodes[3].ChildNodes[1], symbolTable), types.ToArray());
                var         ilMeth            = methodDeclaration.GetILGenerator();
                SymbolTable localSymbols      = new SymbolTable();
                if (stmt.ChildNodes[2].ChildNodes.Count > 0)
                {
                    var currParam = stmt.ChildNodes[2].ChildNodes[0];
                    while (true)
                    {
                        localSymbols.parameters.Add(new Tuple <string, Type>(currParam.ChildNodes[0].ChildNodes[0].Token.ValueString, TypeOfExpr(currParam.ChildNodes[0].ChildNodes[1].ChildNodes[1], symbolTable)));
                        if (currParam.ChildNodes.Count == 1)
                        {
                            break;
                        }
                        currParam = currParam.ChildNodes[1];
                    }
                }
                GenStmt(stmt.ChildNodes[4], ref ilMeth, localSymbols);
                ilMeth.Emit(OpCodes.Ret);
                var methodDec = new FunctionDefinition()
                {
                    methodDefinition = methodDeclaration,
                    arguments        = argumentList
                };
                functionTable.Add(functionName, methodDec);
            }
            else if (stmt.Term.Name == "result")
            {
                GenExpr(stmt.ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1], symbolTable), ref 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 is ReadInt)
             * {
             *  this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("ReadLine", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { }, null));
             *  this.il.Emit(Emit.OpCodes.Call, typeof(int).GetMethod("Parse", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, new System.Type[] { typeof(string) }, null));
             *  this.Store(((ReadInt)stmt).Ident, typeof(int));
             * }
             * else if (stmt is ForLoop)
             * {
             *  // example:
             *  // for x = 0 to 100 do
             *  //   print "hello";
             *  // end;
             *
             *  // x = 0
             *  ForLoop forLoop = (ForLoop)stmt;
             *  Assign assign = new Assign();
             *  assign.Ident = forLoop.Ident;
             *  assign.Expr = forLoop.From;
             *  this.GenStmt(assign);
             *  // jump to the test
             *  Emit.Label test = this.il.DefineLabel();
             *  this.il.Emit(Emit.OpCodes.Br, test);
             *
             *  // statements in the body of the for loop
             *  Emit.Label body = this.il.DefineLabel();
             *  this.il.MarkLabel(body);
             *  this.GenStmt(forLoop.Body);
             *
             *  // to (increment the value of x)
             *  this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]);
             *  this.il.Emit(Emit.OpCodes.Ldc_I4, 1);
             *  this.il.Emit(Emit.OpCodes.Add);
             *  this.Store(forLoop.Ident, typeof(int));
             *
             *  // **test** does x equal 100? (do the test)
             *  this.il.MarkLabel(test);
             *  this.il.Emit(Emit.OpCodes.Ldloc, this.symbolTable[forLoop.Ident]);
             *  this.GenExpr(forLoop.To, typeof(int));
             *  this.il.Emit(Emit.OpCodes.Blt, body);
             * }*/
            else
            {
                throw new System.Exception("don't know how to gen a " + stmt.Term.Name);
            }
        }