Пример #1
0
        public int CompileLoad(FunctionContext context)
        {
            var stack = 0;

            context.Position(Token); // debug info
            stack += context.LoadArray();

            return(stack);
        }
Пример #2
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            CompileCheck(context, Left, 1);
            CompileCheck(context, Index, 1);
            context.LoadArray();
            return 1;
        }
Пример #3
0
        public override int Compile(FunctionContext context)
        {
            var stack = 0;

            stack += Left.Compile(context);
            stack += Index.Compile(context);

            context.Position(Token); // debug info
            stack += context.LoadArray();

            CheckStack(stack, 1);
            return(stack);
        }
Пример #4
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var stack = 0;

            stack += Left.Compile(context);
            stack += Index.Compile(context);
            stack += context.LoadArray();

            CheckStack(stack, 1);
            return(stack);
        }
Пример #5
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack  = Initializer?.Compile(context) ?? 1;
            var global = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal;

            var hasSlice = false;
            var headSize = 0;
            var tailSize = 0;

            foreach (var index in Indices)
            {
                if (index.IsSlice)
                {
                    if (hasSlice)
                    {
                        throw new InvalidOperationException($"Multiple slices in {nameof(DestructuredArrayExpression)}");
                    }

                    hasSlice = true;
                }
                else if (hasSlice)
                {
                    tailSize++;
                }
                else
                {
                    headSize++;
                }
            }

            var fixedSize = headSize + tailSize;

            stack += context.Dup();
            stack += context.LoadField(context.String("length"));
            stack += context.Call(0, new List <ImmediateOperand>());

            var inTail = false;
            var fixedI = 0;

            for (var i = 0; i < Indices.Count; i++)
            {
                var index    = Indices[i];
                var assign   = context.MakeLabel("arrayDestructureAssign");
                var destruct = context.MakeLabel("arrayDestructureIndex");

                if (index.IsSlice)
                {
                    inTail = true;
                }

                if (i < Indices.Count - 1)
                {
                    stack += context.Dup2(); // -> init.length(), init
                }
                stack += context.Load(context.Number(index.IsSlice ? fixedSize : fixedI));
                stack += context.BinaryOperation(TokenType.GreaterThan);
                stack += context.JumpTrue(destruct);
                stack += context.Drop(); // drops initializer
                stack += index.IsSlice ? context.NewArray(0) : context.LoadUndefined();
                stack += context.Jump(assign);

                stack += context.Bind(destruct);

                if (index.IsSlice)
                {
                    stack += context.Load(context.Number(fixedI));
                    stack += context.Load(context.Number(-tailSize - 1));
                    stack += context.LoadUndefined();
                    stack += context.Slice();
                }
                else
                {
                    stack += context.Load(context.Number(inTail ? fixedI - fixedSize : fixedI));
                    stack += context.LoadArray();
                }

                stack += context.Bind(assign);

                if (global)
                {
                    stack += context.LoadGlobal();
                    stack += context.StoreField(context.String(index.Name));
                }
                else
                {
                    if (!context.DefineIdentifier(index.Name, IsReadOnly))
                    {
                        throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, index.Name);
                    }

                    stack += context.Store(context.Identifier(index.Name));
                }

                if (!index.IsSlice)
                {
                    fixedI++;
                }
            }

            CheckStack(stack, 0);
            return(-1);
        }