Esempio n. 1
0
        static LuaArguments EvalNumericFor(NumericFor stat, LuaContext Context, out LuaReturnStatus returned)
        {
            returned.broke    = false;
            returned.returned = false;
            var varList   = EvalExpression(stat.Var, Context);
            var limitList = EvalExpression(stat.Limit, Context);
            var stepList  = EvalExpression(stat.Step, Context);
            var var       = LuaEvents.toNumber(varList[0]);
            var limit     = LuaEvents.toNumber(limitList[0]);
            var step      = LuaEvents.toNumber(stepList[0]);

            if (!(var & limit & step).AsBool())
            {
                throw new LuaException("Error in for loop");
            }
            LuaContext ctx = new LuaContext(Context);

            while ((step > 0 & var <= limit) | (step <= 0 & var >= limit))
            {
                ctx.SetLocal(stat.Variable, var);
                LuaArguments obj = EvalBlock(stat.Block, ctx, out returned);
                if (returned.broke)
                {
                    break;
                }
                if (returned.returned)
                {
                    return(obj);
                }
                var = var + step;
            }
            return(Lua.Return());
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        static LuaObject EvalFunctionDefinition(FunctionDefinition fdef, LuaContext Context)
        {
            LuaObject obj = LuaObject.FromFunction(delegate(LuaArguments args)
            {
                LuaContext ctx = new LuaContext(Context);
                LuaReturnStatus ret;
                for (int i = 0; i < args.Length; i++)
                {
                    if (i < fdef.Arguments.Count)
                    {
                        ctx.SetLocal(fdef.Arguments[i].Name, args[i]);
                    }
                    else
                    {
                        ctx.Varargs.Add(args[i]);
                    }
                }
                return(EvalBlock(fdef.Body, ctx, out ret));
            });

            return(obj);
        }
Esempio n. 4
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);
        }