Exemple #1
0
        public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, OpChain opChain, bool outputUsed)
        {
            if (!outputUsed)
            {
                if (opChain.OpToken.Value == "==")
                {
                    throw new ParserException(opChain.OpToken, "'==' cannot be used like this. Did you mean to use just a single '=' instead?");
                }
                throw new ParserException(opChain, "This expression isn't valid here.");
            }

            bcc.CompileExpressionList(parser, buffer, new Expression[] { opChain.Left, opChain.Right }, true);


            switch (opChain.OpTEMP)
            {
            case Ops.EQUALS:
            case Ops.NOT_EQUALS:
                buffer.Add(opChain.OpToken, OpCode.EQUALS, opChain.OpTEMP == Ops.EQUALS ? 0 : 1);
                break;

            default:
                buffer.Add(opChain.OpToken, OpCode.BINARY_OP, (int)opChain.OpTEMP);
                break;
            }

            if (!outputUsed)
            {
                buffer.Add(null, OpCode.POP);
            }
        }
        public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, DictionaryDefinition dictDef, bool outputUsed)
        {
            if (!outputUsed)
            {
                throw new ParserException(dictDef, "Cannot have a dictionary all by itself.");
            }

            int itemCount = dictDef.Keys.Length;
            List <Expression> expressionList = new List <Expression>();

            for (int i = 0; i < itemCount; ++i)
            {
                expressionList.Add(dictDef.Keys[i]);
                expressionList.Add(dictDef.Values[i]);
            }

            bcc.CompileExpressionList(parser, buffer, expressionList, true);

            List <int> args = new List <int>();

            args.Add(itemCount);
            args.Add(0);
            if (dictDef.CompilationScope.IsStaticallyTyped)
            {
                CastEncoder.EncodeTypeInfoToIntBuffer(args, dictDef.ResolvedKeyType, false);
                args[1] = args.Count;
                CastEncoder.EncodeTypeInfoToIntBuffer(args, dictDef.ResolvedValueType, false);
            }

            buffer.Add(dictDef.FirstToken, OpCode.DEF_DICTIONARY, args.ToArray());
        }
Exemple #3
0
        public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, Instantiate instantiate, bool outputUsed)
        {
            ClassDefinition       cd          = instantiate.Class;
            ConstructorDefinition constructor = cd.Constructor;

            bcc.CompileExpressionList(parser, buffer, instantiate.Args, true);
            buffer.Add(instantiate.NameToken,
                       OpCode.CALL_FUNCTION,
                       (int)FunctionInvocationType.CONSTRUCTOR,
                       instantiate.Args.Length,
                       constructor.FunctionID,
                       outputUsed ? 1 : 0,
                       cd.ClassID);
        }
Exemple #4
0
        public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, ConstructorDefinition constructor, ByteBuffer complexFieldInitializers)
        {
            ByteBuffer tBuffer = new ByteBuffer();

            ClassDefinition cd = (ClassDefinition)constructor.Owner;

            List <int> offsetsForOptionalArgs = new List <int>();

            FunctionDefinitionEncoder.CompileFunctionArgs(bcc, parser, tBuffer, constructor.ArgNames, constructor.DefaultValues, offsetsForOptionalArgs, constructor.ArgLocalIds);

            int minArgs = 0;
            int maxArgs = constructor.ArgNames.Length;

            for (int i = 0; i < constructor.ArgNames.Length; ++i)
            {
                if (constructor.DefaultValues[i] == null)
                {
                    minArgs++;
                }
                else
                {
                    break;
                }
            }

            if (constructor.BaseToken != null)
            {
                bcc.CompileExpressionList(parser, tBuffer, constructor.BaseArgs, true);
                tBuffer.Add(
                    constructor.BaseToken,
                    OpCode.CALL_FUNCTION,
                    (int)FunctionInvocationType.BASE_CONSTRUCTOR,
                    constructor.BaseArgs.Length,
                    cd.BaseClass.Constructor.FunctionID,
                    0,
                    cd.BaseClass.ClassID);
            }

            if (complexFieldInitializers != null)
            {
                tBuffer.Concat(complexFieldInitializers);
            }

            bcc.Compile(parser, tBuffer, constructor.Code);
            tBuffer.Add(null, OpCode.RETURN, 0);

            List <int> args = new List <int>()
            {
                constructor.FunctionID,
                -1,
                minArgs,
                maxArgs,
                constructor.Modifiers.HasStatic ? 4 : 3,
                cd.ClassID,
                constructor.LocalScopeSize,
                tBuffer.Size,
                offsetsForOptionalArgs.Count,
            };

            args.AddRange(offsetsForOptionalArgs);

            buffer.Add(constructor.FirstToken, OpCode.FUNCTION_DEFINITION, "<constructor>", args.ToArray());
            buffer.Concat(tBuffer);
        }
Exemple #5
0
        public static void Compile(ByteCodeCompiler bcc, ParserContext parser, ByteBuffer buffer, FunctionCall funCall, bool outputUsed)
        {
            bool argCountIsNegativeOne       = false;
            FunctionDefinition ownerFunction = funCall.Owner as FunctionDefinition;

            if (ownerFunction != null &&
                ownerFunction.NameToken.Value == "_LIB_CORE_invoke" &&
                ownerFunction.FileScope.CompilationScope.Dependencies.Length == 0)
            {
                argCountIsNegativeOne = true;
            }

            Expression root = funCall.Root;

            if (root is FunctionReference)
            {
                FunctionReference  verifiedFunction = (FunctionReference)root;
                FunctionDefinition fd = verifiedFunction.FunctionDefinition;

                if (parser.InlinableLibraryFunctions.Contains(fd))
                {
                    CompileInlinedLibraryFunctionCall(bcc, parser, buffer, funCall, fd, outputUsed);
                }
                else
                {
                    bcc.CompileExpressionList(parser, buffer, funCall.Args, true);
                    if (fd.Owner is ClassDefinition)
                    {
                        ClassDefinition cd = (ClassDefinition)fd.Owner;
                        if (fd.Modifiers.HasStatic)
                        {
                            buffer.Add(
                                funCall.ParenToken,
                                OpCode.CALL_FUNCTION,
                                (int)FunctionInvocationType.STATIC_METHOD,
                                funCall.Args.Length,
                                fd.FunctionID,
                                outputUsed ? 1 : 0,
                                cd.ClassID);
                        }
                        else
                        {
                            buffer.Add(
                                funCall.ParenToken,
                                OpCode.CALL_FUNCTION,
                                (int)FunctionInvocationType.LOCAL_METHOD,
                                funCall.Args.Length,
                                fd.FunctionID,
                                outputUsed ? 1 : 0,
                                cd.ClassID,
                                verifiedFunction.FunctionDefinition.MemberID);
                        }
                    }
                    else
                    {
                        // vanilla function
                        buffer.Add(
                            funCall.ParenToken,
                            OpCode.CALL_FUNCTION,
                            (int)FunctionInvocationType.NORMAL_FUNCTION,
                            funCall.Args.Length,
                            fd.FunctionID,
                            outputUsed ? 1 : 0,
                            0);
                    }
                }
            }
            else if (root is DotField)
            {
                DotField   ds           = (DotField)root;
                Expression dotRoot      = ds.Root;
                int        globalNameId = parser.GetId(ds.FieldToken.Value);
                bcc.CompileExpression(parser, buffer, dotRoot, true);
                bcc.CompileExpressionList(parser, buffer, funCall.Args, true);
                int localeId = parser.GetLocaleId(ds.Owner.FileScope.CompilationScope.Locale);
                buffer.Add(
                    funCall.ParenToken,
                    OpCode.CALL_FUNCTION,
                    (int)FunctionInvocationType.FIELD_INVOCATION,
                    funCall.Args.Length,
                    0,
                    outputUsed ? 1 : 0,
                    globalNameId,
                    localeId);
            }
            else if (root is BaseMethodReference)
            {
                BaseMethodReference bmr = (BaseMethodReference)root;
                FunctionDefinition  fd  = bmr.ClassToWhichThisMethodRefers.GetMethod(bmr.FieldToken.Value, true);
                if (fd == null)
                {
                    throw new ParserException(bmr.DotToken, "This method does not exist on any base class.");
                }

                bcc.CompileExpressionList(parser, buffer, funCall.Args, true);
                buffer.Add(
                    funCall.ParenToken,
                    OpCode.CALL_FUNCTION,
                    (int)FunctionInvocationType.LOCAL_METHOD,
                    funCall.Args.Length,
                    fd.FunctionID,
                    outputUsed ? 1 : 0,
                    bmr.ClassToWhichThisMethodRefers.ClassID,
                    -1);
            }
            else
            {
                bcc.CompileExpression(parser, buffer, root, true);
                bcc.CompileExpressionList(parser, buffer, funCall.Args, true);
                buffer.Add(
                    funCall.ParenToken,
                    OpCode.CALL_FUNCTION,
                    (int)FunctionInvocationType.POINTER_PROVIDED,
                    argCountIsNegativeOne ? -1 : funCall.Args.Length,
                    0,
                    outputUsed ? 1 : 0,
                    0);
            }
        }