Exemplo n.º 1
0
 public FunctionDeclaration(FunctionModifier modifier, bool isAsync, string name, string description,
                            List <FunctionParameter> parameters, TO2Type declaredReturn, Expression expression,
                            Position start = new Position(), Position end = new Position()) : base(start, end)
 {
     this.modifier       = modifier;
     this.name           = name;
     this.description    = description;
     this.isAsync        = isAsync;
     this.parameters     = parameters;
     this.declaredReturn = declaredReturn;
     this.expression     = expression;
     this.expression.VariableContainer = this;
     this.expression.TypeHint          = context => this.declaredReturn.UnderlyingType(context.ModuleContext);
 }
Exemplo n.º 2
0
        public Function(NmProgram program, string name, FunctionModifier modifier,
                        Type returnType, int scope,
                        List <FunctionParameter> parameters = null,
                        BlockLexeme rawLexeme = null)
        {
            Program    = program;
            Name       = name;
            Modifier   = modifier;
            ReturnType = returnType;
            Parameters = parameters ?? new List <FunctionParameter>();
            RawLexeme  = rawLexeme;
            Scope      = scope;
            IsVariadic = false;

            OriginalName = name;
        }
        public FunctionLexeme(List <Token> tokens) : base(tokens, LexemeType.Function, true)
        {
            switch (tokens[0].Type)
            {
            case TokenType.PublicKeyword:
                Modifier = FunctionModifier.Public;
                break;

            case TokenType.PrivateKeyword:
                Modifier = FunctionModifier.Private;
                break;

            case TokenType.EntrypointKeyword:
                Modifier = FunctionModifier.Entrypoint;
                break;

            case TokenType.FinalizationKeyword:
                Modifier = FunctionModifier.Finalization;
                break;

            case TokenType.InitializationKeyword:
                Modifier = FunctionModifier.Initialization;
                break;

            default:
                Modifier = FunctionModifier.None;
                break;
            }

            var index = 0;

            if (Modifier != FunctionModifier.None)
            {
                index = 1;
            }

            index++; //Function keyword;

            var bracketIndex = tokens.FindIndex(p => p.Type == TokenType.BracketOpen);

            if (bracketIndex != 2 + (Modifier != FunctionModifier.None ? 1 : 0))
            {
                //Has type and Name
                Name       = tokens[bracketIndex - 1];
                ReturnType =
                    tokens[index].StringValue == "void" ? null :
                    new List <Token>
                {
                    tokens[index]
                };

                index += 1;

                while (tokens[index].Type == TokenType.SquareBracketClosed ||
                       tokens[index].Type == TokenType.SquareBracketOpen)
                {
                    if (ReturnType == null)
                    {
                        throw new CompileException(CompileErrorType.UnexpectedToken, tokens[index]);
                    }

                    ReturnType.Add(tokens[index]);
                    index++;
                }

                index++;
            }
            else
            {
                Name = tokens[index];
                index++;
            }

            index++; //bracket
            Token parameterName = null;
            var   parameterType = new List <Token>();

            Parameters = new List <LexemeFunctionParameter>();
            var state = 0;

            var tokenIterator = new TokenIterator <Token>(tokens.Skip(index).Take(tokens.Count - 1 - index));

            while (tokenIterator.GetNext() != null)
            {
                switch (state)
                {
                case 0:
                {
                    if (tokenIterator.Current.Type != TokenType.Identifier)
                    {
                        throw new CompileException(CompileErrorType.UnexpectedToken, tokenIterator.Current);
                    }

                    parameterName = tokenIterator.Current;
                    state         = 1;
                    break;
                }

                case 1:
                {
                    if (tokenIterator.Current.Type != TokenType.Colon)
                    {
                        throw new CompileException(CompileErrorType.UnexpectedToken, tokenIterator.Current);
                    }

                    state = 2;
                    break;
                }

                case 2:
                {
                    if (tokenIterator.Current.Type != TokenType.Identifier)
                    {
                        throw new CompileException(CompileErrorType.UnexpectedToken, tokenIterator.Current);
                    }

                    parameterType.Add(tokenIterator.Current);

                    tokenIterator.GetNext();
                    while (tokenIterator.Current.Type == TokenType.SquareBracketOpen ||
                           tokenIterator.Current.Type == TokenType.SquareBracketClosed)
                    {
                        parameterType.Add(tokenIterator.Current);

                        if (tokenIterator.GetNext() == null)
                        {
                            break;
                        }
                    }

                    Parameters.Add(new LexemeFunctionParameter(parameterType, parameterName));
                    state = 3;

                    if (parameterType.Count == 1 && !tokenIterator.IsLast)
                    {
                        tokenIterator.RollBack();
                    }

                    break;
                }

                case 3:
                {
                    if (tokenIterator.Current.Type != TokenType.ComaSign)
                    {
                        throw new CompileException(CompileErrorType.UnexpectedToken, tokenIterator.Current);
                    }
                    state = 0;
                    break;
                }
                }
            }
        }