Exemplo n.º 1
0
        public Node Statement()
        {
            Node      node = null;
            TokenType type = CurrentToken().type;

            if (IsAssignment())
            {
                node = Assignment();
            }
            else if (IsArrayAccess())
            {
                node = ArrayUpdate();
            }
            else if (IsWhile())
            {
                node = While();
            }
            else if (IsFor())
            {
                node = For();
            }
            else if (IsIfElse())
            {
                node = If();
            }
            else if (IsFunctionDef())
            {
                node = FunctionDefinition();
            }
            else if (IsFunctionCall())
            {
                node = FunctionCall();
            }
            else if (type == TokenType.PRINT)
            {
                MatchAndEat(TokenType.PRINT);
                node = new PrintNode(Expression(), "sameline");
            }
            else if (type == TokenType.PRINTLN)
            {
                MatchAndEat(TokenType.PRINTLN);
                node = new PrintNode(Expression(), "newline");
            }
            else if (type == TokenType.WAIT)
            {
                MatchAndEat(TokenType.WAIT);
                node = new WaitNode(Expression());
            }
            else
            {
                Console.WriteLine("Unknown language construct: "
                                  + CurrentToken().text);
                Environment.Exit(0);
            }
            return(node);
        }
        public static void main(string[] args)
        {
            Node firstMsg  = new PrintNode(new NumberNode(1), "newline");
            Node secondMsg = new PrintNode(new NumberNode(2), "newline");

            Node wait = new WaitNode(new NumberNode(2000));

            List <Node> script = new List <Node>();

            script.Add(firstMsg);
            script.Add(wait);
            script.Add(secondMsg);

            foreach (Node statement in script)
            {
                statement.Eval();
            }
        }