private static ExpressionNode ParseUnit(Parser.State state, Context context)
        {
            if (state.GetToken().Is(TokenSubType.BraceSquareLeft))
            {
                state.GetNextNeToken();
                var func = new FunctionNode();

                FunctionParser.ParseParametersList(state, func);
                if (state.IsErrorOccured())
                {
                    return(null);
                }

                var subContext = new Context(context, context.Values);
                func.Code    = Parse(state, subContext);
                func.Context = subContext;

                if (state.IsErrorOccured())
                {
                    return(null);
                }

                return(func);
            }

            return(ParseValue(state, context));
        }
        public static CoreNode Parse(Parser.State state)
        {
            var            node = new CoreNode();
            ExpressionNode value;

            int lastIndex     = state.Index;
            var globalContext = new Context();

            while ((value = ExpressionParser.Parse(state, globalContext)) != null && !state.IsErrorOccured())
            {
                if (lastIndex == state.Index)
                {
                    break;
                }

                lastIndex     = state.Index;
                value.Context = globalContext;
                node.Code     = value;

                if (state.Index + 1 < state.Tokens.Count && !state.IsErrorOccured())
                {
                    Console.WriteLine($"{state.Index} / {state.Tokens.Count}");
                    state.ErrorCode    = (uint)ErrorCodes.T_UnexpectedEndOfFile;
                    state.ErrorMessage = "Something really wrong happend";
                }
            }

            return(node);
        }
        public static ExpressionNode Parse(Parser.State state)
        {
            if (state.IsErrorOccured() || state.GetToken().Is(TokenType.EOF))
            {
                return(null);
            }

            var block = new BlockNode();

            do
            {
                while (state.GetToken().Is(TokenSubType.Colon))
                {
                    state.GetNextNeToken();
                }


                var unit = ParseUnit(state);
                FunctionCallNode fc_unit;

                if (unit == null || state.IsErrorOccured())
                {
                    break;
                }

                while (!state.IsErrorOccured() &&
                       !state.GetToken().Is(TokenType.EOF) &&
                       (fc_unit = ParseFunctionCall(state, unit)) != null)
                {
                    unit = fc_unit;
                }

                block.Code.Add(unit);
            } while (state.GetToken().Is(TokenSubType.Colon));

            return(block);
        }
        private static ExpressionNode ParseValue(Parser.State state)
        {
            state.Save();
            var token = state.GetTokenAndMoveNe();

            if (token.Is(TokenType.Number))
            {
                state.Drop();
                return(new NumberNode(
                           value: token.Value,
                           isDecimal: token.Is(TokenSubType.Decimal)
                           ));
            }
            else if (token.Is(TokenType.Identifier))
            {
                state.Drop();
                return(new VariableNode(token.Value));
            }
            else if (token.Is(TokenType.String))
            {
                state.Drop();
                return(new StringNode(token.Value));
            }
            else if (token.Is(TokenSubType.BraceRoundLeft))
            {
                state.Drop();
                var node = Parse(state);
                if (state.IsErrorOccured())
                {
                    return(node);
                }

                if (!state.GetToken().Is(TokenSubType.BraceRoundRight))
                {
                    state.ErrorCode    = (uint)ErrorCodes.P_ClosingBraceRequired;
                    state.ErrorMessage =
                        "Expected <BraceRoundRight>, " +
                        $"but <{state.GetToken().SubType}> was given";
                    return(node);
                }

                state.GetNextNeToken();
                return(node);
            }
            state.Restore();

            return(null);
        }
        internal static FunctionCallNode ParseFunctionCall(Parser.State state, ExpressionNode func)
        {
            var call = new FunctionCallNode
            {
                CalleeExpression = func
            };

            ExpressionNode arg = ParseUnit(state);

            if (state.IsErrorOccured() || arg == null)
            {
                return(null);
            }

            call.Argument = arg;
            return(call);
        }
Ejemplo n.º 6
0
        public static CoreNode Parse(Parser.State state)
        {
            var  node = new CoreNode();
            Node value;

            int lastIndex = state.Index;

            while ((value = ExpressionParser.Parse(state)) != null && !state.IsErrorOccured())
            {
                if (lastIndex == state.Index)
                {
                    break;
                }

                lastIndex = state.Index;
                node.code.Add(value);
            }

            return(node);
        }