public IfElseStatement(LuaFunction checkstate_function)
 {
     conditionCheckFunction = checkstate_function;
     executionBlock = null;
     nextStatement = null;
     executingStatement = null;
 }
        public override bool Execute()
        {
            if (!stateChecked)
            {
                stateChecked = true;

                if (checkState())
                {
                    executingStatement = this;
                }
                else
                {
                    IfElseStatement ies = nextStatement;
                    while( ies != null && executingStatement == null )
                    {
                        if (!ies.checkState())
                        {
                            ies = ies.nextStatement;
                        }
                        else
                        {
                            executingStatement = ies;
                        }
                    }
                }
            }

            if (executingStatement != null)
            {
                if (executingStatement.executionBlock == null)
                    return true;

                while (positionInExecList != executingStatement.executionBlock.Count && executingStatement.executionBlock[positionInExecList].Execute())
                {
                    positionInExecList++;
                }
                if (positionInExecList == executingStatement.executionBlock.Count)
                {
                    //we did it(whole queue)!
                    return true;
                }
            }
            else
            {
                //There is nothing to do
                return true;
            }

            return false;
        }
        //back to state before execution
        public override void Reset()
        {
            base.Reset();
            stateChecked = false;
            executingStatement = null;

            IfElseStatement ies = nextStatement;
            while (ies != null)
            {
                ies.Reset();
                ies = ies.nextStatement;
            }

            if (executionBlock == null)
                return;

            foreach (ConditionalStatement cs in executionBlock)
            {
                cs.Reset();
            }
        }
 public void SetNextConditionalStatement(IfElseStatement cs)
 {
     nextStatement = cs;
 }
        // Use it only for user-typed scripts
        public static List<ConditionalStatement> RegisterUserIngameScript(string user_defined_script)
        {
            List<ConditionalStatement> ret_val = new List<ConditionalStatement>();
            List<Pair<LuaFunction, LuaFunction>> ncs_block = new List<Pair<LuaFunction,LuaFunction>>();
            Stack<ConditionalStatement> block_depth = new Stack<ConditionalStatement>();

            Pair<string, LuaFunction> blocking_func_pair = null;
            string func_buffer = "";
            string[] loc = user_defined_script.Split(new string[] { "\r\n", "\n" }, System.StringSplitOptions.None);

            Regex re_if = new Regex("(\\s+if\\s|^if\\s)(?<ConditionExp>[a-zA-Z0-9\\s=+\\/*-<>~]*)\\sthen($|\\s)");
            Regex re_elseif = new Regex("(\\s+elseif\\s|^elseif\\s)(?<ConditionExp>[a-zA-Z0-9\\s=+\\/*-<>~]*)\\sthen($|\\s)");
            Regex re_else = new Regex("(\\s+|^)else($|\\s)");
            Regex re_end = new Regex("(\\s+|^)end($|\\s)");
            Regex re_while = new Regex("(\\s+while\\s|^while\\s)(?<ConditionExp>[a-zA-Z0-9\\s=+\\/*-<>~]*)\\sdo($|\\s)");

            foreach (string line in loc)
            {
                //if match one of the following: [if elseif else end while] then do:
                if (re_if.IsMatch(line) || re_elseif.IsMatch(line) || re_else.IsMatch(line) || re_end.IsMatch(line) || re_while.IsMatch(line))
                {
                    LuaFunction lf = (func_buffer.Length > 3) ? GenerateUserFunction(func_buffer) : null;
                    if (lf != null)
                    {
                        ncs_block.Add(new Pair<LuaFunction, LuaFunction>(lf, null));
                    }

                    if (ncs_block.Count > 0)
                    {
                        if (block_depth.Count > 0 && block_depth.Peek() is IfElseStatement)
                        {
                            IfElseStatement ies = (IfElseStatement)block_depth.Peek();
                            ies.AddCSToExecutionBlock(new NonconditionalStatement(ncs_block));
                        }
                        else if (block_depth.Count > 0 && block_depth.Peek() is WhileStatement)
                        {
                            WhileStatement ws = (WhileStatement)block_depth.Peek();
                            ws.AddCSToExecutionBlock(new NonconditionalStatement(ncs_block));
                        }
                        else if (block_depth.Count == 0)
                        {
                            ret_val.Add(new NonconditionalStatement(ncs_block));
                        }
                        else
                        {
                            Debug.LogError("[LUA:RUIS] Lel script.");
                        }
                        ncs_block = new List<Pair<LuaFunction, LuaFunction>>();
                    }
                    func_buffer = "";
                }

                //check for IF statement
                if (re_if.IsMatch(line))
                {
                    //IF found!

                    //Start a statement
                    LuaFunction cclf = GenerateUserFunction("if " + re_if.Match(line).Groups["ConditionExp"].Value + "then return true\n else return false\n end");
                    IfElseStatement new_statement = new IfElseStatement(cclf);
                    if (block_depth.Count == 0)
                    {
                        ret_val.Add(new_statement);
                    }
                    else
                    {
                        if (block_depth.Peek() is IfElseStatement)
                        {
                            IfElseStatement ies = (IfElseStatement)block_depth.Peek();
                            ies.AddCSToExecutionBlock(new_statement);
                        }
                        else if (block_depth.Peek() is WhileStatement)
                        {
                            WhileStatement ws = (WhileStatement)block_depth.Peek();
                            ws.AddCSToExecutionBlock(new_statement);
                        }
                    }
                    block_depth.Push(new_statement);
                }
                //check for ELSEIF statement
                else if (re_elseif.IsMatch(line))
                {
                    //ELSEIF found!
                    LuaFunction cclf = GenerateUserFunction("if " + re_elseif.Match(line).Groups["ConditionExp"].Value + "then return true\n else return false\n end");
                    IfElseStatement new_statement = new IfElseStatement(cclf);

                    if (block_depth.Peek() is IfElseStatement)
                    {
                        IfElseStatement ies = (IfElseStatement)block_depth.Pop();
                        ies.SetNextConditionalStatement(new_statement);
                        block_depth.Push(new_statement);
                    }
                }
                //check for ELSE statement
                else if (re_else.IsMatch(line))
                {
                    //ELSE found!
                    IfElseStatement new_statement = new IfElseStatement(null);

                    if (block_depth.Peek() is IfElseStatement)
                    {
                        IfElseStatement ies = (IfElseStatement)block_depth.Pop();
                        ies.SetNextConditionalStatement(new_statement);
                        block_depth.Push(new_statement);
                    }
                }
                //check for while
                else if (re_while.IsMatch(line))
                {
                    //WHILE found!
                    LuaFunction cclf = GenerateUserFunction("if " + re_while.Match(line).Groups["ConditionExp"].Value + "then return true\n else return false\n end");
                    WhileStatement new_statement = new WhileStatement(cclf);

                    if (block_depth.Count == 0)
                    {
                        ret_val.Add(new_statement);
                    }
                    else
                    {
                        if (block_depth.Peek() is IfElseStatement)
                        {
                            IfElseStatement ies = (IfElseStatement)block_depth.Peek();
                            ies.AddCSToExecutionBlock(new_statement);
                        }
                        else if (block_depth.Peek() is WhileStatement)
                        {
                            WhileStatement ws = (WhileStatement)block_depth.Peek();
                            ws.AddCSToExecutionBlock(new_statement);
                        }
                    }
                    block_depth.Push(new_statement);
                }
                //check for END ✞
                else if (re_end.IsMatch(line))
                {
                    //E.N.D.
                    block_depth.Pop();
                }
                else
                {
                    bool found_blocking_func = false;
                    foreach (Pair<string, LuaFunction> f in blockingFunctions)
                    {
                        //regexp byłyby wydajniejszy?
                        string[] variants = {f.First + " ", f.First + "\t", f.First + "("};
                        foreach(string v in variants)
                        {
                            if (line.Contains(v))
                            {
                                found_blocking_func = true;
                                blocking_func_pair = f;
                                break;
                            }
                        }

                        if (found_blocking_func)
                            break;

                    }

                    func_buffer += line + "\n";

                    if (found_blocking_func)
                    {
                        try
                        {
                            ncs_block.Add(new Pair<LuaFunction, LuaFunction>(GenerateUserFunction(func_buffer), blocking_func_pair.Second));

                            func_buffer = "";
                        }
                        catch (NLua.Exceptions.LuaException e)
                        {
                            throw e;
                        }
                    }
                }
            }

            if (func_buffer.Length > 2)
            {
                try
                {
                    ncs_block.Add(new Pair<LuaFunction, LuaFunction>(GenerateUserFunction(func_buffer), null));
                }
                catch (NLua.Exceptions.LuaException e)
                {
                    throw e;
                }
            }

            if (ncs_block.Count > 0)
                ret_val.Add(new NonconditionalStatement(ncs_block));

            return ret_val;
        }