예제 #1
0
        public static List <string> GetContext(string Css, string Keyword)
        {
            List <string> Contexts = new List <string>();

            CssFx.CssParser Parser = new CssFx.CssParser("", Css);
            Parser.ParseStyleSheet();
            CssFx.CssStyleSheet ParsedCss = Parser.StyleSheet;
            foreach (CssFx.CssStatement Stmt in ParsedCss.Statements)
            {
                switch (Stmt.GetType().Name)
                {
                case ("CssRuleSet"):
                    Contexts.AddRange(GetContext((CssFx.CssRuleSet)Stmt, Keyword));
                    break;

                case ("CssAtRule"):
                    Contexts.AddRange(GetContext((CssFx.CssAtRule)Stmt, Keyword));
                    break;
                }
            }
            foreach (string Comment in ParsedCss.Comments)
            {
                if (Comment.IndexOf(Keyword, StringComparison.OrdinalIgnoreCase) > -1)
                {
                    Contexts.Add("Comment");
                }
            }
            return(Contexts);
        }
예제 #2
0
    private void GenStmt(Stmt stmt)
    {
        if (stmt is Sequence)
        {
            var seq = (Sequence)stmt;
            GenStmt(seq.First);
            GenStmt(seq.Second);
        }

        else if (stmt is DeclareVariable)
        {
            // declare a local
            var declare = (DeclareVariable)stmt;
            SymbolTable[declare.Ident] = _il.DeclareLocal(declare.Expr.GetType());

            // set the initial value
            var assign = new Assign { Ident = declare.Ident, Expr = declare.Expr };
            GenStmt(assign);
        }

        else if (stmt is Assign)
        {
            var assign = (Assign)stmt;
            GenerateLoadToStackForExpr(assign.Expr, assign.Expr.GetType());
            GenerateStoreFromStack(assign.Ident, assign.Expr.GetType());
        }
        else if (stmt is ConsoleOut)
        {
            // the "ConsoleOut" statement is an alias for System.Console.WriteLine.
            // it uses the string case
            GenerateLoadToStackForExpr(((ConsoleOut)stmt).Expr, typeof(string));
            //Generate console.writeline
            _il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));
        }
        else if (stmt is ReadConsoleIn)
        {
            _il.Emit(OpCodes.Call,
                typeof(Console).GetMethod("ReadLine", BindingFlags.Public | BindingFlags.Static, null,
                    new Type[] { }, null));

            Type identType = GetIdentType(stmt);

            if(identType == typeof(int))
                _il.Emit(OpCodes.Call,
                typeof(int).GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null,
                    new[] { typeof(string) }, null));

            GenerateStoreFromStack(((ReadConsoleIn)stmt).Ident, identType);
        }
        else if (stmt is ForLoop)
        {
            // example:
            // for x = 0 to 100 up
            //    "hello";
            // end;

            // x = 0
            var forLoop = (ForLoop)stmt;
            var assign = new Assign { Ident = forLoop.Ident, Expr = forLoop.From };
            GenStmt(assign);
            // jump to the test
            var test = _il.DefineLabel();
            _il.Emit(OpCodes.Br, test);

            // statements in the body of the for loop
            var body = _il.DefineLabel();
            _il.MarkLabel(body);
            GenStmt(forLoop.Body);

            // to (increment the value of x)
            _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
            _il.Emit(OpCodes.Ldc_I4, 1);
            _il.Emit(forLoop.Type == ArithOp.Up ? OpCodes.Add : OpCodes.Sub);
            GenerateStoreFromStack(forLoop.Ident, typeof(int));

            // **test** does x equal 100? (do the test)
            _il.MarkLabel(test);
            _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
            GenerateLoadToStackForExpr(forLoop.To, typeof(int));
            _il.Emit(forLoop.Type == ArithOp.Up ? OpCodes.Blt : OpCodes.Bgt, body);
        }
        else
        {
            throw new Exception("don't know how to gen a " + stmt.GetType().Name);
        }
    }
예제 #3
0
파일: CodeGen.cs 프로젝트: slb1988/cs143
    private void GenStmt(Stmt stmt)
    {
        if (stmt is Sequence)
        {
            Sequence seq = (Sequence)stmt;
            this.GenStmt(seq.First);
            this.GenStmt(seq.Second);
        }

        else if (stmt is DeclareVar)
        {
            // declare a local
            DeclareVar declare = (DeclareVar)stmt;
            this.symbolTable[declare.Ident] = this.il.DeclareLocal(this.TypeOfExpr(declare.Expr));

            // set the initial value
            Assign assign = new Assign();
            assign.Ident = declare.Ident;
            assign.Expr  = declare.Expr;
            this.GenStmt(assign);
        }

        else if (stmt is Assign)
        {
            Assign assign = (Assign)stmt;
            this.GenExpr(assign.Expr, this.TypeOfExpr(assign.Expr));
            this.Store(assign.Ident, this.TypeOfExpr(assign.Expr));
        }
        else if (stmt is Print)
        {
            // the "print" statement is an alias for System.Console.WriteLine.
            // it uses the string case
            this.GenExpr(((Print)stmt).Expr, typeof(string));
            this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { typeof(string) }));
        }

        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.GetType().Name);
        }
    }
예제 #4
0
    private void GenStmt(Stmt stmt)
    {
        if (stmt is Sequence)
        {
            Sequence seq = (Sequence)stmt;
            this.GenStmt(seq.First);
            this.GenStmt(seq.Second);
        }

        else if (stmt is DeclareVar)
        {
            // declare a local
            DeclareVar declare = (DeclareVar)stmt;
            this.symbolTable[declare.Ident] = this.il.DeclareLocal(this.TypeOfExpr(declare.Expr));

            // set the initial value
            Assign assign = new Assign();
            assign.Ident = declare.Ident;
            assign.Expr  = declare.Expr;
            this.GenStmt(assign);
        }

        else if (stmt is Assign)
        {
            Assign assign = (Assign)stmt;
            this.GenExpr(assign.Expr, this.TypeOfExpr(assign.Expr));
            this.Store(assign.Ident, this.TypeOfExpr(assign.Expr));
        }
        else if (stmt is Print)
        {
            // the "print" statement is an alias for System.Console.WriteLine.
            // it uses the string case
            this.GenExpr(((Print)stmt).Expr, typeof(string));
            this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { typeof(string) }));
        }

        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 mcIf)
        {
            mcIf mcif = (mcIf)stmt;
            //this.GenExpr(mcif.compExpr, typeof(int));
            Emit.Label Else   = this.il.DefineLabel();
            Emit.Label Then   = this.il.DefineLabel();
            Emit.Label Salida = this.il.DefineLabel();
            if (mcif.compExpr is CompExpr)
            {
                CompExpr compExpr = (CompExpr)mcif.compExpr;
                this.GenExpr(compExpr.Left, typeof(int));
                this.GenExpr(compExpr.Rigth, typeof(int));
                genCodComp(compExpr.Op, Then);
            }
            else
            {
                GenExpr(mcif.compExpr, typeof(string));
                this.il.Emit(Emit.OpCodes.Ldc_I4, 0);
                this.il.Emit(Emit.OpCodes.Bne_Un, Then);
            }
            if (mcif.Else != null)
            {
                this.il.Emit(Emit.OpCodes.Br, Else);
            }
            else
            {
                this.il.Emit(Emit.OpCodes.Br, Salida);
            }
            this.il.MarkLabel(Then);
            GenStmt(mcif.Then);
            this.il.Emit(Emit.OpCodes.Br, Salida);
            this.il.MarkLabel(Else);
            if (mcif.Else != null)
            {
                GenStmt(mcif.Else);
            }
            this.il.MarkLabel(Salida);
        }
        else if (stmt is WhileLoop)
        {
            WhileLoop  whileLoop = (WhileLoop)stmt;
            Emit.Label Body      = this.il.DefineLabel();
            Emit.Label Salida    = this.il.DefineLabel();
            Emit.Label Cond      = this.il.DefineLabel();
            this.il.MarkLabel(Cond);
            if (whileLoop.Cond is CompExpr)
            {
                CompExpr compExpr = (CompExpr)whileLoop.Cond;
                this.GenExpr(compExpr.Left, typeof(int));
                this.GenExpr(compExpr.Rigth, typeof(int));
                genCodComp(compExpr.Op, Body);
            }
            else
            {
                GenExpr(whileLoop.Cond, typeof(string));
                this.il.Emit(Emit.OpCodes.Ldc_I4, 0);
                this.il.Emit(Emit.OpCodes.Bne_Un, Body);
            }
            this.il.Emit(Emit.OpCodes.Br, Salida);
            this.il.MarkLabel(Body);
            GenStmt(whileLoop.Body);
            this.il.Emit(Emit.OpCodes.Br, Cond);
            this.il.MarkLabel(Salida);
        }
        //*******************
        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("imposible generar: " + stmt.GetType().Name);
        }
    }
예제 #5
0
    private void GenStmt(Stmt stmt)
    {
        if (stmt is Sequence)
        {
            Sequence seq = (Sequence)stmt;
            this.GenStmt(seq.First);
            this.GenStmt(seq.Second);
        }

        else if (stmt is DeclareVar)
        {
            // declare a local
            DeclareVar declare = (DeclareVar)stmt;
            this.symbolTable[declare.Ident] = this.il.DeclareLocal(this.TypeOfExpr(declare.Expr));

            // set the initial value
            Assign assign = new Assign();
            assign.Ident = declare.Ident;
            assign.Expr = declare.Expr;
            this.GenStmt(assign);
        }

        else if (stmt is Assign)
        {
            Assign assign = (Assign)stmt;
            this.GenExpr(assign.Expr, this.TypeOfExpr(assign.Expr));
            this.Store(assign.Ident, this.TypeOfExpr(assign.Expr));
        }
        else if (stmt is Print)
        {
            // the "print" statement is an alias for System.Console.WriteLine.
            // it uses the string case
            this.GenExpr(((Print)stmt).Expr, typeof(string));
            this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { typeof(string) }));
        }

        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.GetType().Name);
        }
    }
예제 #6
0
파일: CodeRun.cs 프로젝트: afumfer/KNote
        private void RunStmt(Stmt stmt)
        {
            if (flagBreak)
            {
                return;
            }

            //Application.DoEvents();

            #region Sequence

            if (stmt is Sequence)
            {
                Sequence seq = (Sequence)stmt;
                RunStmt(seq.First);
                RunStmt(seq.Second);
            }

            #endregion

            #region DeclareVar

            else if (stmt is DeclareVar)
            {
                // declare
                DeclareVar declare = (DeclareVar)stmt;

                CodeDeclareSymbol(declare);

                //TODO: (Z) Sustituir lo anterior por esto cuando se
                //       arregle el código de asignación + declaración.
                //Assign assign = new Assign();
                //assign.Ident = declare.Ident;
                //assign.Expr = declare.Expr;
                //RunStmt(assign);
            }

            #endregion

            #region Assign

            else if (stmt is Assign)
            {
                Assign assign = (Assign)stmt;
                CodeStoreSymbol(assign);
            }

            #endregion

            #region Print / PrintLine / Clear

            else if (stmt is Print)
            {
                Print print = (Print)stmt;
                CodeExecutePrint(print);
            }

            else if (stmt is PrintLine)
            {
                PrintLine printLine = (PrintLine)stmt;
                CodeExecutePrintLine(printLine);
            }

            else if (stmt is Clear)
            {
                CodeExecuteClear();
            }

            #endregion

            #region FunctionStmt

            else if (stmt is FunctionStmt)
            {
                FunctionStmt fun = (FunctionStmt)stmt;
                CodeExecuteFunction(fun.Function);
            }

            #endregion

            #region ReadVar

            else if (stmt is ReadVar)
            {
                ReadVar            read = (ReadVar)stmt;
                ReadVarItem        readVarItem;
                Assign             assign;
                List <ReadVarItem> readVarItmes = new List <ReadVarItem>();

                foreach (var pair in read.Vars)
                {
                    readVarItem          = new ReadVarItem();
                    readVarItem.VarIdent = pair.Key.Ident;
                    readVarItem.VarValue = CodeReadSymbol(pair.Key);
                    readVarItem.Label    = GenExpr(pair.Value);
                    readVarItmes.Add(readVarItem);
                }

                if (CodeExecuteReadVars(readVarItmes))
                {
                    foreach (ReadVarItem vi in readVarItmes)
                    {
                        assign = new Assign();
                        //assign.Ident = vi.Var.Ident;
                        assign.Ident = vi.VarIdent;
                        assign.Expr  = ValueToExpr(vi.VarValue.GetType(), vi.VarNewValueText);
                        RunStmt(assign);
                    }
                }
            }

            #endregion

            #region BreakStmt

            else if (stmt is BreakStmt)
            {
                flagBreak = true;
                return;
            }

            #endregion

            #region  FoorLoop

            else if (stmt is ForLoop)
            {
                // example:
                // for x = 0 to 100
                //   print "hello";
                // end for;

                ForLoop forLoop = (ForLoop)stmt;

                IntVal numFrom = new IntVal();
                IntVal numTo   = new IntVal();

                Assign assignFrom = new Assign();
                assignFrom.Ident = forLoop.Ident;

                assignFrom.Expr = forLoop.From;
                RunStmt(assignFrom);

                numFrom.Value = Convert.ToInt32(GenExpr(forLoop.From));
                numTo.Value   = Convert.ToInt32(GenExpr(forLoop.To));

                while (numFrom.Value <= numTo.Value)
                {
                    if (flagBreak)
                    {
                        break;
                    }
                    RunStmt(forLoop.Body);
                    numFrom.Value++;
                    assignFrom.Expr = numFrom;
                    RunStmt(assignFrom);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            #region FoorEachLoop

            else if (stmt is ForEachLoop)
            {
                // example:
                // foreach x in myColec
                //   print "hello";
                //   print x.MyProp;
                // end foreach;

                ForEachLoop forEachLoop = (ForEachLoop)stmt;

                object colec = GenExpr(forEachLoop.Colec);

                foreach (object o in (IEnumerable <object>)colec)
                {
                    if (flagBreak)
                    {
                        break;
                    }

                    // TODO: Pending susutiruir CodeSpecialStoreObject by CodeStoreSymbol
                    //       In the future, CodeStoreSymbol should store the variable
                    //       if it had not previously been declared.
                    CodeSpecialStoreObject(forEachLoop.Ident, o);
                    // CodeStoreSymbol(forEachLoop.Ident, o);

                    RunStmt(forEachLoop.Body);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            #region IfStmt

            else if (stmt is IfStmt)
            {
                // example:
                // if a == 10 then
                //   print "hello";
                // else
                //   print "bye";
                // end if;

                IfStmt ifStmt = (IfStmt)stmt;
                IntVal ifExp  = new IntVal();

                ifExp.Value = Convert.ToInt32(GenExpr(ifStmt.TestExpr));

                if (ifExp.Value != 0)
                {
                    RunStmt(ifStmt.BodyIf);
                }
                else
                {
                    if (ifStmt.DoElse)
                    {
                        RunStmt(ifStmt.BodyElse);
                    }
                }
            }

            #endregion

            #region WhileStmt

            else if (stmt is WhileStmt)
            {
                // example:
                // while a <= 10
                //   print "hello";
                //   a = a + 1;
                // end while;

                WhileStmt whileStmt = (WhileStmt)stmt;
                IntVal    whileExp  = new IntVal();

                while (true)
                {
                    if (flagBreak)
                    {
                        break;
                    }
                    whileExp.Value = Convert.ToInt32(GenExpr(whileStmt.TestExpr));
                    if (whileExp.Value == 0)
                    {
                        break;
                    }
                    RunStmt(whileStmt.Body);
                }
                if (flagBreak)
                {
                    flagBreak = false;
                }
            }

            #endregion

            else
            {
                throw new System.Exception("don't know how to gen a " + stmt.GetType().Name);
            }
        }
예제 #7
0
    private void GenStmt(Stmt stmt)
    {
        //Обработка элемента последовательности дерева
        if (stmt is Sequence)
        {
            Sequence seq = (Sequence)stmt;
            //Выполним обработку левой части дерева
            this.GenStmt(seq.First);
            //Выполним обработку правой части дерева
            this.GenStmt(seq.Second);
        }

        //Обработка элемента дерева - "Объявление"
        else if (stmt is DeclareVar)
        {
            // Добавим переменную в список переменных
            DeclareVar declare = (DeclareVar)stmt;
            this.varTable[declare.Ident] = this.TypeOfExpr(declare.Expression);

            //Приведем элемент типа "Объевление" к типу "Присвоение"
            Assign assign = new Assign();
            assign.Ident      = declare.Ident;
            assign.Expression = declare.Expression;

            //Запишем тип переменной
            accum.Append(string.Format("\n{0} ", this.TypeOfExpr(declare.Expression).Name));
            //Запустим на обработку
            this.GenStmt(assign);
        }

        //Обработка элемента дерева - "Присвоение"
        else if (stmt is Assign)
        {
            Assign assign = (Assign)stmt;
            accum.Append(string.Format("{0}=", assign.Ident));

            //Обработка правой части элемента
            this.GenAssign(assign.Expression);
            accum.Append(";");
        }

        //Обработка элемента дерева - "Вывод данных"
        else if (stmt is Print)
        {
            Print print = (Print)stmt;
            accum.Append(print.VarExpression != null
                             ? string.Format("\nConsole.WriteLine(\"{0}\", {1});", this.GetExprValue(print.Expression),
                                             this.GetExprValue(print.VarExpression))
                             : string.Format("\nConsole.WriteLine(\"{0}\");", this.GetExprValue(print.Expression)));
        }

        //Обработка элемента дерева - "Ввод данных"
        else if (stmt is ReadValue)
        {
            ReadValue readValue = (ReadValue)stmt;
            accum.Append(readValue.Exp != null
                             ? string.Format("\n{0} = Console.ReadLine(\"{1}\");", readValue.Ident, this.GetExprValue(readValue.Exp))
                             : string.Format("\n{0} = Console.ReadLine();", readValue.Ident));

            //Проверка, что переменная объявлена ранее
            //CheckVariable
        }
        else if (stmt is IfElse)
        {
            IfElse ifElse    = (IfElse)stmt;
            string operation = string.Empty;

            switch (ifElse.Condition.Operation)
            {
            case ConOperation.Equal: operation = "="; break;

            case ConOperation.Less: operation = "<"; break;

            case ConOperation.LessEqual: operation = "<="; break;

            case ConOperation.More: operation = ">"; break;

            case ConOperation.MoreEqual: operation = ">="; break;
            }
            accum.Append(string.Format("\nif ({0}{1}{2})", this.GetExprValue(ifElse.Condition.Left), operation, this.GetExprValue(ifElse.Condition.Right)));


            if (ifElse.BodyThen != null)
            {
                accum.Append("\n{\n");
                this.GenStmt(ifElse.BodyThen);
                accum.Append("\n}");
            }

            if (ifElse.BodyElse != null)
            {
                if (ifElse.BodyThen == null)
                {
                    throw new System.Exception("error if - else");
                }
                accum.Append("\nelse\n{\n");
                this.GenStmt(ifElse.BodyElse);
                accum.Append("\n}");
            }
        }
        else if (stmt is ForNext)
        {
            ForNext forNext = (ForNext)stmt;

            accum.Append(string.Format("\nfor("));
            Assign assign = new Assign();
            assign.Ident      = forNext.Ident;
            assign.Expression = forNext.From;
            this.GenStmt(assign);
            accum.Append(string.Format("{0}<{1};{2}++)", forNext.Ident, this.GetExprValue(forNext.To), forNext.Ident));

            this.varTable[forNext.Ident] = typeof(int);

            if (forNext.Body != null)
            {
                accum.Append("\n{");
                this.GenStmt(forNext.Body);
                accum.Append("\n}");
            }
        }
        else
        {
            throw new System.Exception("Отсутствует инструкция для генерирования операции: " + stmt.GetType().Name);
        }
    }
예제 #8
0
        private void GenStmt(Stmt stmt)
        {
            if (stmt is Sequence)
            {
                var seq = (Sequence)stmt;
                GenStmt(seq.First);
                GenStmt(seq.Second);
            }

            else if (stmt is DeclareVar)
            {
                // declare a local
                var declare = (DeclareVar)stmt;
                _symbolTable[declare.Ident] = _il.DeclareLocal(declare.Expr.GetType());

                // set the initial value
                var assign = new Assign(declare.Ident, declare.Expr);
                GenStmt(assign);
            }

            else if (stmt is Assign)
            {
                var assign = (Assign)stmt;
                GenerateLoadToStackForExpr(assign.Expr, assign.Expr.GetType());
                GenerateStoreFromStack(assign.Ident, assign.Expr.GetType());
            }
            else if (stmt is Print)
            {
                // the "print" statement is an alias for System.Console.WriteLine.
                // it uses the string case
                GenerateLoadToStackForExpr(((Print)stmt).Expr, typeof(string));
                //Generate console.writeline
                _il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }));
            }

            else if (stmt is ReadInt)
            {
                _il.Emit(OpCodes.Call,
                         typeof(Console).GetMethod("ReadLine", BindingFlags.Public | BindingFlags.Static, null,
                                                   Array.Empty <Type>(), null));
                _il.Emit(OpCodes.Call,
                         typeof(int).GetMethod("Parse", BindingFlags.Public | BindingFlags.Static, null,
                                               new[] { typeof(string) }, null));
                GenerateStoreFromStack(((ReadInt)stmt).Ident, typeof(int));
            }
            else if (stmt is ReadString)
            {
                _il.Emit(OpCodes.Call,
                         typeof(Console).GetMethod("ReadLine", BindingFlags.Public | BindingFlags.Static, null,
                                                   Array.Empty <Type>(), null));
                GenerateStoreFromStack(((ReadString)stmt).Ident, typeof(string));
            }
            else if (stmt is ForLoop)
            {
                // example:
                // for x = 0 to 100 do
                //   print "hello";
                // end;

                // x = 0
                var forLoop = (ForLoop)stmt;
                var assign  = new Assign(forLoop.Ident, forLoop.From);
                GenStmt(assign);
                // jump to the test
                var test = _il.DefineLabel();
                _il.Emit(OpCodes.Br, test);

                // statements in the body of the for loop
                var body = _il.DefineLabel();
                _il.MarkLabel(body);
                GenStmt(forLoop.Body);

                // to (increment the value of x)
                _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
                _il.Emit(OpCodes.Ldc_I4, 1);
                _il.Emit(OpCodes.Add);
                GenerateStoreFromStack(forLoop.Ident, typeof(int));

                // **test** does x equal 100? (do the test)
                _il.MarkLabel(test);
                _il.Emit(OpCodes.Ldloc, SymbolTable[forLoop.Ident]);
                GenerateLoadToStackForExpr(forLoop.To, typeof(int));
                _il.Emit(OpCodes.Blt, body);
            }
            else
            {
                throw new Exception("don't know how to gen a " + stmt.GetType().Name);
            }
        }
예제 #9
0
    private void GenStmt(Stmt stmt)
    {
        if (stmt is Sequence)
        {
            Sequence seq = (Sequence)stmt;
            this.GenStmt(seq.First);
            this.GenStmt(seq.Second);
        }

        else if (stmt is DeclareVar)
        {
            // declare a local
            DeclareVar declare = (DeclareVar)stmt;
            this.symbolTable[declare.Ident] = this.il.DeclareLocal(this.TypeOfExpr(declare.Expr));

            // set the initial value
            Assign assign = new Assign();
            assign.Ident = declare.Ident;
            assign.Expr = declare.Expr;
            this.GenStmt(assign);
        }

        else if (stmt is Assign)
        {
            Assign assign = (Assign)stmt;
            this.GenExpr(assign.Expr, this.TypeOfExpr(assign.Expr));
            this.Store(assign.Ident, this.TypeOfExpr(assign.Expr));
        }
        else if (stmt is Print)
        {
            // the "print" statement is an alias for System.Console.WriteLine.
            // it uses the string case
            this.GenExpr(((Print)stmt).Expr, typeof(string));
            this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { typeof(string) }));
        }

        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 mcIf)
        {
            mcIf mcif = (mcIf)stmt;
            //this.GenExpr(mcif.compExpr, typeof(int));
            Emit.Label Else = this.il.DefineLabel();
            Emit.Label Then = this.il.DefineLabel();
            Emit.Label Salida = this.il.DefineLabel();
            if (mcif.compExpr is CompExpr)
            {
                CompExpr compExpr = (CompExpr)mcif.compExpr;
                this.GenExpr(compExpr.Left, typeof(int));
                this.GenExpr(compExpr.Rigth, typeof(int));
                genCodComp(compExpr.Op, Then);
            }
            else
            {
                GenExpr(mcif.compExpr, typeof(string));
                this.il.Emit(Emit.OpCodes.Ldc_I4, 0);
                this.il.Emit(Emit.OpCodes.Bne_Un, Then);
            }
            if (mcif.Else != null)
                this.il.Emit(Emit.OpCodes.Br, Else);
            else
                this.il.Emit(Emit.OpCodes.Br, Salida);
            this.il.MarkLabel(Then);
            GenStmt(mcif.Then);
            this.il.Emit(Emit.OpCodes.Br, Salida);
            this.il.MarkLabel(Else);
            if (mcif.Else != null)
                GenStmt(mcif.Else);
            this.il.MarkLabel(Salida);

        }
        else if (stmt is WhileLoop)
        {
            WhileLoop whileLoop = (WhileLoop)stmt;
            Emit.Label Body = this.il.DefineLabel();
            Emit.Label Salida = this.il.DefineLabel();
            Emit.Label Cond = this.il.DefineLabel();
            this.il.MarkLabel(Cond);
            if (whileLoop.Cond is CompExpr)
            {
                CompExpr compExpr = (CompExpr)whileLoop.Cond;
                this.GenExpr(compExpr.Left, typeof(int));
                this.GenExpr(compExpr.Rigth, typeof(int));
                genCodComp(compExpr.Op, Body);
            }
            else
            {
                GenExpr(whileLoop.Cond, typeof(string));
                this.il.Emit(Emit.OpCodes.Ldc_I4, 0);
                this.il.Emit(Emit.OpCodes.Bne_Un, Body);
            }
            this.il.Emit(Emit.OpCodes.Br, Salida);
            this.il.MarkLabel(Body);
            GenStmt(whileLoop.Body);
            this.il.Emit(Emit.OpCodes.Br, Cond);
            this.il.MarkLabel(Salida);
        }
        //*******************
        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("imposible generar: " + stmt.GetType().Name);
        }
    }
예제 #10
0
        private void GenStmt(Stmt stmt)
        {
            if (stmt is Sequence)
            {
                Sequence seq = (Sequence)stmt;
                this.GenStmt(seq.First);
                this.GenStmt(seq.Second);
            }

            else if (stmt is DeclareVar)
            {
                // declare a local
                DeclareVar declare = (DeclareVar)stmt;
                this.symbolTable[declare.Ident] = this.il.DeclareLocal(this.TypeOfExpr(declare.Expr));

                // set the initial value
                Assign assign = new Assign();
                assign.Ident = declare.Ident;
                assign.Expr  = declare.Expr;
                this.GenStmt(assign);
            }

            else if (stmt is Assign)
            {
                Assign assign = (Assign)stmt;

                if (this.typefieldList.ContainsKey(assign.Ident))
                {
                    if (!this.typefieldList[assign.Ident].IsStatic)
                    {
                        this.il.Emit(Emit.OpCodes.Ldarg_0);
                    }
                }


                // if(assign.Ident
                this.GenExpr(assign.Expr, this.TypeOfExpr(assign.Expr));
                this.Store(assign.Ident, this.TypeOfExpr(assign.Expr));
            }
            else if (stmt is Print)
            {
                // the "print" statement is an alias for System.Console.WriteLine.
                // it uses the string case
                this.GenExpr(((Print)stmt).Expr, typeof(string));
                this.il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new System.Type[] { typeof(string) }));
                // this.il.Emit(
            }
            else if (stmt is VoidMethodCall)
            {
                this.GenerteMethodCallCode((MethodCall)((VoidMethodCall)stmt).Expr);
            }

            else if (stmt is IfCondition)
            {
                IfCondition ifCon = (IfCondition)stmt;
                this.GenExpr(ifCon.BooleanExp, /*harcoded for string */ typeof(string));

                Emit.Label endOfIfBlock = this.il.DefineLabel();


                if (((BinExpr)ifCon.BooleanExp).Op == BinOp.EqualTo)
                {
                    this.il.Emit(Emit.OpCodes.Brfalse_S, endOfIfBlock);
                }
                else
                {
                    this.il.Emit(Emit.OpCodes.Brtrue_S, endOfIfBlock);
                }

                this.GenStmt(ifCon.Body);

                this.il.MarkLabel(endOfIfBlock);
                // this.GenerteMethodCallCode((MethodCall)((VoidMethodCall)stmt).Expr);
            }

            else if (stmt is WhileLoop)
            {
                WhileLoop whileLoop = (WhileLoop)stmt;

                Emit.Label whileBodyStart = this.il.DefineLabel();
                Emit.Label whileBodyEnd   = this.il.DefineLabel();
                Emit.Label whileCondition = this.il.DefineLabel();

                this.il.Emit(Emit.OpCodes.Br_S, whileBodyEnd);

                this.il.MarkLabel(whileBodyStart);
                this.GenStmt(whileLoop.Body);
                this.il.MarkLabel(whileBodyEnd);

                this.GenExpr(whileLoop.BooleanExp, /*harcoded for int */ typeof(int));

                if (((BinExpr)whileLoop.BooleanExp).Op == BinOp.EqualTo)
                {
                    this.il.Emit(Emit.OpCodes.Brtrue_S, whileBodyStart);
                }
                else
                {
                    this.il.Emit(Emit.OpCodes.Brfalse_S, whileBodyStart);
                }
            }


            // System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();

            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.GetType().Name);
            }
        }