Exemple #1
0
        internal override Expression ResolveType(VariableScope varScope, PastelCompiler compiler)
        {
            this.Root = this.Root.ResolveType(varScope, compiler);

            PType rootType = this.Root.ResolvedType;

            if (rootType.IsStructOrClass)
            {
                string fieldName = this.FieldName.Value;
                rootType.FinalizeType(compiler);
                if (rootType.IsStruct)
                {
                    this.StructType = rootType.StructDef;
                    int fieldIndex;
                    if (!this.StructType.FlatFieldIndexByName.TryGetValue(fieldName, out fieldIndex))
                    {
                        throw new ParserException(this.FieldName, "The struct '" + this.StructType.NameToken.Value + "' does not have a field called '" + fieldName + "'.");
                    }
                    this.ResolvedType = this.StructType.FlatFieldTypes[fieldIndex];
                }
                else
                {
                    this.ClassType = rootType.ClassDef;
                    if (!this.ClassType.Members.ContainsKey(this.FieldName.Value))
                    {
                        throw new ParserException(this.FieldName, "The class '" + this.ClassType.NameToken.Value + "' does not have a member called '" + fieldName + "'.");
                    }

                    ICompilationEntity ce = this.ClassType.Members[fieldName];
                    if (ce is FieldDefinition fd)
                    {
                        this.ResolvedType = fd.FieldType;
                    }
                    else
                    {
                        FunctionDefinition func = (FunctionDefinition)ce;
                        this.ResolvedType = PType.FunctionOf(this.FieldName, func.ReturnType, func.ArgTypes);
                    }
                }
                return(this);
            }

            this.CoreFunctionId = this.DetermineCoreFunctionId(this.Root.ResolvedType, this.FieldName.Value);
            if (this.CoreFunctionId != CoreFunction.NONE)
            {
                CoreFunctionReference cfr = new CoreFunctionReference(this.FirstToken, this.CoreFunctionId, this.Root, this.Owner);
                cfr.ResolvedType = new PType(this.Root.FirstToken, null, "@CoreFunc");
                return(cfr);
            }

            throw new NotImplementedException();
        }
Exemple #2
0
        internal override Expression ResolveType(VariableScope varScope, PastelCompiler compiler)
        {
            for (int i = 0; i < this.Args.Length; ++i)
            {
                this.Args[i] = this.Args[i].ResolveType(varScope, compiler);
            }

            this.Root = this.Root.ResolveType(varScope, compiler);

            if (this.Root is FunctionReference)
            {
                FunctionDefinition functionDefinition = ((FunctionReference)this.Root).Function;
                this.VerifyArgTypes(functionDefinition.ArgTypes, compiler);
                this.ResolvedType = functionDefinition.ReturnType;
                return(this);
            }
            else if (this.Root is CoreFunctionReference)
            {
                CoreFunctionReference  nfr = (CoreFunctionReference)this.Root;
                CoreFunctionInvocation nfi;
                if (nfr.Context == null)
                {
                    nfi = new CoreFunctionInvocation(this.FirstToken, nfr.CoreFunctionId, this.Args, this.Owner);
                }
                else
                {
                    nfi = new CoreFunctionInvocation(this.FirstToken, nfr.CoreFunctionId, nfr.Context, this.Args, this.Owner);
                }

                return(nfi.ResolveType(varScope, compiler));
            }
            else if (this.Root is ExtensibleFunctionReference)
            {
                return(new ExtensibleFunctionInvocation(this.FirstToken, (ExtensibleFunctionReference)this.Root, this.Args).ResolveType(varScope, compiler));
            }
            else if (this.Root is ConstructorReference)
            {
                PType typeToConstruct = ((ConstructorReference)this.Root).TypeToConstruct;
                typeToConstruct.FinalizeType(compiler);
                return(new ConstructorInvocation(this.FirstToken, typeToConstruct, this.Args, this.Owner));
            }
            else if (this.Root.ResolvedType.RootValue == "Func")
            {
                return(new FunctionPointerInvocation(compiler, this.FirstToken, this.Root, this.Args));
            }

            throw new ParserException(this.OpenParenToken, "This expression cannot be invoked like a function.");
        }