public static PartialFunctionExpression Create(
            AphidExpressionContext context_aphidExpressionContext,
            CallExpression call_callExpression,
            int value_i,
            int value_i1
            )
        {
            PartialFunctionExpression partialFunctionExpression
                = new PartialFunctionExpression
                      (context_aphidExpressionContext, call_callExpression);

            ((AphidExpression)partialFunctionExpression).Index  = value_i;
            ((AphidExpression)partialFunctionExpression).Length = value_i1;
            return(partialFunctionExpression);

            // TODO: Edit factory method of PartialFunctionExpression
            // This method should be able to configure the object in all possible ways.
            // Add as many parameters as needed,
            // and assign their values to each field by using the API.
        }
Esempio n. 2
0
        private Expression ParseFunctionExpression()
        {
            Expression exp;

            NextToken();

            switch (_currentToken.TokenType)
            {
            case AphidTokenType.LeftParenthesis:
                var funcExp = new FunctionExpression()
                {
                    Args = new List <IdentifierExpression>()
                };

                NextToken();

                if (_currentToken.TokenType != AphidTokenType.RightParenthesis)
                {
                    while (true)
                    {
                        if (_currentToken.TokenType == AphidTokenType.Identifier)
                        {
                            funcExp.Args.Add(ParseIdentifierExpression() as IdentifierExpression);

                            if (_currentToken.TokenType == AphidTokenType.Comma)
                            {
                                NextToken();
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            throw new AphidParserException(_currentToken);
                        }
                    }
                }

                Match(AphidTokenType.RightParenthesis);

                var isSingleLine = _currentToken.TokenType != AphidTokenType.LeftBrace;

                var body = ParseBlock(false);

                if (isSingleLine)
                {
                    funcExp.Body = new List <Expression> {
                        new UnaryOperatorExpression(AphidTokenType.retKeyword, body[0])
                    };
                }
                else
                {
                    funcExp.Body = body;
                }

                exp = funcExp;

                break;

            default:

                exp = new PartialFunctionExpression((CallExpression)ParseCallExpression());
                break;
            }

            return(exp);
        }
Esempio n. 3
0
        private Expression ParseFunctionExpression()
        {
            Expression exp;

            NextToken();

            switch (_currentToken.TokenType)
            {
                case AphidTokenType.LeftParenthesis:
                    var funcExp = new FunctionExpression()
                    {
                        Args = new List<IdentifierExpression>()
                    };

                    NextToken();

                    if (_currentToken.TokenType != AphidTokenType.RightParenthesis)
                    {
                        while (true)
                        {
                            if (_currentToken.TokenType == AphidTokenType.Identifier)
                            {
                                funcExp.Args.Add(ParseIdentifierExpression() as IdentifierExpression);

                                if (_currentToken.TokenType == AphidTokenType.Comma)
                                {
                                    NextToken();
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                throw new AphidParserException(_currentToken);
                            }
                        }
                    }

                    Match(AphidTokenType.RightParenthesis);

                    var isSingleLine = _currentToken.TokenType != AphidTokenType.LeftBrace;

                    var body = ParseBlock(false);

                    if (isSingleLine)
                    {
                        funcExp.Body = new List<Expression> { new UnaryOperatorExpression(AphidTokenType.retKeyword, body[0]) };
                    }
                    else
                    {
                        funcExp.Body = body;
                    }

                    exp = funcExp;

                    break;

                default:

                    exp = new PartialFunctionExpression((CallExpression)ParseCallExpression());
                    break;
            }

            return exp;
        }
        public AphidObject InterpretPartialFunctionExpression(PartialFunctionExpression expression)
        {
            var obj = (AphidObject)InterpretExpression(expression.Call.FunctionExpression);
            var func = (AphidFunction)obj.Value;
            var partialArgCount = func.Args.Length - expression.Call.Args.Count();
            var partialArgs = func.Args.Skip(partialArgCount).ToArray();
            var partialFunc = new AphidFunction()
            {
                Args = partialArgs,
                Body = new List<Expression>
                {
                    new UnaryOperatorExpression(AphidTokenType.retKeyword,
                        new CallExpression(
                            expression.Call.FunctionExpression,
                            expression.Call.Args.Concat(partialArgs.Select(x => new IdentifierExpression(x))).ToArray())),
                },
                ParentScope = _currentScope,
            };

            return new AphidObject(partialFunc);
        }
Esempio n. 5
0
 private AphidExpression ParseFunctionExpression()
 {
     AphidExpression exp = default(AphidExpression);
     var body = new System.Collections.Generic.List<AphidExpression>();
     var args = new System.Collections.Generic.List<AphidExpression>();
     AphidExpression argExp = default(AphidExpression);
     NextToken();
     if ((this._currentToken.TokenType == AphidTokenType.LeftParenthesis)) {
         NextToken();
         if (((this._currentToken.TokenType == AphidTokenType.RightParenthesis)
                     == false)) {
             for (
             ; true;
             ) {
                 if ((this._currentToken.TokenType == AphidTokenType.Identifier)) {
                     var id = ParseIdentifierExpression();
                     argExp = id;
                     if ((this._currentToken.TokenType == AphidTokenType.AssignmentOperator)) {
                         var op = this._currentToken.TokenType;
                         NextToken();
                         argExp = new BinaryOperatorExpression(id, op, ParseExpression());
                     }
                     args.Add(argExp);
                     if ((this._currentToken.TokenType == AphidTokenType.Comma)) {
                         NextToken();
                     }
                     else {
     break;
                     }
                 }
                 else {
                     throw new AphidParserException(_currentToken);
                 }
             }
         }
         Match(AphidTokenType.RightParenthesis);
         var isSingleLine = (this._currentToken.TokenType != AphidTokenType.LeftBrace);
         var block = ParseSingleBlock();
         if ((isSingleLine && UseImplicitReturns)) {
             body.Add(new UnaryOperatorExpression(AphidTokenType.retKeyword, block[0]));
         }
         else {
             body = block;
         }
         exp = new FunctionExpression(args, body);
     }
     else {
         exp = new PartialFunctionExpression(ParseCallExpression());
     }
     return exp;
 }