public static VariableDeclarationNode parse(int TokenNumber)
        {
            VariableDeclarationNode node = new VariableDeclarationNode();

            if (!(Parser.NeededToken(TokenNumber, "value", "var", 0) &&
                  Parser.NeededToken(TokenNumber + 1, "type", "name", 0) &&
                  Parser.NeededToken(TokenNumber + 2, "value", ":", 0)))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            node.SetName(Parser.TokensArray[TokenNumber + 1].Value);
            TokenNumber += 3;
            ExpressionNode ExprNode = ExpressionParser.parse(TokenNumber);

            if (ExprNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = ExprNode.ErrorLine;
                return(node);
            }
            node.SetExpression(ExprNode);
            node.TokenNumber = ExprNode.TokenNumber;
            return(node);
        }
        public static ReturnStatementNode parse(int TokenNumber)
        {
            ReturnStatementNode node = new ReturnStatementNode();

            if (!Parser.NeededToken(TokenNumber, "value", "return", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            ExpressionNode ExprNode = ExpressionParser.parse(TokenNumber);

            if (ExprNode.BadParsed)
            {
                node.IsVoid = true;
            }
            else
            {
                node.SetExpression(ExprNode);
                TokenNumber = ExprNode.TokenNumber;
            }
            node.TokenNumber = TokenNumber;
            return(node);
        }
Exemple #3
0
        public static WhileLoopNode parse(int TokenNumber)
        {
            WhileLoopNode node = new WhileLoopNode();

            if (!Parser.NeededToken(TokenNumber, "value", "while", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            ExpressionNode ExprNode = ExpressionParser.parse(TokenNumber);

            if (ExprNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = ExprNode.ErrorLine;
                return(node);
            }
            node.SetExpression(ExprNode);
            TokenNumber = ExprNode.TokenNumber;
            if (!Parser.NeededToken(TokenNumber, "value", "loop", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            List <Node> BodyNodes = BodyParser.parse(TokenNumber);

            if (BodyNodes.Count == 0)
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            Node LastNode = BodyNodes[BodyNodes.Count - 1];

            if (LastNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = LastNode.ErrorLine;
                return(node);
            }
            for (int i = 0; i < BodyNodes.Count; ++i)
            {
                node.AddBodyNode(BodyNodes[i]);
            }
            TokenNumber = LastNode.TokenNumber;
            if (!Parser.NeededToken(TokenNumber, "value", "end", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            node.TokenNumber = TokenNumber;
            return(node);
        }
Exemple #4
0
        public static IfStatementNode parse(int TokenNumber)
        {
            IfStatementNode node = new IfStatementNode();

            if (!Parser.NeededToken(TokenNumber, "value", "if", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            ExpressionNode ExprNode = ExpressionParser.parse(TokenNumber);

            if (ExprNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = ExprNode.ErrorLine;
                return(node);
            }
            node.SetExpression(ExprNode);
            TokenNumber = ExprNode.TokenNumber;
            if (!Parser.NeededToken(TokenNumber, "value", "then", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            List <Node> BodyNodes = BodyParser.parse(TokenNumber);

            if (BodyNodes.Count == 0)
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            Node LastNode = BodyNodes[BodyNodes.Count - 1];

            if (LastNode.BadParsed)
            {
                node.BadParsed = true;
                node.ErrorLine = LastNode.ErrorLine;
                return(node);
            }
            for (int i = 0; i < BodyNodes.Count; ++i)
            {
                node.AddIfBodyNode(BodyNodes[i]);
            }
            TokenNumber = LastNode.TokenNumber;
            if (Parser.NeededToken(TokenNumber, "value", "else", 0))
            {
                TokenNumber++;
                BodyNodes = BodyParser.parse(TokenNumber);
                if (BodyNodes.Count == 0)
                {
                    node.BadParsed = true;
                    node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                    return(node);
                }
                LastNode = BodyNodes[BodyNodes.Count - 1];
                if (LastNode.BadParsed)
                {
                    node.BadParsed = true;
                    node.ErrorLine = LastNode.ErrorLine;
                    return(node);
                }
                for (int i = 0; i < BodyNodes.Count; ++i)
                {
                    node.AddElseBodyNode(BodyNodes[i]);
                }
                TokenNumber = LastNode.TokenNumber;
            }
            if (!Parser.NeededToken(TokenNumber, "value", "end", 0))
            {
                node.BadParsed = true;
                node.ErrorLine = Parser.TokensArray[Math.Min(TokenNumber, Parser.TokensArray.Length - 1)].Line;
                return(node);
            }
            TokenNumber++;
            node.TokenNumber = TokenNumber;
            return(node);
        }
Exemple #5
0
        public static CallNode parse(int TokenNumber)
        {
            CallNode node = new CallNode();

            if ((Parser.NeededToken(TokenNumber, "type", "name", 0) || Parser.NeededToken(TokenNumber, "value", "this", 0)) &&
                Parser.NeededToken(TokenNumber + 1, "value", ".", 0))
            {
                node.SetCallerName(Parser.TokensArray[TokenNumber].Value);
                TokenNumber += 2;
            }
            while (true)
            {
                string CurrentCallee            = "";
                List <ExpressionNode> arguments = new List <ExpressionNode>();
                if (!Parser.NeededToken(TokenNumber, "type", "name", 0))
                {
                    node.BadParsed = true;
                    node.ErrorLine = Parser.TokensArray[Math.Min(Parser.TokensArray.Length - 1, TokenNumber)].Line;
                    return(node);
                }
                CurrentCallee = Parser.TokensArray[TokenNumber].Value;
                TokenNumber++;
                if (Parser.NeededToken(TokenNumber, "value", "(", 0))
                {
                    TokenNumber++;
                    while (true)
                    {
                        ExpressionNode enode = ExpressionParser.parse(TokenNumber);
                        if (enode.BadParsed)
                        {
                            node.BadParsed = true;
                            node.ErrorLine = enode.ErrorLine;
                            return(node);
                        }
                        TokenNumber = enode.TokenNumber;
                        arguments.Add(enode);
                        if (Parser.NeededToken(TokenNumber, "value", ",", 0))
                        {
                            TokenNumber++;
                            continue;
                        }
                        if (Parser.NeededToken(TokenNumber, "value", ")", 0))
                        {
                            TokenNumber++;
                            break;
                        }
                        node.BadParsed = true;
                        node.ErrorLine = Parser.TokensArray[Math.Min(Parser.TokensArray.Length - 1, TokenNumber)].Line;
                        return(node);
                    }
                }
                node.AddCallee(CurrentCallee, arguments);
                if (Parser.NeededToken(TokenNumber, "value", ".", 0))
                {
                    TokenNumber++;
                    continue;
                }
                break;
            }
            node.TokenNumber = TokenNumber;
            return(node);
        }