Beispiel #1
0
        public static Node ParseProgram()
        {
            //Diagram rules
            //1st -> SubDec | FuncDec
            //SubDec | FuncDec -> SubDec | FuncDec
            //SubDec | FuncDec -> EOF

            Statements root = new Statements();

            /*
             *
             * while(tokens.actual.type != "EOF"){
             *  if(tokens.actual.type == "SUB" || tokens.actual.type == "FUNCTION"){
             *      root.Add(ParseFunction());
             *  }
             *  else {
             *      if(tokens.actual.type == "LINEBREAK"){
             *          CurrentLine++;
             *          tokens.SelectNext();
             *      } else
             *          throw new SystemException ($"Statement line outside functions are not allowed. Got a {tokens.actual.type}.(position {tokens.position}) [Line: {CurrentLine}]");
             *  }
             * }
             *
             */



            //TODO- treat func declarations

            //parse the statements
            while (tokens.actual.type != "EOF")
            {
                root.Add(ParseStatements("main"));
                Expect("PERIOD", true);
            }

            /*
             * //add a main funccall
             * FuncCall mainCall = new FuncCall();
             * mainCall.value = "Main";
             * root.Add(mainCall);
             */

            return(root);
        }
Beispiel #2
0
        public static Node ParseProgram()
        {
            //Diagram rules
            //1st -> SubDec | FuncDec
            //SubDec | FuncDec -> SubDec | FuncDec
            //SubDec | FuncDec -> EOF

            Statements root = new Statements();

            while (tokens.actual.type != "EOF")
            {
                if (tokens.actual.type == "SUB" || tokens.actual.type == "FUNCTION")
                {
                    root.Add(ParseFunction());
                }
                else
                {
                    if (tokens.actual.type == "LINEBREAK")
                    {
                        CurrentLine++;
                        tokens.SelectNext();
                    }
                    else
                    {
                        throw new SystemException($"Statement line outside functions are not allowed. Got a {tokens.actual.type}.(position {tokens.position}) [Line: {CurrentLine}]");
                    }
                }
            }

            //add a main funccall
            FuncCall mainCall = new FuncCall();

            mainCall.value = "main";
            root.Add(mainCall);

            return(root);
        }
Beispiel #3
0
        public static Node ParseStatements()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st -> Begin\n
            //BEGIN\n -> Statement
            //BEGIN\n -> END
            //Statement -> Statement
            //Statement -> END

            Statements root = new Statements();

            /* V2.2 removed START , left here for reference on fiture Handouts
             * //END check
             * bool ended = false;
             *
             *
             * //Statements always start with START
             * if(tokens.actual.type != "START"){
             *  throw new SystemException ($"Statements need to start with a START symbol (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * tokens.SelectNext();
             * if(tokens.actual.type != "LINEBREAK"){
             *  throw new SystemException ($"START needs a linebreak (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * tokens.SelectNext();
             * CurrentLine++;
             */

            while (tokens.actual.type != "EOF")
            {
                /*
                 * if(tokens.actual.type == "START"){
                 *  root.Add(ParseStatements());
                 * }
                 */
                //If line is empty, skip this line;
                if (tokens.actual.type == "LINEBREAK")
                {
                    tokens.SelectNext();
                    CurrentLine++;
                    continue; //double check to see if this does what I think it does!!!
                }

                //Return if ELSE line is found
                if (tokens.actual.type == "ELSE")
                {
                    break;
                }

                if (tokens.actual.type != "END" && tokens.actual.type != "WEND")
                {
                    //Console.WriteLine(tokens.actual.type);
                    root.Add(ParseStatement());
                }
                else
                {
                    //ended = true;
                    tokens.SelectNext();
                    if (tokens.actual.type != "LINEBREAK" && tokens.actual.type != "IF" && tokens.actual.type != "EOF")
                    {
                        throw new SystemException($"Invalid END construction. (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    if (tokens.actual.type == "LINEBREAK")
                    {
                        tokens.SelectNext();
                        CurrentLine++;
                    }
                    break;
                }
            }

            /*
             * if(ended){
             *   return root;
             * }
             * throw new SystemException ($"END symbol not found, probably missing or misplacing a END line (position {tokens.position}) [Line: {CurrentLine}]");
             *
             */
            return(root);
        }
Beispiel #4
0
        public static Node ParseStatements()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st -> Begin\n
            //BEGIN\n -> Statement
            //BEGIN\n -> END
            //Statement -> Statement
            //Statement -> END

            Statements root = new Statements();

            //END check
            bool ended = false;

            //Statements always start with START
            if (tokens.actual.type != "START")
            {
                throw new SystemException($"Statements need to start with a START symbol (position {tokens.position}) [Line: {CurrentLine}]");
            }
            tokens.SelectNext();
            if (tokens.actual.type != "LINEBREAK")
            {
                throw new SystemException($"START needs a linebreak (position {tokens.position}) [Line: {CurrentLine}]");
            }
            tokens.SelectNext();
            CurrentLine++;

            while (tokens.actual.type != "EOF")
            {
                //break lines?
                //
                if (tokens.actual.type == "START")
                {
                    root.Add(ParseStatements());
                }

                if (tokens.actual.type != "END")
                {
                    //Console.WriteLine(tokens.actual.type);
                    root.Add(ParseStatement());
                }
                else
                {
                    ended = true;
                    tokens.SelectNext();
                    if (tokens.actual.type != "LINEBREAK" && tokens.actual.type != "EOF")
                    {
                        throw new SystemException($"END doesnt end the file or break the line (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    if (tokens.actual.type != "EOF")
                    {
                        tokens.SelectNext();
                        CurrentLine++;
                    }
                    break;
                }
            }
            if (ended)
            {
                return(root);
            }
            throw new SystemException($"END symbol not found, probably missing or misplacing a END line (position {tokens.position}) [Line: {CurrentLine}]");
        }
Beispiel #5
0
        public static Node ParseStatements(string statementsType)
        {
            //Console.WriteLine("parsing statemwents");
            //Diagram rules
            //1st -> Begin\n
            //BEGIN\n -> Statement
            //BEGIN\n -> END
            //Statement -> Statement
            //Statement -> END

            Statements root = new Statements();

            // V2.2 removed START , left here for reference on future Handouts
            // V2.3 re-added START (sort of), so it was a good decision not to delete it

            //END check
            bool ended = false;

            /*
             * if(main){ //if these are the main statements, need to start with a sub main declaration
             *  //Statements always start with START (Sub main()\n)
             *  Expect("SUB",true);
             *  Expect("MAIN",true);
             *  Expect("POPEN",true);
             *  Expect("PCLOSE",true);
             *  Expect("LINEBREAK",true);
             *  CurrentLine++;
             * }
             */



            while (tokens.actual.type != "EOF")
            {
                /*
                 * if(tokens.actual.type == "START"){
                 *  root.Add(ParseStatements());
                 * }
                 */
                //If line is empty, skip this line;
                if (tokens.actual.type == "LINEBREAK")
                {
                    tokens.SelectNext();
                    CurrentLine++;
                    continue; //double check to see if this does what I think it does!!!
                }

                List <String> exitTokens = new List <String>()
                {
                    "ELSE", "END", "WEND"
                };

                //Return if ELSE line is found
                if (tokens.actual.type == "ELSE")
                {
                    ended = true;
                    break;
                }

                if (!exitTokens.Contains(tokens.actual.type))
                {
                    root.Add(ParseStatement());
                }
                else
                {
                    switch (tokens.actual.type)
                    {
                    case "ELSE":
                        if (statementsType != "if")
                        {
                            throw new SystemException($"Invalid END construction. 'Else' keyword outside if statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;

                    case "WEND":
                        if (statementsType != "while")
                        {
                            throw new SystemException($"Invalid END construction. 'Wend' keyword outside while statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;

                    case "END":
                        if (statementsType != "func" && statementsType != "sub" && statementsType != "if" && statementsType != "else")
                        {
                            throw new SystemException($"Invalid END construction. 'end' keyword outside funtion statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;
                    }

                    tokens.SelectNext();
                    if (statementsType == "func" || statementsType == "sub")
                    {
                        break;
                    }

                    if (tokens.actual.type != "LINEBREAK" && tokens.actual.type != "IF" && tokens.actual.type != "EOF" && tokens.actual.type != "SUB" && tokens.actual.type != "FUNCTION")
                    {
                        throw new SystemException($"Invalid END construction. (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    if (tokens.actual.type == "LINEBREAK")
                    {
                        tokens.SelectNext();
                        CurrentLine++;
                    }
                    break;
                }
            }

            if (ended)
            {
                return(root);
            }
            throw new SystemException($"END symbol not found, probably missing a correct END line (position {tokens.position}) [Line: {CurrentLine}]");
        }
Beispiel #6
0
        public static Statements ParseStatements(string statementsType)
        {
            //Console.WriteLine("parsing statemwents");

            Statements root = new Statements();


            //Completly new statements parser:

            root.Add(ParseStatement());
            while (tokens.actual.type != "PERIOD")
            {
                Expect("THEN", true);
                root.Add(ParseStatement());
            }
            return(root);

            //END of completlly new parser



            //END check
            bool ended = false;



            while (tokens.actual.type != "EOF")
            {
                /*
                 * if(tokens.actual.type == "START"){
                 *  root.Add(ParseStatements());
                 * }
                 */
                //remove, tokenizer treats this now
                //If line is empty, skip this line;
                if (tokens.actual.type == "LINEBREAK")
                {
                    Console.WriteLine("LINEBREAK on 174 should never happen");
                    tokens.SelectNext();
                    CurrentLine++;
                    continue; //double check to see if this does what I think it does!!!
                }
                //remove, maybe leave the ELSE case. Every statements end with a . now
                List <String> exitTokens = new List <String>()
                {
                    "ELSE", "END", "WEND"
                };

                //Return if ELSE line is found
                if (tokens.actual.type == "ELSE")
                {
                    ended = true;
                    break;
                }

                if (!exitTokens.Contains(tokens.actual.type))
                {
                    root.Add(ParseStatement());
                }
                else
                {
                    switch (tokens.actual.type)
                    {
                    case "ELSE":
                        if (statementsType != "if")
                        {
                            throw new SystemException($"Invalid END construction. 'Else' keyword outside if statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;

                    case "WEND":
                        if (statementsType != "while")
                        {
                            throw new SystemException($"Invalid END construction. 'Wend' keyword outside while statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;

                    case "END":
                        if (statementsType != "func" && statementsType != "sub" && statementsType != "if")
                        {
                            throw new SystemException($"Invalid END construction. 'end' keyword outside funtion statements (position {tokens.position}) [Line: {CurrentLine}]");
                        }
                        ended = true;
                        break;
                    }

                    tokens.SelectNext();
                    if (statementsType == "func" || statementsType == "sub")
                    {
                        break;
                    }

                    if (tokens.actual.type != "LINEBREAK" && tokens.actual.type != "IF" && tokens.actual.type != "EOF" && tokens.actual.type != "SUB" && tokens.actual.type != "FUNCTION")
                    {
                        throw new SystemException($"Invalid END construction. (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    if (tokens.actual.type == "LINEBREAK")
                    {
                        tokens.SelectNext();
                        CurrentLine++;
                    }
                    break;
                }
            }

            if (ended)
            {
                return(root);
            }
            throw new SystemException($"END symbol not found, probably missing a correct END line (position {tokens.position}) [Line: {CurrentLine}]");
        }
Beispiel #7
0
        public static Node ParseStatements(bool main)
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st -> Begin\n
            //BEGIN\n -> Statement
            //BEGIN\n -> END
            //Statement -> Statement
            //Statement -> END

            Statements root = new Statements();

            // V2.2 removed START , left here for reference on future Handouts
            // V2.3 re-added START (sort of), so it was a good decision not to delete it

            //END check
            bool ended = false;

            if (main) //if these are the main statements, need to start with a sub main declaration
            //Statements always start with START (Sub main()\n)
            {
                if (tokens.actual.type != "SUB")
                {
                    throw new SystemException($"Start your program with a Sub main() format (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                if (tokens.actual.type != "MAIN")
                {
                    throw new SystemException($"Start your program with a Sub main() format (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                if (tokens.actual.type != "POPEN")
                {
                    throw new SystemException($"Start your program with a Sub main() format (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                if (tokens.actual.type != "PCLOSE")
                {
                    throw new SystemException($"Start your program with a Sub main() format (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                if (tokens.actual.type != "LINEBREAK")
                {
                    throw new SystemException($"Start your program with a Sub main() format (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
                CurrentLine++;
            }



            while (tokens.actual.type != "EOF")
            {
                /*
                 * if(tokens.actual.type == "START"){
                 *  root.Add(ParseStatements());
                 * }
                 */
                //If line is empty, skip this line;
                if (tokens.actual.type == "LINEBREAK")
                {
                    tokens.SelectNext();
                    CurrentLine++;
                    continue; //double check to see if this does what I think it does!!!
                }

                //Return if ELSE line is found
                if (tokens.actual.type == "ELSE")
                {
                    ended = true;
                    break;
                }

                if (tokens.actual.type != "END" && tokens.actual.type != "WEND")
                {
                    //Console.WriteLine(tokens.actual.type);
                    root.Add(ParseStatement());
                }
                else
                {
                    ended = true;
                    tokens.SelectNext();
                    if (tokens.actual.type != "LINEBREAK" && tokens.actual.type != "IF" && tokens.actual.type != "EOF" && tokens.actual.type != "SUB")
                    {
                        throw new SystemException($"Invalid END construction. (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    if (tokens.actual.type == "LINEBREAK")
                    {
                        tokens.SelectNext();
                        CurrentLine++;
                    }
                    break;
                }
            }

            if (ended)
            {
                return(root);
            }
            throw new SystemException($"END symbol not found, probably missing a correct END line (position {tokens.position}) [Line: {CurrentLine}]");
        }