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

            var stack      = 0;
            var identifier = context.Identifier(Name);

            if (!context.Compiler.Options.UseImplicitGlobals && identifier == null)
            {
                throw new MondCompilerException(FileName, Line, CompilerError.UndefinedIdentifier, Name);
            }

            if (identifier == null)
            {
                stack += context.LoadGlobal();
                stack += context.LoadField(context.String(Name));
            }
            else
            {
                stack += context.Load(identifier);
            }

            CheckStack(stack, 1);
            return(stack);
        }
Пример #2
0
        public override int Compile(FunctionContext context)
        {
            var stack      = 0;
            var identifier = context.Identifier(Name);

            // anything that starts with 'op_' is always an implicit global
            if (!context.Compiler.Options.UseImplicitGlobals && identifier == null && !Name.StartsWith("op_"))
            {
                throw new MondCompilerException(this, CompilerError.UndefinedIdentifier, Name);
            }

            context.Position(Token); // debug info

            if (identifier == null)
            {
                stack += context.LoadGlobal();
                stack += context.LoadField(context.String(Name));
            }
            else
            {
                stack += context.Load(identifier);
            }

            CheckStack(stack, 1);
            return(stack);
        }
Пример #3
0
        public int CompileStore(FunctionContext context)
        {
            var stack      = 0;
            var identifier = context.Identifier(Name);

            if (!context.Compiler.Options.UseImplicitGlobals && identifier == null)
            {
                throw new MondCompilerException(FileName, Line, CompilerError.UndefinedIdentifier, Name);
            }

            if (identifier == null)
            {
                stack += context.LoadGlobal();
                stack += context.StoreField(context.String(Name));
            }
            else
            {
                if (identifier.IsReadOnly)
                {
                    throw new MondCompilerException(FileName, Line, CompilerError.CantModifyReadonlyVar, Name);
                }

                stack += context.Store(identifier);
            }

            return(stack);
        }
Пример #4
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;

            foreach (var field in Fields)
            {
                var name = field.Alias ?? field.Name;

                stack += context.Dup();
                stack += context.LoadField(context.String(field.Name));

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

                    stack += context.Store(context.Identifier(name));
                }
            }

            stack += context.Drop();

            CheckStack(stack, 0);
            return(-1);
        }
Пример #5
0
        public int CompileStore(FunctionContext context)
        {
            var stack      = 0;
            var identifier = context.Identifier(Name);

            if (!context.Compiler.Options.UseImplicitGlobals && identifier == null && !Name.StartsWith("op_"))
            {
                throw new MondCompilerException(this, CompilerError.UndefinedIdentifier, Name);
            }

            if (identifier == null)
            {
                stack += context.LoadGlobal();

                context.Position(Token); // debug info
                stack += context.StoreField(context.String(Name));
            }
            else
            {
                if (identifier.IsReadOnly)
                {
                    throw new MondCompilerException(this, CompilerError.CantModifyReadonlyVar, Name);
                }

                context.Position(Token); // debug info
                stack += context.Store(identifier);
            }

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

            if (context.Name != null)
            {
                var callExpression = Value as CallExpression;
                if (callExpression != null)
                {
                    var identifierExpression = callExpression.Method as IdentifierExpression;
                    if (identifierExpression != null && context.Identifier(identifierExpression.Name) == context.Name)
                    {
                        callExpression.CompileTailCall(context);
                        return 0;
                    }
                }
            }

            if (Value != null)
                CompileCheck(context, Value, 1);
            else
                context.LoadUndefined();

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

            var isStatement = Parent is IBlockStatementExpression;

            if (Name == null && isStatement)
                throw new MondCompilerException(FileName, Line, "Function is never used");

            IdentifierOperand identifier = null;

            if (Name != null)
            {
                if (!context.DefineIdentifier(Name, true))
                    throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", Name);

                identifier = context.Identifier(Name);
            }

            var functionContext = context.MakeFunction(Name);
            functionContext.Function(FileName, Name);

            context.PushFrame();

            for (var i = 0; i < Arguments.Count; i++)
            {
                var name = Arguments[i];

                if (!functionContext.DefineArgument(i, name))
                    throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", name);
            }

            functionContext.Bind(functionContext.Label);
            functionContext.Enter();
            Block.Compile(functionContext);
            functionContext.LoadUndefined();
            functionContext.Return();

            context.PopFrame();

            context.Closure(functionContext.Label);

            if (identifier != null)
            {
                if (!isStatement) // statements should leave nothing on the stack
                    context.Dup();

                context.Store(identifier);

                if (isStatement)
                    return 0;
            }

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

            var enumerator = context.DefineInternal("enumerator", true);

            var stack = 0;
            var start = context.MakeLabel("foreachStart");
            var end   = context.MakeLabel("foreachEnd");

            // set enumerator
            stack += Expression.Compile(context);
            stack += context.LoadField(context.String("getEnumerator"));
            stack += context.Call(0);
            stack += context.Store(enumerator);

            // loop while moveNext returns true
            stack += context.Bind(start);
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("moveNext"));
            stack += context.Call(0);
            stack += context.JumpFalse(end);

            // loop body
            context.PushScope();
            context.PushLoop(start, end);

            if (!context.DefineIdentifier(Identifier))
            {
                throw new MondCompilerException(FileName, Line, CompilerError.IdentifierAlreadyDefined, Identifier);
            }

            var identifier = context.Identifier(Identifier);

            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("current"));
            stack += context.Store(identifier);

            stack += Block.Compile(context);
            stack += context.Jump(start);

            context.PopLoop();
            context.PopScope();

            // after loop
            stack += context.Bind(end);
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("dispose"));
            stack += context.Call(0);
            stack += context.Drop();

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

            var sequenceContext = context.Root as SequenceBodyContext;

            if (sequenceContext == null)
            {
                throw new MondCompilerException(this, CompilerError.YieldInFun);
            }

            var state      = sequenceContext.SequenceBody.State;
            var enumerable = sequenceContext.SequenceBody.Enumerable;

            var stack          = 0;
            var nextState      = sequenceContext.SequenceBody.NextState;
            var nextStateLabel = sequenceContext.SequenceBody.MakeStateLabel(context);

            stack += Value.Compile(context);
            stack += context.Load(enumerable);
            stack += context.StoreField(context.String("current"));

            stack += context.Load(context.Number(nextState)); // set resume point
            stack += context.Store(state);

            stack += context.StoreState(sequenceContext.LocalIndex - 1); // save locals
            stack += context.LoadTrue();
            stack += context.Return();

            stack += context.Bind(nextStateLabel);
            stack += context.LoadState(sequenceContext.LocalIndex - 1); // load locals

            if (!(Parent is IBlockExpression))
            {
                stack += context.Load(context.Identifier("#input"));
                CheckStack(stack, 1);
                return(stack);
            }

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

            var stack          = 0;
            var shouldBeGlobal = context.FrameIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal;

            foreach (var declaration in Declarations)
            {
                var name = declaration.Name;

                if (!shouldBeGlobal)
                {
                    if (!context.DefineIdentifier(name, IsReadOnly))
                    {
                        throw new MondCompilerException(FileName, Line, CompilerError.IdentifierAlreadyDefined, name);
                    }
                }

                if (declaration.Initializer == null)
                {
                    continue;
                }

                if (!shouldBeGlobal)
                {
                    var identifier = context.Identifier(name);

                    stack += declaration.Initializer.Compile(context);
                    stack += context.Store(identifier);
                }
                else
                {
                    stack += declaration.Initializer.Compile(context);
                    stack += context.LoadGlobal();
                    stack += context.StoreField(context.String(name));
                }
            }

            CheckStack(stack, 0);
            return(stack);
        }
Пример #11
0
        public void CompileStore(FunctionContext context)
        {
            var identifier = context.Identifier(Name);

            /*if (identifier == null)
                throw new MondCompilerException(FileName, Line, "Undefined identifier '{0}'", Name);*/

            if (identifier == null)
            {
                context.LoadGlobal();
                context.StoreField(context.String(Name));
            }
            else
            {
                if (identifier.IsReadOnly)
                    throw new MondCompilerException(FileName, Line, "Can not modify '{0}' because it is readonly", Name);

                context.Store(identifier);
            }
        }
Пример #12
0
        public override int Compile(FunctionContext context)
        {
            context.Line(FileName, Line);

            var identifier = context.Identifier(Name);

            /*if (identifier == null)
                throw new MondCompilerException(FileName, Line, "Undefined identifier '{0}'", Name);*/

            if (identifier == null)
            {
                context.LoadGlobal();
                context.LoadField(context.String(Name));
            }
            else
            {
                context.Load(identifier);
            }

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

            if (context is SequenceBodyContext)
            {
                throw new MondCompilerException(FileName, Line, CompilerError.ReturnInSeq);
            }

            var stack = 0;

            if (context.AssignedName != null)
            {
                var callExpression = Value as CallExpression;
                if (callExpression != null)
                {
                    var identifierExpression = callExpression.Method as IdentifierExpression;
                    if (identifierExpression != null && context.Identifier(identifierExpression.Name) == context.AssignedName)
                    {
                        stack += callExpression.CompileTailCall(context);
                        CheckStack(stack, 0);
                        return(stack);
                    }
                }
            }

            if (Value != null)
            {
                stack += Value.Compile(context);
            }
            else
            {
                stack += context.LoadUndefined();
            }

            stack += context.Return();

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

            foreach (var declaration in Declarations)
            {
                var name = declaration.Name;

                if (!context.DefineIdentifier(name))
                    throw new MondCompilerException(FileName, Line, "Identifier '{0}' was previously defined in this scope", name);

                if (declaration.Initializer != null)
                {
                    var identifier = context.Identifier(name);

                    CompileCheck(context, declaration.Initializer, 1);

                    context.Store(identifier);
                }
            }

            return 0;
        }
Пример #15
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);
        }
Пример #16
0
        public override int Compile(FunctionContext context)
        {
            context.Position(Token);

            var stack = 0;
            var start = context.MakeLabel("foreachStart");
            var cont  = context.MakeLabel("foreachContinue");
            var brk   = context.MakeLabel("foreachBreak");
            var end   = context.MakeLabel("foreachEnd");

            var containsFunction = new LoopContainsFunctionVisitor();

            Block.Accept(containsFunction);

            var enumerator = context.DefineInternal("enumerator", true);

            // set enumerator
            context.Statement(Expression);
            stack += Expression.Compile(context);
            stack += context.LoadField(context.String("getEnumerator"));
            stack += context.Call(0, new List <ImmediateOperand>());
            stack += context.Store(enumerator);

            var loopContext = containsFunction.Value ? new LoopContext(context) : context;

            // loop body
            loopContext.PushScope();
            loopContext.PushLoop(containsFunction.Value ? cont : start, containsFunction.Value ? brk : end);

            IdentifierOperand identifier;

            if (DestructureExpression != null)
            {
                identifier = context.DefineInternal(Identifier, true);
            }
            else
            {
                // create the loop variable outside of the loop context (but inside of its scope!)
                if (!context.DefineIdentifier(Identifier))
                {
                    throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, Identifier);
                }

                identifier = context.Identifier(Identifier);
            }
            stack += loopContext.Bind(start); // continue (without function)

            if (containsFunction.Value)
            {
                stack += loopContext.Enter();
            }

            // loop while moveNext returns true
            context.Statement(InToken, InToken);
            stack += loopContext.Load(enumerator);
            stack += loopContext.LoadField(context.String("moveNext"));
            stack += loopContext.Call(0, new List <ImmediateOperand>());
            stack += loopContext.JumpFalse(containsFunction.Value ? brk : end);

            stack += loopContext.Load(enumerator);
            stack += loopContext.LoadField(context.String("current"));
            stack += loopContext.Store(identifier);

            if (DestructureExpression != null)
            {
                stack += loopContext.Load(identifier);
                stack += DestructureExpression.Compile(loopContext);
            }

            stack += Block.Compile(loopContext);

            if (containsFunction.Value)
            {
                stack += loopContext.Bind(cont); // continue (with function)
                stack += loopContext.Leave();
            }

            stack += loopContext.Jump(start);

            if (containsFunction.Value)
            {
                stack += loopContext.Bind(brk); // break (with function)
                stack += loopContext.Leave();
            }

            loopContext.PopLoop();
            loopContext.PopScope();

            // after loop
            stack += context.Bind(end); // break (without function)
            stack += context.Load(enumerator);
            stack += context.LoadField(context.String("dispose"));
            stack += context.Call(0, new List <ImmediateOperand>());
            stack += context.Drop();

            CheckStack(stack, 0);
            return(stack);
        }
Пример #17
0
        public override int Compile(FunctionContext context)
        {
            var isStatement    = Parent is IBlockExpression;
            var shouldBeGlobal = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal;
            var shouldStore    = Name != null;

            IdentifierOperand identifier = null;

            if (shouldStore && !shouldBeGlobal)
            {
                if (!context.DefineIdentifier(Name, true))
                {
                    throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, Name);
                }

                identifier = context.Identifier(Name);
            }

            // compile body
            var functionContext = context.MakeFunction(Name ?? DebugName);

            functionContext.Function(functionContext.FullName);
            functionContext.Position(Token);
            functionContext.PushScope();

            for (var i = 0; i < Arguments.Count; i++)
            {
                var name = Arguments[i];

                if (!functionContext.DefineArgument(i, name))
                {
                    throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, name);
                }
            }

            if (OtherArguments != null && !functionContext.DefineArgument(Arguments.Count, OtherArguments))
            {
                throw new MondCompilerException(this, CompilerError.IdentifierAlreadyDefined, OtherArguments);
            }

            CompileBody(functionContext);
            functionContext.PopScope();

            // assign result
            var stack = 0;

            context.Position(Token); // debug info
            stack += context.Closure(functionContext.Label);

            if (shouldStore)
            {
                if (!isStatement) // statements should leave nothing on the stack
                {
                    stack += context.Dup();
                }

                if (!shouldBeGlobal)
                {
                    stack += context.Store(identifier);
                }
                else
                {
                    stack += context.LoadGlobal();
                    stack += context.StoreField(context.String(Name));
                }

                if (isStatement)
                {
                    CheckStack(stack, 0);
                    return(stack);
                }
            }

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