Example #1
0
        public static Node parseProgram()
        {
            Node tree = new Statements(new Token(), new List <Node>());

            while ((tokens.actual.type == TokenType.SUB) || (tokens.actual.type == TokenType.FUNCTION))
            {
                if (tokens.actual.type == TokenType.SUB)
                {
                    tree.children.Add(parseSubDec());
                }
                else
                {
                    tree.children.Add(parseFuncDec());
                }
            }
            return(tree);
        }
Example #2
0
        public static Node parseStatement()
        {
            Node output;

            if (tokens.actual.type == TokenType.IDENTIFIER)
            {
                Node identifier = new Identifier(tokens.actual.Copy());
                tokens.selectNext();
                output = new Assignment(tokens.actual.Copy(), new List <Node>()
                {
                    identifier
                });
                tokens.selectNext();
                output.children.Add(parseExpression());
            }
            else if (tokens.actual.type == TokenType.CALL)
            {
                tokens.selectNext();
                if (tokens.actual.type != TokenType.IDENTIFIER)
                {
                    throw new Exception("Expecting sub identifier after call token");
                }
                Token funcIdentifier = tokens.actual.Copy();
                tokens.selectNext();

                if (tokens.actual.type != TokenType.PARENTHESESBEGIN)
                {
                    throw new Exception("Expecting ( after sub identifier");
                }
                tokens.selectNext();
                List <Node> arguments = new List <Node>();
                if (tokens.actual.type == TokenType.PARENTHESESEND)
                {
                    tokens.selectNext();
                }
                else
                {
                    arguments.Add(parseExpression());
                    while (tokens.actual.type == TokenType.COMA)
                    {
                        tokens.selectNext();
                        arguments.Add(parseExpression());
                    }
                    if (tokens.actual.type != TokenType.PARENTHESESEND)
                    {
                        throw new Exception("Exepecting ) to finish sub call");
                    }
                    tokens.selectNext();
                }
                output = new SubCall(funcIdentifier, arguments);
            }
            else if (tokens.actual.type == TokenType.PRINT)
            {
                output = new Print(tokens.actual.Copy(), new List <Node>());
                tokens.selectNext();
                output.children.Add(parseExpression());
            }
            else if (tokens.actual.type == TokenType.DIM)
            {
                output = new VarDec(tokens.actual.Copy(), new List <Node>());
                tokens.selectNext();
                if (tokens.actual.type == TokenType.IDENTIFIER)
                {
                    output.children.Add(new Identifier(tokens.actual.Copy()));
                    tokens.selectNext();
                    if (tokens.actual.type == TokenType.AS)
                    {
                        tokens.selectNext();
                        output.children.Add(parseType());
                    }
                    else
                    {
                        throw new Exception("Invalid Syntax - Expecting As token");
                    }
                }
                else
                {
                    throw new Exception("Invalid Syntax - Expecting Identifier after DIM");
                }
            }
            else if (tokens.actual.type == TokenType.WHILE)
            {
                output = new While(tokens.actual.Copy(), new List <Node>());
                tokens.selectNext();
                output.children.Add(parseRelExpression());
                if (tokens.actual.type != TokenType.BREAKLINE)
                {
                    throw new Exception("Invalid Syntax -> Expecting new line");
                }
                tokens.selectNext();
                Node whileStatements = new Statements(tokens.actual.Copy(), new List <Node>());
                while (tokens.actual.type != TokenType.WEND)
                {
                    whileStatements.children.Add(parseStatement());
                    if (tokens.actual.type != TokenType.BREAKLINE)
                    {
                        throw new Exception("Invalid Syntax -> Expecting new line");
                    }
                    tokens.selectNext();
                }
                output.children.Add(whileStatements);
                tokens.selectNext();
            }
            else if (tokens.actual.type == TokenType.IF)
            {
                output = new If(tokens.actual.Copy(), new List <Node>());
                tokens.selectNext();
                Node firstSon = parseRelExpression();
                if (tokens.actual.type == TokenType.THEN)
                {
                    tokens.selectNext();
                    if (tokens.actual.type != TokenType.BREAKLINE)
                    {
                        throw new Exception("Invalid Syntax -> Expecting new line");
                    }
                    tokens.selectNext();
                    Node secondSon = new Statements(tokens.actual.Copy(), new List <Node>());
                    while ((tokens.actual.type != TokenType.ELSE) && (tokens.actual.type != TokenType.END))
                    {
                        secondSon.children.Add(parseStatement());
                        if (tokens.actual.type != TokenType.BREAKLINE)
                        {
                            throw new Exception("Invalid Syntax -> Expecting new line");
                        }
                        tokens.selectNext();
                    }
                    output.children.Add(firstSon);
                    output.children.Add(secondSon);
                }
                else
                {
                    throw new Exception("Invalid Syntax -> Missing THEN");
                }
                if (tokens.actual.type == TokenType.ELSE)
                {
                    tokens.selectNext();
                    if (tokens.actual.type != TokenType.BREAKLINE)
                    {
                        throw new Exception("Invalid Syntax -> Expecting new line");
                    }
                    tokens.selectNext();
                    Node thirdSon = new Statements(tokens.actual.Copy(), new List <Node>());
                    while (tokens.actual.type != TokenType.END)
                    {
                        thirdSon.children.Add(parseStatement());
                        if (tokens.actual.type != TokenType.BREAKLINE)
                        {
                            throw new Exception("Invalid Syntax -> Expecting new line");
                        }
                        tokens.selectNext();
                    }
                    output.children.Add(thirdSon);
                    tokens.selectNext();
                    if (tokens.actual.type == TokenType.CONDICAO)
                    {
                        tokens.selectNext();
                    }
                    else
                    {
                        throw new Exception("Invalid Syntax -> Missing IF of END IF");
                    }
                }
                else if (tokens.actual.type == TokenType.END)
                {
                    tokens.selectNext();
                    if (tokens.actual.type == TokenType.CONDICAO)
                    {
                        tokens.selectNext();
                    }
                    else
                    {
                        throw new Exception("Invalid Syntax -> Missing IF of END IF");
                    }
                }
                else
                {
                    throw new Exception("Invalid Syntax -> Missing END IF");
                }
            }
            else
            {
                output = new NoOp();
            }
            return(output);
        }
Example #3
0
        public static Node parseFuncDec()
        {
            tokens.selectNext();
            if (tokens.actual.type != TokenType.IDENTIFIER)
            {
                throw new Exception("Expecting Identifier after function token");
            }
            Token actualToken = tokens.actual.Copy();

            tokens.selectNext();

            if (tokens.actual.type != TokenType.PARENTHESESBEGIN)
            {
                throw new Exception("Expecting ( token");
            }
            tokens.selectNext();
            if ((tokens.actual.type != TokenType.PARENTHESESEND) &&
                (tokens.actual.type != TokenType.IDENTIFIER))
            {
                throw new Exception("Expecting ) or variable declaration");
            }

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

            if (tokens.actual.type == TokenType.IDENTIFIER)
            {
                Node vardec;

                vardec = new VarDec(tokens.actual.Copy(), new List <Node>());
                vardec.children.Add(new Identifier(tokens.actual.Copy()));
                tokens.selectNext();
                if (tokens.actual.type != TokenType.AS)
                {
                    throw new Exception("Expecting AS after identifier declaration");
                }
                tokens.selectNext();
                vardec.children.Add(parseType());
                varDecs.Add(vardec);

                while (tokens.actual.type == TokenType.COMA)
                {
                    tokens.selectNext();
                    vardec = new VarDec(tokens.actual.Copy(), new List <Node>());
                    vardec.children.Add(new Identifier(tokens.actual.Copy()));
                    tokens.selectNext();
                    if (tokens.actual.type != TokenType.AS)
                    {
                        throw new Exception("Expecting AS after identifier declaration");
                    }
                    tokens.selectNext();
                    vardec.children.Add(parseType());
                    varDecs.Add(vardec);
                }
            }

            tokens.selectNext(); //skipping )

            if (tokens.actual.type != TokenType.AS)
            {
                throw new Exception("Expecting AS after identifier of func");
            }
            tokens.selectNext();

            Node tree = new FuncDec(actualToken, varDecs, parseType());

            if (tokens.actual.type != TokenType.BREAKLINE)
            {
                throw new Exception("Expecting new Line");
            }

            tokens.selectNext();

            Node statements = new Statements(new Token(), new List <Node>());

            if (tokens.Spy().type == TokenType.END)
            {
                tokens.selectNext();
                if (tokens.actual.type != TokenType.FUNCTION)
                {
                    throw new Exception("Expecting FUNCTION after END");
                }
                tokens.selectNext();
                tree.children.Add(statements);
                return(tree);
            }

            statements.children.Add(parseStatement());
            if (tokens.actual.type != TokenType.BREAKLINE)
            {
                throw new Exception("Expecting breakline");
            }
            tokens.selectNext();

            while (tokens.actual.type != TokenType.END)
            {
                statements.children.Add(parseStatement());
                if (tokens.actual.type != TokenType.BREAKLINE)
                {
                    throw new Exception("Expecting breakline");
                }
                tokens.selectNext();
            }

            tokens.selectNext();
            if (tokens.actual.type != TokenType.FUNCTION)
            {
                throw new Exception("Expecting FUNCTION after END TOKEN");
            }
            tokens.selectNext();
            tree.children.Add(statements);
            if ((tokens.actual.type != TokenType.BREAKLINE) && (tokens.actual.type != TokenType.EOF))
            {
                throw new Exception("Expecting breakline");
            }
            tokens.selectNext();
            return(tree);
        }
Example #4
0
        public void StartGame()
        {
            while (true)
            {
                Console.WriteLine("Witaj w grze Diablo\n" +
                                  "[1] Zacznij nową grę\n" +
                                  "[X] Zamknij program");
                String command = Console.ReadLine();
                if (command == "X")
                {
                    break;
                }
                else if (command == "1")
                {
                    Console.Clear();
                    String     name;
                    EHeroClass eHeroClass = EHeroClass.barbarzynca;
                    while (true)
                    {
                        Console.WriteLine("Give hero name");
                        name = ValidateName(Console.ReadLine());
                        if (name != "")
                        {
                            while (true)
                            {
                                Console.Clear();
                                Console.WriteLine("Take hero class:\n" +
                                                  "1. barbarzynca\n" +
                                                  "2. paladyn\n" +
                                                  "3. amazonka");
                                if (Enum.TryParse(Console.ReadLine(), out eHeroClass))
                                {
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("You chose incorrect hero class\n" +
                                                      "Try again.");
                                }
                            }
                            break;
                        }
                        else
                        {
                            Console.WriteLine("You gave incorrect hero name\n" +
                                              "Try again.");
                        }
                    }
                    Hero hero = new Hero(name, eHeroClass);
                    Console.Clear();
                    hero.Write();
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                    Console.Clear();
                    NonPlayerCharacter nonPlayerCharacter1 = new NonPlayerCharacter("Akara");
                    Statements         statements          = new Statements(nonPlayerCharacter1, hero);
                    statements.CreateNPCDialogPart1();
                    nonPlayerCharacter1.npcDialogPart = statements.GetNPCDialogPart();
                    NonPlayerCharacter nonPlayerCharacter2 = new NonPlayerCharacter("Deckard");
                    statements = new Statements(nonPlayerCharacter2, hero);
                    statements.CreateNPCDialogPart2();
                    nonPlayerCharacter2.npcDialogPart = statements.GetNPCDialogPart();
                    NonPlayerCharacter nonPlayerCharacter3 = new NonPlayerCharacter("Kashya");
                    statements = new Statements(nonPlayerCharacter3, hero);
                    statements.CreateNPCDialogPart3();
                    nonPlayerCharacter3.npcDialogPart = statements.GetNPCDialogPart();
                    Location location1 = new Location("Silverymoon", true);

                    location1.AddNonPlayerCharacter(nonPlayerCharacter1);
                    location1.AddNonPlayerCharacter(nonPlayerCharacter2);
                    location1.AddNonPlayerCharacter(nonPlayerCharacter3);

                    Location location2 = new Location("Kwazo", true);
                    Location location3 = new Location("Elidary", true);
                    Location location4 = new Location("Manta", false);
                    Location location5 = new Location("Uzupik", false);
                    Location location6 = new Location("Drago", false);


                    listLocations.AddRange(
                        new List <Location>()
                    {
                        location1,
                        location2,
                        location3,
                        location4,
                        location5,
                        location6
                    }
                        );

                    ShowLocation(location1, hero);
                    break;
                }
                else
                {
                    Console.WriteLine("\nYou have chosen the wrong command.\n" +
                                      "Try again.");
                }
            }
        }