Example #1
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.BeginTransaction();
            state.Colorize(tokens, Color.Whitespace, Color.Operator);

            var innerBuilder = new ExpressionBuilder(ExpressionFlags.Standard);

            innerBuilder.Add(new FieldSymbol("__$0"));
            var operatorsParser = new OperatorsParser(innerBuilder);

            var result =
                from op in operatorsParser.Scan(state)
                from closing in state.Scan("^ /']'", Color.Operator)
                select op;

            if (result.ValueOrOriginal(out _, out var original))
            {
                innerBuilder.Add(new FieldSymbol("__$1"));
                if (innerBuilder.ToExpression().If(out var expression, out var exception))
                {
                    var lambda = new LambdaSymbol(2, expression);
                    builder.Add(new SendBinaryMessageSymbol("foldr", Precedence.ChainedOperator));
                    builder.Add(lambda);
                    state.CommitTransaction();

                    return(Unit.Matched());
                }
                else
                {
                    state.RollBackTransaction();
                    return(failedMatch <Unit>(exception));
                }
            }
Example #2
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.Colorize(tokens, Color.Whitespace, Color.Operator);
            state.BeginTransaction();

            var expressionBuilder = new ExpressionBuilder(builder.Flags);
            var parser            = new AnyLambdaParser(expressionBuilder);
            var result            =
                from parsed in parser.Scan(state)
                from endToken in state.Scan("/']'", Color.Operator)
                select parsed;

            if (result.ValueOrOriginal(out _, out var original))
            {
                if (expressionBuilder.ToExpression().If(out var expression, out var exception))
                {
                    builder.Add(new ZipLambdaSymbol(expression));
                    state.CommitTransaction();

                    return(Unit.Matched());
                }
                else
                {
                    state.RollBackTransaction();
                    return(failedMatch <Unit>(exception));
                }
            }
Example #3
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.Colorize(tokens, Color.Whitespace, Color.Keyword, Color.Whitespace);

            if (getBlock(state).ValueOrCast <Unit>(out var block, out var asUnit))
            {
                block.AddReturnIf(new UnitSymbol());
                var lambda        = new LambdaSymbol(0, block);
                var invokeBuilder = new ExpressionBuilder(ExpressionFlags.Standard);
                invokeBuilder.Add(lambda);
                invokeBuilder.Add(new PostfixInvokeSymbol(new Expression[0]));
                if (invokeBuilder.ToExpression().If(out var invokeExpression, out var exception))
                {
                    builder.Add(new TrySymbol(invokeExpression));
                    return(Unit.Matched());
                }
                else
                {
                    return(failedMatch <Unit>(exception));
                }
            }
            else
            {
                return(asUnit);
            }
        }
Example #4
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.Colorize(tokens, Color.Whitespace, Color.Structure);

            var result =
                from c in getExpression(state, builder.Flags | ExpressionFlags.Comparisand)
                from scanned in state.Scan("^ /')' /(|s|) /('->' | '=>')", Color.Structure, Color.Whitespace, Color.Structure)
                from typeConstraint in parseTypeConstraint(state)
                from b in getLambdaBlock(scanned.Contains("->"), state, builder.Flags, typeConstraint)
                select(c, b);

            if (result.If(out var tuple))
            {
                var(comparisand, block) = tuple;

                var expressionBuilder = new ExpressionBuilder(ExpressionFlags.Standard);
                expressionBuilder.Add(new FieldSymbol("__$0"));
                expressionBuilder.Add(new MatchSymbol());
                expressionBuilder.Add(comparisand);

                if (expressionBuilder.ToExpression().If(out var comparison, out var exception))
                {
                    var list = new List <Statement> {
                        new If(comparison, block)
                    };
                    var lambdaSymbol = new LambdaSymbol(1, new Block(list));
                    builder.Add(lambdaSymbol);
                    return(Unit.Matched());
                }
                else
                {
                    return(failedMatch <Unit>(exception));
                }
            }

            return(Unit.Matched());
        }
Example #5
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            state.Colorize(tokens, Color.Whitespace, Color.Keyword);
            if (builder.ToExpression().If(out var expression, out var exception))
            {
                var comprehensions = new List <(Symbol, Expression, IMaybe <Expression>, string)>();

                if (getInnerComprehension(state).ValueOrCast <Unit>(out var tuple, out var asUnit))
                {
                    var(comparisand, source, ifExp) = tuple;
                    var image = $"for {comparisand} := {source}";
                    comprehensions.Add((comparisand, source, ifExp, image));
                }
                else
                {
                    return(asUnit);
                }

                while (state.More)
                {
                    var parser = new InnerComprehensionParser(builder, comprehensions);
                    if (parser.Scan(state).If(out _, out var anyException))
                    {
                    }
                    else if (anyException.If(out exception))
                    {
                        return(failedMatch <Unit>(exception));
                    }
                    else
                    {
                        break;
                    }
                }

                var stack = new Stack <(Symbol, Expression, IMaybe <Expression>, string)>();
                foreach (var item in comprehensions)
                {
                    stack.Push(item);
                }

                For forStatement;
                var images = new StringBuilder();
                if (stack.Count > 0)
                {
                    var(symbol, source, ifExp, image) = stack.Pop();
                    images.Append(image);
                    var yieldStatement = new Yield(expression);
                    var block          = new Block(yieldStatement);
                    if (ifExp.If(out var boolean))
                    {
                        block = new Block(new If(boolean, block));
                    }

                    forStatement = new For(symbol, source, block);
                }
                else
                {
                    return(notMatched <Unit>());
                }

                while (stack.Count > 0)
                {
                    var(symbol, source, ifExp, image) = stack.Pop();
                    images.Append(image);
                    var block = new Block(forStatement);
                    if (ifExp.If(out var boolean))
                    {
                        block = new Block(new If(boolean, block));
                    }

                    forStatement = new For(symbol, source, block);
                }

                builder.Clear();

                state.CreateReturnType();

                var statements = new List <Statement>
                {
                    forStatement, new Return(new Expression(new NoneSymbol()), state.GetReturnType())
                };

                state.RemoveReturnType();

                builder.Add(new ComprehensionSymbol(new Block(statements), images.ToString()));

                return(Unit.Matched());
            }
            else
            {
                return(failedMatch <Unit>(exception));
            }
        }
Example #6
0
        public override IMatched <Unit> Parse(ParseState state, Token[] tokens, ExpressionBuilder builder)
        {
            var functionName = tokens[2].Text;

            if (functionName == @"\/")
            {
                return(notMatched <Unit>());
            }
            else
            {
                state.Colorize(tokens, Color.Whitespace, Color.Invokable, Color.OpenParenthesis);

                if (getArgumentsPlusLambda(state, builder.Flags).ValueOrCast <Unit>(out var tuple, out var asUnit))
                {
                    var(arguments, possibleLambda) = tuple;

                    if (state.BlockFollows())
                    {
                        state.Scan("^ /':'", Color.Structure);
                        if (state.Advance().ValueOrOriginal(out _, out var unitMatched))
                        {
                            var tempObjectField   = newLabel("object");
                            var outerBuilder      = new ExpressionBuilder(ExpressionFlags.Standard);
                            var setPropertyParser = new SetPropertyParser(builder, tempObjectField, outerBuilder);
                            while (state.More)
                            {
                                if (setPropertyParser.Scan(state).If(out _, out var anyException))
                                {
                                }
                                else if (anyException.If(out var exception))
                                {
                                    return(failedMatch <Unit>(exception));
                                }
                                else
                                {
                                    break;
                                }
                            }

                            state.Regress();

                            if (outerBuilder.ToExpression().If(out var outerExpression, out var outerException))
                            {
                                builder.Add(new NewObjectSymbol(tempObjectField, functionName, outerExpression));
                            }
                            else
                            {
                                return(failedMatch <Unit>(outerException));
                            }
                        }
                        else
                        {
                            return(unitMatched);
                        }
                    }
                    else if (state.Macro(functionName).If(out var function))
                    {
                        builder.Add(new MacroInvokeSymbol(function, arguments));
                    }
                    else
                    {
                        builder.Add(new InvokeSymbol(functionName, arguments, possibleLambda, builder.Flags[ExpressionFlags.Comparisand]));
                    }

                    return(Unit.Matched());
                }