示例#1
0
        public CoreFunctionReference(Token firstToken, CoreFunction coreFunctionId, Expression context, ICompilationEntity owner) : base(firstToken, owner)
        {
            this.CoreFunctionId = coreFunctionId;
            this.Context        = context;

            this.ReturnType         = CoreFunctionUtil.GetCoreFunctionReturnType(this.CoreFunctionId);
            this.ArgTypes           = CoreFunctionUtil.GetCoreFunctionArgTypes(this.CoreFunctionId);
            this.ArgTypesIsRepeated = CoreFunctionUtil.GetCoreFunctionIsArgTypeRepeated(this.CoreFunctionId);
        }
示例#2
0
        internal override Expression ResolveType(VariableScope varScope, PastelCompiler compiler)
        {
            // The args were already resolved.
            // This ensures that they match the core function definition

            PType[] expectedTypes = CoreFunctionUtil.GetCoreFunctionArgTypes(this.Function);
            bool[]  isArgRepeated = CoreFunctionUtil.GetCoreFunctionIsArgTypeRepeated(this.Function);

            switch (this.Function)
            {
            case CoreFunction.FORCE_PARENS:
                if (this.Args.Length != 1)
                {
                    throw new ParserException(this.FirstToken, "Expected 1 arg.");
                }

                return(new ForcedParenthesis(this.FirstToken, this.Args[0]));
            }

            Dictionary <string, PType> templateLookup = new Dictionary <string, PType>();

            int verificationLength = expectedTypes.Length;

            if (verificationLength > 0 && isArgRepeated[isArgRepeated.Length - 1])
            {
                verificationLength--;
            }

            for (int i = 0; i < verificationLength; ++i)
            {
                if (!PType.CheckAssignmentWithTemplateOutput(compiler, expectedTypes[i], this.Args[i].ResolvedType, templateLookup))
                {
                    PType expectedType = expectedTypes[i];
                    if (templateLookup.ContainsKey(expectedType.ToString()))
                    {
                        expectedType = templateLookup[expectedType.ToString()];
                    }
                    throw new ParserException(this.Args[i].FirstToken, "Incorrect type. Expected " + expectedType + " but found " + this.Args[i].ResolvedType + ".");
                }
            }

            if (expectedTypes.Length < this.Args.Length)
            {
                if (isArgRepeated[isArgRepeated.Length - 1])
                {
                    PType expectedType = expectedTypes[expectedTypes.Length - 1];
                    for (int i = expectedTypes.Length; i < this.Args.Length; ++i)
                    {
                        if (!PType.CheckAssignment(compiler, expectedType, this.Args[i].ResolvedType))
                        {
                            throw new ParserException(this.Args[i].FirstToken, "Incorrect type. Expected " + expectedTypes[i] + " but found " + this.Args[i].ResolvedType + ".");
                        }
                    }
                }
                else
                {
                    throw new ParserException(this.FirstToken, "Too many arguments.");
                }
            }

            PType returnType = CoreFunctionUtil.GetCoreFunctionReturnType(this.Function);

            if (returnType.HasTemplates)
            {
                returnType = returnType.ResolveTemplates(templateLookup);
            }

            this.ResolvedType = returnType;

            return(this);
        }