Exemple #1
0
        private bool ParseDestructorDeclaration(
            TokenReader reader, 
            bool isVirtual, 
            out MethodDeclaration method)
        {
            Token tok = reader.Peek();
            if (!this.Expect(reader, Keyword.Destructor))
            {
                method = null;
                return false;
            }

            if (!this.Expect(reader, Keyword.LeftParen))
            {
                method = null;
                return false;
            }

            if (!this.Expect(reader, Keyword.RightParen))
            {
                method = null;
                return false;
            }

            method = new MethodDeclaration(
                tok,
                "destructor",
                false,
                isVirtual,
                false);
            return true;
        }
Exemple #2
0
        private bool ParseMethodDeclaration(TokenReader reader, out MethodDeclaration method)
        {
            method = null;
            Token tok = reader.Peek();
            Token start = tok;
            if (tok.Is(Keyword.Constructor))
            {
                return this.ParseConstructorDeclaration(reader, out method);
            }

            if (tok.Is(Keyword.Destructor))
            {
                return this.ParseDestructorDeclaration(
                    reader,
                    false,
                    out method);
            }

            bool isStatic = false;
            bool isVirtual = false;
            bool isAbstract = false;
            if (tok.Is(Keyword.Static))
            {
                isStatic = true;
                reader.Read();
                tok = reader.Peek();
            }
            else if (tok.Is(Keyword.Abstract))
            {
                isAbstract = true;
                reader.Read();
                tok = reader.Peek();
            }
            else if (tok.Is(Keyword.Virtual))
            {
                isVirtual = true;
                reader.Read();
                tok = reader.Peek();
                if (tok.Is(Keyword.Destructor))
                {
                    return this.ParseDestructorDeclaration(
                        reader,
                        true,
                        out method);
                }
            }

            bool isFunction = false;
            if (tok.Is(Keyword.Function))
            {
                isFunction = true;
                reader.Read();
            }
            else
            {
                if (!this.Expect(reader, Keyword.Procedure))
                {
                    return false;
                }
            }

            string methodName = null;
            if (!this.ExpectIdentifier(reader, out methodName))
            {
                return false;
            }

            MethodDeclaration methodDecl = new MethodDeclaration(
                start,
                methodName,
                isStatic,
                isVirtual,
                isAbstract);

            if (!this.Expect(reader, Keyword.LeftParen))
            {
                return false;
            }

            tok = reader.Peek();
            while (!tok.Is(Keyword.RightParen))
            {
                ParameterDeclaration parameter = null;
                if (!this.ParseParameterDeclaration(reader, out parameter))
                {
                    return false;
                }

                methodDecl.AddParameter(parameter);

                tok = reader.Peek();
                if (tok.Is(Keyword.SemiColon))
                {
                    reader.Read();
                    tok = reader.Peek();
                }
            }

            if (!this.Expect(reader, Keyword.RightParen))
            {
                return false;
            }

            if (isFunction)
            {
                if (!this.Expect(reader, Keyword.Colon))
                {
                    return false;
                }

                TypeReference returnType = null;
                if (!this.ParseTypeReference(reader, out returnType))
                {
                    return false;
                }

                methodDecl.ReturnType = returnType;
            }

            method = methodDecl;
            return true;
        }
Exemple #3
0
        private bool ParseConstructorDeclaration(TokenReader reader, out MethodDeclaration method)
        {
            Token start = reader.Peek();
            if (!this.Expect(reader, Keyword.Constructor))
            {
                method = null;
                return false;
            }

            method = null;
            MethodDeclaration methodDecl = new MethodDeclaration(
                start,
                "constructor",
                false,
                false,
                false);

            if (!this.Expect(reader, Keyword.LeftParen))
            {
                return false;
            }

            Token tok = reader.Peek();
            while (!tok.Is(Keyword.RightParen))
            {
                ParameterDeclaration parameter = null;
                if (!this.ParseParameterDeclaration(reader, out parameter))
                {
                    return false;
                }

                methodDecl.AddParameter(parameter);

                tok = reader.Peek();
                if (tok.Is(Keyword.SemiColon))
                {
                    reader.Read();
                    tok = reader.Peek();
                }
            }

            if (!this.Expect(reader, Keyword.RightParen))
            {
                return false;
            }

            method = methodDecl;
            return true;
        }
Exemple #4
0
        private bool TryCreateMethod(
            CompilerContext context, 
            TypeDefinition type,
            MethodDeclaration methodDecl, 
            out MethodInfo methodInfo)
        {
            methodInfo = new MethodInfo(type)
            {
                Name = methodDecl.MethodName,
                IsStatic = methodDecl.IsStatic,
                IsVirtual = methodDecl.IsVirtual || methodDecl.IsAbstract,
                IsAbstract = methodDecl.IsAbstract
            };

            TypeDefinition returnType = null;
            if (methodDecl.ReturnType != null)
            {
                if (!this.TryResolveTypeReference(context, methodDecl.ReturnType, out returnType))
                {
                    return false;
                }

                methodInfo.ReturnType = returnType;
            }

            foreach (ParameterDeclaration paramDecl in methodDecl.Parameters)
            {
                TypeDefinition paramDef = null;
                if (!this.TryResolveTypeReference(context, paramDecl.Type, out paramDef))
                {
                    return false;
                }

                foreach (string name in paramDecl.ParameterNames)
                {
                    ParameterInfo paramInfo = new ParameterInfo
                    {
                        Name = name,
                        Type = paramDef
                    };

                    methodInfo.Parameters.Add(paramInfo);
                }
            }

            if (methodInfo.IsVirtual)
            {
                methodInfo.AssignVTableIndex();
            }

            return true;
        }
Exemple #5
0
 public void AddPublicMethod(MethodDeclaration method)
 {
     this.publicMethods.Add(method);
 }
Exemple #6
0
 public void AddProtectedMethod(MethodDeclaration method)
 {
     this.protectedMethods.Add(method);
 }
Exemple #7
0
 public void AddPrivateMethod(MethodDeclaration method)
 {
     this.privateMethods.Add(method);
 }