public void Visit(MethodDeclr ast)
        {
            PrintWrap("MethodDeclaration", () =>
                {
                    PrintWrap("Return type", () => ast.MethodReturnType.Visit(this));

                    PrintWrap("FunctionName", () => ast.MethodName.Visit(this));

                    PrintWrap("Arguments", () => ast.Arguments.ForEach(arg => arg.Visit(this)));

                    PrintWrap("Body", () => ast.Body.Visit(this));
                });
        }
Esempio n. 2
0
        public static Symbol DefineMethod(MethodDeclr method)
        {
            IType returnType = CreateSymbolType(method.MethodReturnType);

            return new MethodSymbol(method.Token.TokenValue, returnType, method);
        }
Esempio n. 3
0
 public MethodSymbol(string name, IType returnType, MethodDeclr declr)
     : base(name, returnType)
 {
     MethodDeclr = declr;
 }
 public void Visit(MethodDeclr ast)
 {
     Exec(ast);
 }
        private void ValidateReturnStatementType(MethodDeclr ast, Symbol symbol)
        {
            if (!ResolvingTypes)
            {
                return;
            }

            IType returnStatementType;

            // no return found
            if (ast.ReturnAst == null)
            {
                returnStatementType = new BuiltInType(ExpressionTypes.Void);
            }
            else
            {
                returnStatementType = ast.ReturnAst.AstSymbolType;
            }

            var delcaredSymbol = ScopeUtil.CreateSymbolType(ast.MethodReturnType);

            // if its inferred, just use whatever the return statement i
            if (delcaredSymbol.ExpressionType == ExpressionTypes.Inferred)
            {
                return;
            }

            if (!TokenUtil.EqualOrPromotable(returnStatementType.ExpressionType, delcaredSymbol.ExpressionType))
            {
                throw new InvalidSyntax(String.Format("Return type {0} for function {1} is not of the same type of declared method (type {2})",
                    returnStatementType.ExpressionType, symbol.Name, delcaredSymbol.ExpressionType));
            }
        }
        public void Visit(MethodDeclr ast)
        {
            var previousMethod = CurrentMethod;

            CurrentMethod = ast;

            var symbol = ScopeUtil.DefineMethod(ast);

            Current.Define(symbol);

            ScopeTree.CreateScope();

            ast.Arguments.ForEach(arg => arg.Visit(this));

            ast.Body.Visit(this);

            SetScope(ast);

            if (symbol.Type.ExpressionType == ExpressionTypes.Inferred)
            {
                if (ast.ReturnAst == null)
                {
                    ast.AstSymbolType = new BuiltInType(ExpressionTypes.Void);
                }
                else
                {
                    ast.AstSymbolType = ast.ReturnAst.AstSymbolType;
                }
            }
            else
            {
                ast.AstSymbolType = symbol.Type;
            }

            ValidateReturnStatementType(ast, symbol);

            ScopeTree.PopScope();

            CurrentMethod = previousMethod;
        }