コード例 #1
0
        static Expression CompileIfStat(Ast.IfStat ifstat, LabelTarget returnTarget, LabelTarget breakTarget, Expression Context)
        {
            var condition = GetAsBool(CompileSingleExpression(ifstat.Condition, Context));
            var block     = CompileBlock(ifstat.Block, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context));

            if (ifstat.ElseIfs.Count == 0)
            {
                if (ifstat.ElseBlock == null)
                {
                    return(Expression.IfThen(condition, block));
                }
                else
                {
                    var elseBlock = CompileBlock(ifstat.ElseBlock, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context));
                    return(Expression.IfThenElse(condition, block, elseBlock));
                }
            }
            else
            {
                Expression b = null;
                for (int i = ifstat.ElseIfs.Count - 1; i >= 0; i--)
                {
                    IfStat branch = ifstat.ElseIfs[i];
                    var    cond   = GetAsBool(CompileSingleExpression(branch.Condition, Context));
                    var    body   = CompileBlock(branch.Block, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context));
                    if (b == null)
                    {
                        if (ifstat.ElseBlock == null)
                        {
                            b = Expression.IfThen(cond, body);
                        }
                        else
                        {
                            var elseBlock = CompileBlock(ifstat.ElseBlock, returnTarget, breakTarget, Expression.New(LuaContext_New_parent, Context));
                            b = Expression.IfThenElse(cond, body, elseBlock);
                        }
                    }
                    else
                    {
                        b = Expression.IfThenElse(cond, body, b);
                    }
                }

                var tree = Expression.IfThenElse(condition, block, b);
                return(tree);
            }
            throw new NotImplementedException();
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: NoitaOmmel/NetLua
        Ast.IfStat ParseIf(ParseTreeNode node)
        {
            if (node.Term.Name == "If")
            {
                Ast.IExpression condition = ParseExpression(node.ChildNodes[1]);
                Ast.Block       block     = ParseBlock(node.ChildNodes[3]);

                Ast.IfStat If = new Ast.IfStat();
                If.Block     = block;
                If.Condition = condition;
                If.ElseIfs   = new List <Ast.IfStat>();
                If.Span      = node.Span;

                ParseTreeNode ElseifNode = node.ChildNodes[4];
                ParseTreeNode ElseNode   = node.ChildNodes[5];

                while (ElseifNode.ChildNodes.Count != 0)
                {
                    var        childnode = ElseifNode.ChildNodes[0];
                    Ast.IfStat elseif    = new Ast.IfStat();
                    elseif.Span      = childnode.Span;
                    elseif.Condition = ParseExpression(childnode.ChildNodes[1]);
                    elseif.Block     = ParseBlock(childnode.ChildNodes[3]);

                    If.ElseIfs.Add(elseif);

                    ElseifNode = childnode.ChildNodes[4];
                }

                if (ElseNode.ChildNodes.Count != 0)
                {
                    If.ElseBlock = ParseBlock(ElseNode.ChildNodes[0].ChildNodes[1]);
                }

                return(If);
            }
            throw new Exception("Invalid If node");
        }
コード例 #3
0
ファイル: Compiler.cs プロジェクト: Myvar/LuaBlock
        public void iterateLogic(List<string> Lines, IfStat ifs)
        {
            var a = TypeToString((ifs.Condition as BinaryExpression).Left);
            var b = TypeToString((ifs.Condition as BinaryExpression).Right);

            Lines.Add("#/scoreboard players test lua " + a + " " + b + " " + b);
        }
コード例 #4
0
ファイル: Parser.cs プロジェクト: chenzuo/NetLua
        Ast.IfStat ParseIf(ParseTreeNode node)
        {
            if (node.Term.Name == "If")
            {
                Ast.IExpression condition = ParseExpression(node.ChildNodes[1]);
                Ast.Block block = ParseBlock(node.ChildNodes[3]);

                Ast.IfStat If = new Ast.IfStat();
                If.Block = block;
                If.Condition = condition;
                If.ElseIfs = new List<Ast.IfStat>();

                ParseTreeNode ElseifNode = node.ChildNodes[4];
                ParseTreeNode ElseNode = node.ChildNodes[5];

                while (ElseifNode.ChildNodes.Count != 0)
                {
                    var childnode = ElseifNode.ChildNodes[0];
                    Ast.IfStat elseif = new Ast.IfStat();
                    elseif.Condition = ParseExpression(childnode.ChildNodes[1]);
                    elseif.Block = ParseBlock(childnode.ChildNodes[3]);

                    If.ElseIfs.Add(elseif);

                    ElseifNode = childnode.ChildNodes[4];
                }

                if (ElseNode.ChildNodes.Count != 0)
                    If.ElseBlock = ParseBlock(ElseNode.ChildNodes[0].ChildNodes[1]);

                return If;
            }
            throw new Exception("Invalid If node");
        }