示例#1
0
        static LuaArguments EvalGenericFor(GenericFor stat, LuaContext Context, out LuaReturnStatus returned)
        {
            returned.broke    = false;
            returned.returned = false;

            LuaArguments args = null;

            foreach (IExpression expr in stat.Expressions)
            {
                if (args == null)
                {
                    args = EvalExpression(expr, Context);
                }
                else
                {
                    args.Concat(EvalExpression(expr, Context));
                }
            }
            LuaObject  f = args[0], s = args[1], var = args[2];
            LuaContext ctx = new LuaContext(Context);

            while (true)
            {
                LuaArguments res = f.Call(s, var);
                for (int i = 0; i < stat.Variables.Count; i++)
                {
                    ctx.SetLocal(stat.Variables[i], res[i]);
                }
                if (res[0].IsNil)
                {
                    break;
                }
                var = res[0];
                LuaArguments obj = EvalBlock(stat.Block, ctx, out returned);
                if (returned.broke)
                {
                    break;
                }
                if (returned.returned)
                {
                    return(obj);
                }
            }

            return(Lua.Return());
        }
示例#2
0
        internal static LuaArguments EvalBlock(Block Block, LuaContext Context, out LuaReturnStatus returned)
        {
            returned.broke    = false;
            returned.returned = false;
            LuaArguments obj = new LuaObject[] { LuaObject.Nil };

            foreach (IStatement stat in Block.Statements)
            {
                if (stat is Assignment)
                {
                    Assignment   assign = stat as Assignment;
                    LuaArguments values = null;
                    foreach (IExpression expr in assign.Expressions)
                    {
                        if (values == null)
                        {
                            values = EvalExpression(expr, Context);
                        }
                        else
                        {
                            values.Concat(EvalExpression(expr, Context));
                        }
                    }
                    for (int i = 0; i < assign.Variables.Count; i++)
                    {
                        SetAssignable(assign.Variables[i], values[i], Context);
                    }
                }
                else if (stat is LocalAssignment)
                {
                    LocalAssignment assign = stat as LocalAssignment;
                    LuaArguments    values = null;
                    foreach (IExpression expr in assign.Values)
                    {
                        if (values == null)
                        {
                            values = EvalExpression(expr, Context);
                        }
                        else
                        {
                            values.Concat(EvalExpression(expr, Context));
                        }
                    }
                    for (int i = 0; i < assign.Names.Count; i++)
                    {
                        Context.SetLocal(assign.Names[i], values[i]);
                    }
                }
                else if (stat is ReturnStat)
                {
                    ReturnStat ret = stat as ReturnStat;
                    returned.returned = true;
                    List <LuaObject> values = new List <LuaObject>();
                    int i = 0;
                    foreach (IExpression expr in ret.Expressions)
                    {
                        if (i == ret.Expressions.Count - 1)
                        {
                            values.AddRange(EvalExpression(expr, Context));
                        }
                        else
                        {
                            values.Add(EvalExpression(expr, Context)[0]);
                        }
                        i++;
                    }
                    return(values.ToArray());
                }
                else if (stat is FunctionCall)
                {
                    FunctionCall call = stat as FunctionCall;
                    EvalFunctionCall(call, Context);
                }
                else if (stat is Block)
                {
                    Block      block = stat as Block;
                    LuaContext ctx   = new LuaContext(Context);
                    obj = EvalBlock(block, ctx, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
                else if (stat is IfStat)
                {
                    obj = EvalIf(stat as IfStat, Context, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
                else if (stat is WhileStat)
                {
                    obj = EvalWhile(stat as WhileStat, Context, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
                else if (stat is RepeatStat)
                {
                    obj = EvalRepeat(stat as RepeatStat, Context, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
                else if (stat is BreakStat)
                {
                    returned.returned = false;
                    returned.broke    = true;
                    return(Lua.Return(LuaObject.Nil));
                }
                else if (stat is NumericFor)
                {
                    obj = EvalNumericFor(stat as NumericFor, Context, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
                else
                {
                    obj = EvalGenericFor(stat as GenericFor, Context, out returned);
                    if (returned.returned)
                    {
                        return(obj);
                    }
                }
            }

            return(obj);
        }