예제 #1
0
        public Parser()
        {
            RootNode = new Root();
            Variables = new List<VariableInfo>();
            Tokens = new List<Token>();

            SetKeywords();
            SetVariables();
        }
예제 #2
0
        /// <summary>
        /// <IFSTATEMENT> := 'IF (' <CONDITION> ')' <STATEMENTS> 'END'
        /// If statements within if statements do not work
        /// </summary>
        private Node ParseIfStatement(Node node, ref Node currentNodeMarker)
        {
            node = new Node();

            node.Value = "IF";

            TokenIndex++;

            //Semantics
            if (!GetCurrentToken().Value.Equals("("))
                Console.Write("Enclose the condition in (...)");

            TokenIndex++;

            //Parse the condition in the left child of the IF node
            node.Left = ParseCondition(node.Left);

            //Semantics
            if (!GetCurrentToken().Value.Equals(")"))
                Console.Write("Enclose the condition in (...)");

            //Skip the right bracket and new line
            TokenIndex++;
            TokenIndex++;

            //Create a root class node for the top of the IF tree
            Root root = new Root();
            //Insert the IF node in the root
            root.Insert(node.Value, node.Left);

            //Mark the current node and set it
            currentNodeMarker = node.Right;
            Node currentNode = node.Right;

            //Parse statements until END is found
            while (!GetCurrentToken().Value.Equals("END"))
            {
                if (GetCurrentToken().Type.Equals(TokenType.EOL))
                {
                    TokenIndex++;
                }
                //Insert the statements in
                currentNode = ParseStatement(currentNode, ref currentNodeMarker);

                if (currentNode != null)
                {
                    root.Insert("STATEMENTS", currentNode);
                    currentNode = currentNode.Right;
                    currentNodeMarker = currentNode;
                }

                TokenIndex++;
            }

            //The final right node will always be empty, so remove it
            root.NullifyEmptyChildren();

            return (Node)root;
        }