Ejemplo n.º 1
0
        public static Node ParseExpression()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st - term
            //term -> minus | plus
            //term -> EOF
            //minus | plus -> term

            Node root = ParseTerm();

            //list to check if token is valid in a term
            List <String> allowedTokens = new List <String>()
            {
                "PLUS", "MINUS"
            };

            while (allowedTokens.Contains(tokens.actual.type))
            {
                //make a new node, current root is the new node left child
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;

                root.value = (tokens.actual.type == "MINUS") ? '-' : '+';
                tokens.SelectNext();
                root.children[1] = ParseTerm();
            }

            return(root);
        }
Ejemplo n.º 2
0
        public static Node ParseTerm()
        {
            //Console.WriteLine("Parsing term");
            //Term rules
            //1st - factor
            //factor -> times | div
            //factor -> exit term
            //times | div -> factor

            //Get first factor
            Node root = ParseFactor();

            //list to check if token is valid in a term
            List <String> allowedTokens = new List <String>()
            {
                "TIMES", "DIV"
            };

            //check if token type is allowed
            while (allowedTokens.Contains(tokens.actual.type))
            {
                //make a new node, current root is the new node left child
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;

                root.value = (tokens.actual.type == "TIMES") ? '*' : '/';
                tokens.SelectNext();
                root.children[1] = ParseFactor();
            }
            return(root);
        }
Ejemplo n.º 3
0
        public static Node ParseRelExpression()
        {
            //Console.WriteLine("parsing relative expression");
            //Diagram rules
            //1st - expression
            //expression -> equal | higher | lower
            //equal | higher| lower -> expression

            Node root = new BinOp();

            root.children[0] = ParseExpression();
            switch (tokens.actual.type)
            {
            case "EQUAL":
                root.value = "==";
                break;

            case "HIGHER":
                root.value = ">";
                break;

            case "LOWER":
                root.value = "<";
                break;
            }
            tokens.SelectNext();
            root.children[1] = ParseExpression();

            return(root);
        }
Ejemplo n.º 4
0
 public void Add(BinOp node)
 {
     if (node.value.ToString() != "vardec")
     {
         throw new SystemException($"Function declaration vardec node is not a vardec. received a {node.value.ToString()}. [Line: {Parser.CurrentLine}]");
     }
     varList.AddLast(node);
 }
Ejemplo n.º 5
0
        public static Node ParseTerm()
        {
            //Console.WriteLine("Parsing term");
            //Term rules
            //1st - factor
            //factor -> times | div | and
            //factor -> exit term
            //times | div | and -> factor

            //Get first factor
            Node root = ParseFactor();

            //TODO check if there 3 are all possibilities
            //list to check if token is valid in a term
            List <String> allowedTokens = new List <String>()
            {
                "TIMES", "DIVIDED"
            };                                                                  //removed and

            //check if token type is allowed
            while (allowedTokens.Contains(tokens.actual.type))
            {
                //make a new node, current root is the new node left child
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;

                switch (tokens.actual.type)
                {
                case "TIMES":
                    root.value = '*';
                    break;

                case "DIVIDED":
                    tokens.SelectNext();
                    Expect("BY", false);
                    root.value = '/';
                    break;

                    /*
                     * case "AND":
                     *  root.value = '&';
                     *  break;
                     */
                }
                tokens.SelectNext();
                root.children[1] = ParseFactor();
            }
            return(root);
        }
Ejemplo n.º 6
0
        public static Node ParseExpression()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st - term
            //term -> minus | plus | or
            //term -> EOF
            //minus | plus | or -> term

            Node root = ParseTerm();

            //TODO verify if only these three possible operations are expressions
            //list to check if token is valid in a term
            List <String> allowedTokens = new List <String>()
            {
                "PLUS", "MINUS"
            };

            while (allowedTokens.Contains(tokens.actual.type))
            {
                //make a new node, current root is the new node left child
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;
                switch (tokens.actual.type)
                {
                case "MINUS":
                    root.value = '-';
                    break;

                case "PLUS":
                    root.value = '+';
                    break;

                    /*
                     * case "OR":
                     *  root.value = '|';
                     *  break;
                     */
                }
                tokens.SelectNext();
                root.children[1] = ParseTerm();
            }

            return(root);
        }
Ejemplo n.º 7
0
        public static Node ParseRelExpression()
        {
            //Console.WriteLine("parsing relative expression");
            //Diagram rules
            //1st - expression
            //expression -> equal | higher | lower
            //equal | higher| lower -> expression
            //expression ->

            Node root = ParseExpression();

            List <String> allowedTokens = new List <String>()
            {
                "EQUAL", "HIGHER", "LOWER"
            };

            if (allowedTokens.Contains(tokens.actual.type))
            {
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;

                switch (tokens.actual.type)
                {
                case "EQUAL":
                    root.value = "==";
                    break;

                case "HIGHER":
                    root.value = ">";
                    break;

                case "LOWER":
                    root.value = "<";
                    break;
                }
                tokens.SelectNext();
                root.children[1] = ParseExpression();
            }

            return(root);
        }
Ejemplo n.º 8
0
        public static Node ParseStatement()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st - Identfier
            //identifier - =
            //= - expression
            //1st - print
            //print - expression
            //1st - statements

            Node root;

            //TODO- condense this code by replacing similar code with some functions (expect line end for example)

            switch (tokens.actual.type)
            {
            case "END":
                return(new NoOp());

            case "IDENTIFIER":
                // IDENTIFIER - = - EXPRESSION
                // get identifier
                Identifier ident = new Identifier((string)tokens.actual.value);
                tokens.SelectNext();
                if (tokens.actual.type != "EQUAL")     //expecting a EQUAL
                {
                    throw new SystemException($"Identifier with no assignment ({tokens.actual.value}) (position {tokens.position}) [Line: {CurrentLine}]");
                }
                root             = new BinOp();        //create the assign biop node
                root.value       = '=';
                root.children[0] = ident;              //left is the identfier
                tokens.SelectNext();
                root.children[1] = ParseExpression();  //right is a expression

                if (tokens.actual.type != "LINEBREAK") //expect linebreak after the expression;
                {
                    throw new SystemException($"No LINEBREAK after assignment (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();     //go to next token after linebreak
                return(root);

            case "PRINT":
                //print - expression
                root       = new UnOp();
                root.value = "print";
                tokens.SelectNext();
                root.children[0] = ParseExpression();
                if (tokens.actual.type != "LINEBREAK")     //expect linebreak after the expression;
                {
                    throw new SystemException($"No LINEBREAK after PRINT (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();     //go to next token after linebreak
                return(root);

            case "WHILE":
                root = new WhileNode();
                tokens.SelectNext();
                root.children[0] = ParseRelExpression();
                if (tokens.actual.type != "LINEBREAK")     //expect linebreak after the expression;
                {
                    throw new SystemException($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();                  //go to next token after linebreak
                root.children[1] = ParseStatements(); //already goes to next line, no select next required
                return(root);

            case "IF":
                root = new IfNode();
                tokens.SelectNext();
                root.children[0] = ParseRelExpression();

                if (tokens.actual.type != "THEN")     //expect THEN after the rel expression;
                {
                    throw new SystemException($"THEN is required after an IF's Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();

                if (tokens.actual.type != "LINEBREAK")     //expect linebreak after THEN;
                {
                    throw new SystemException($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();                  //go to next token after linebreak

                root.children[1] = ParseStatements(); //already goes to next line, no select next required

                if (tokens.actual.type == "ELSE")
                {
                    tokens.SelectNext();
                    if (tokens.actual.type != "LINEBREAK")     //expect linebreak after ELSE;
                    {
                        throw new SystemException($"No LINEBREAK after ELSE (position {tokens.position}) [Line: {CurrentLine}]");
                    }
                    CurrentLine++;
                    tokens.SelectNext();                  //go to next token after linebreak
                    root.children[2] = ParseStatements(); //left the token after the END, so it is probably on IF (END IF\n)
                }

                if (tokens.actual.type != "IF")     //expect IF after the statements END for END IF\n construction;
                {
                    throw new SystemException($"IF section end symbol not found, found END instead of END IF (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();

                if (tokens.actual.type != "LINEBREAK")     //expect linebreak after THEN;
                {
                    throw new SystemException($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();     //go to next token after linebreak

                return(root);

            default:
                return(new NoOp());
            }
        }
Ejemplo n.º 9
0
        public static Node ParseStatement()
        {
            //Console.WriteLine("parsing expression");
            //Diagram rules
            //1st - Identfier
            //identifier - =
            //= - expression
            //1st - print
            //print - expression
            //1st - statements

            Node root;

            switch (tokens.actual.type)
            {
            case "END":
                return(new NoOp());

            case "IDENTIFIER":
                // IDENTIFIER - = - EXPRESSION
                // get identifier
                Identifier ident = new Identifier((string)tokens.actual.value);
                tokens.SelectNext();
                if (tokens.actual.type != "EQUAL")     //expecting a EQUAL
                {
                    throw new SystemException($"Identifier with no assignment ({tokens.actual.value}) (position {tokens.position}) [Line: {CurrentLine}]");
                }
                root             = new BinOp();        //creat the assign biop node
                root.value       = '=';
                root.children[0] = ident;              //left is the identfier
                tokens.SelectNext();
                root.children[1] = ParseExpression();  //right is a expression

                if (tokens.actual.type != "LINEBREAK") //expect linebreak after the expression;
                {
                    throw new SystemException($"No LINEBREAK after assignment (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();     //go to next token after linebreak
                return(root);

            case "PRINT":
                //print - expression
                root       = new UnOp();
                root.value = "print";
                tokens.SelectNext();
                root.children[0] = ParseExpression();
                if (tokens.actual.type != "LINEBREAK")     //expect linebreak after the expression;
                {
                    throw new SystemException($"No LINEBREAK after PRINT (position {tokens.position}) [Line: {CurrentLine}]");
                }
                CurrentLine++;
                tokens.SelectNext();     //go to next token after linebreak
                return(root);

            case "START":
                return(ParseStatements());

            default:
                return(new NoOp());
            }
        }
Ejemplo n.º 10
0
        public static Node ParseFunction()
        {
            FuncDec root = new FuncDec();

            bool sub = tokens.actual.type == "SUB";

            if (sub)
            {
                Expect("SUB", true);
                root.type = "none";
            }
            else
            {
                Expect("FUNCTION", true);;
            }

            Expect("IDENTIFIER", false);
            root.value = tokens.actual.value;
            tokens.SelectNext();

            Expect("POPEN", true);
            if (tokens.actual.type != "PCLOSE")
            {
                do
                {
                    if (tokens.actual.type == "COMMA")
                    {
                        tokens.SelectNext();
                    }
                    Expect("IDENTIFIER", false);
                    BinOp dim = new BinOp();
                    dim.value             = "vardec";
                    dim.children[0]       = new NoOp();
                    dim.children[0].value = (string)tokens.actual.value;
                    tokens.SelectNext();

                    Expect("AS", true);

                    dim.children[1] = parseType();

                    root.Add(dim);
                } while(tokens.actual.type == "COMMA");
            }
            Expect("PCLOSE", true);

            if (!sub)
            {
                Expect("AS", true);
                switch (tokens.actual.type)
                {
                case "INTEGER":
                    root.type = "integer";
                    break;

                case "BOOLEAN":
                    root.type = "boolean";
                    break;

                default:
                    throw new SystemException($"Expecting a VARTYPE token, got a {tokens.actual.type} (position {tokens.position}) [Line: {CurrentLine}]");
                }
                tokens.SelectNext();
            }
            Expect("LINEBREAK", true);
            CurrentLine++;

            root.children[0] = ParseStatements(sub ? "sub":"func");

            if (sub)
            {
                Expect("SUB", true);
            }
            else
            {
                Expect("FUNCTION", true);
            }

            Expect("LINEBREAK", true);
            CurrentLine++;


            return(root);
        }
Ejemplo n.º 11
0
        public static Node ParseRelExpression()
        {
            //Console.WriteLine("parsing relative expression");
            //Diagram rules
            //1st - expression
            //expression -> equal | higher | lower
            //equal | higher| lower -> expression
            //expression ->

            Node root = ParseExpression();

            //TODO make rel expressions
            List <String> allowedTokens = new List <String>()
            {
                "IS", "EQUALS", "NOT"
            };

            if (allowedTokens.Contains(tokens.actual.type))
            {
                Node temp = root;
                root             = new BinOp();
                root.children[0] = temp;

                bool not = false;

                if (tokens.actual.type == "IS")
                {
                    tokens.SelectNext();
                    if (tokens.actual.type == "NOT")
                    {
                        //Node notChild= root;
                        //root = new UnOp();
                        //notNode.value = "not";
                        //root.children[1] = notNode;
                        not = true;
                        tokens.SelectNext();
                    }
                    switch (tokens.actual.type)
                    {
                    case "EQUAL":
                        tokens.SelectNext();
                        Expect("TO", true);
                        root.value = "==";
                        break;

                    case "HIGHER":
                        tokens.SelectNext();
                        Expect("THAN", true);
                        root.value = ">";
                        break;

                    case "LOWER":
                        tokens.SelectNext();
                        Expect("THAN", true);
                        root.value = "<";
                        break;

                    default:
                        root.value = "==";
                        break;
                    }
                }
                else if (tokens.actual.type == "EQUALS")
                {
                    root.value = "==";
                    tokens.SelectNext();
                }
                else if (tokens.actual.type == "NOT")
                {
                    tokens.SelectNext();
                    Expect("EQUALS", true);
                    root.value = "==";
                }

                root.children[1] = ParseExpression();

                //apply not
                if (not)
                {
                    Node oldRoot = root;
                    root             = new UnOp();
                    root.value       = "not";
                    root.children[0] = oldRoot;
                }
            }

            //if and or or, apply it to new relexp
            if (tokens.actual.type == "AND")
            {
                tokens.SelectNext();
                Node oldRoot = root;
                root             = new BinOp();
                root.value       = '&';
                root.children[0] = oldRoot;
                root.children[1] = ParseRelExpression();
            }
            if (tokens.actual.type == "OR")
            {
                tokens.SelectNext();
                Node oldRoot = root;
                root             = new BinOp();
                root.value       = '|';
                root.children[0] = oldRoot;
                root.children[1] = ParseRelExpression();
            }


            return(root);
        }
Ejemplo n.º 12
0
        //public static Node ParseStatements(string poi){
        //    return ParseStatements("none");
        // }

        public static Node ParseStatement()
        {
            //Console.WriteLine("parsing expression");

            Node root;

            //TODO- condense this code by replacing similar code with some functions (expect line end for example)

            switch (tokens.actual.type)
            {
            case "END":
                return(new NoOp());

            //assing, start wuth put
            case "PUT":
                root       = new BinOp();
                root.value = "=";
                tokens.SelectNext();

                root.children[1] = ParseRelExpression();
                Expect("IN", true);
                Expect("IDENTIFIER", false);

                Identifier ident = new Identifier((string)tokens.actual.value);
                root.children[0] = ident;

                tokens.SelectNext();

                return(root);

            /*case "IDENTIFIER":
             *  // IDENTIFIER - = - EXPRESSION
             *  // get identifier
             *  Identifier ident = new Identifier((string)tokens.actual.value);
             *  tokens.SelectNext();
             *  if(tokens.actual.type != "EQUAL"){ //expecting a EQUAL
             *      throw new SystemException ($"Identifier with no assignment ({tokens.actual.value}) (position {tokens.position}) [Line: {CurrentLine}]");
             *  }
             *  root = new BinOp(); //create the assign biop node
             *  root.value = '=';
             *  root.children[0] = ident; //left is the identfier
             *  tokens.SelectNext();
             *  root.children[1] = ParseRelExpression(); //right is a relative expression
             *
             *  if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after the expression;
             *      throw new SystemException ($"No LINEBREAK after assignment (position {tokens.position}) [Line: {CurrentLine}]");
             *  }
             *  CurrentLine++;
             *  tokens.SelectNext(); //go to next token after linebreak
             *  return root;*/
            case "DO":
                tokens.SelectNext();
                Expect("IDENTIFIER", false);

                FuncCall call = new FuncCall();
                call.value = (string)tokens.actual.value;

                tokens.SelectNext();

                if (tokens.actual.type == "WITH")
                {
                    tokens.SelectNext();
                    do
                    {
                        if (tokens.actual.type == "COMMA")
                        {
                            tokens.SelectNext();
                        }
                        call.Add(ParseRelExpression());
                    }while (tokens.actual.type == "COMMA");
                }
                return(call);

            case "TO":
                tokens.SelectNext();
                return(ParseFunction());

            /*
             * case "CALL":
             *  tokens.SelectNext();
             *  Expect("IDENTIFIER",false);
             *
             *  FuncCall call = new FuncCall();
             *  call.value = (string)tokens.actual.value;
             *
             *  tokens.SelectNext();
             *  Expect("POPEN",true);
             *  if(tokens.actual.type != "PCLOSE"){
             *      do{
             *          if(tokens.actual.type == "COMMA"){
             *              tokens.SelectNext();
             *          }
             *          call.Add(ParseRelExpression());
             *      }while (tokens.actual.type == "COMMA");
             *  }
             *  Expect("PCLOSE",true);
             *  return call;
             */
            case "PRINT":
                //print - expression
                root       = new UnOp();
                root.value = "print";
                tokens.SelectNext();
                root.children[0] = ParseRelExpression();
                return(root);    //cl change

            /*
             * if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after the expression;
             *  throw new SystemException ($"No LINEBREAK after PRINT (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * CurrentLine++;
             * tokens.SelectNext(); //go to next token after linebreak
             * return root;
             */
            case "WHILE":
                root = new WhileNode();
                tokens.SelectNext();
                root.children[0] = ParseRelExpression();

                Expect("DO", true);
                root.children[1] = ParseStatements("while");

                Expect("PERIOD", true);

                return(root);

            /*
             * root = new WhileNode();
             * tokens.SelectNext();
             * root.children[0] = ParseRelExpression();
             * if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after the expression;
             *  throw new SystemException ($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * CurrentLine++;
             * tokens.SelectNext(); //go to next token after linebreak
             * root.children[1] = ParseStatements("while"); //already goes to next line, no select next required
             * return root;
             */
            /*
             * case "DIM":
             * root = new BinOp();
             * root.value = "vardec";
             * tokens.SelectNext();
             *
             * if(tokens.actual.type != "IDENTIFIER"){
             *  throw new SystemException ($"IDENTIFIER is required after an variable declaration declaration (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * root.children[0] = new NoOp();
             * root.children[0].value = (string)tokens.actual.value;
             * tokens.SelectNext();
             *
             * if(tokens.actual.type != "AS"){
             *  throw new SystemException ($"'As' is required in an variable declaration (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * tokens.SelectNext();
             *
             * root.children[1] = parseType();
             *
             * if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after the expression;
             *  throw new SystemException ($"No LINEBREAK after Variable declaration(position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * CurrentLine++;
             * tokens.SelectNext(); //go to next token after linebreak
             *
             * return root;
             */
            case "IF":
                root = new IfNode();
                tokens.SelectNext();
                root.children[0] = ParseRelExpression();

                Expect("DO", true);
                root.children[1] = ParseStatements("if");

                Expect("PERIOD", true);

                if (tokens.actual.type == "ELSE")
                {
                    tokens.SelectNext();
                    root.children[2] = ParseStatements("else");
                }

                return(root);

            /*
             * if(tokens.actual.type != "THEN"){ //expect THEN after the rel expression;
             *  throw new SystemException ($"THEN is required after an IF's Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * tokens.SelectNext();
             *
             * if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after THEN;
             *  throw new SystemException ($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * CurrentLine++;
             * tokens.SelectNext(); //go to next token after linebreak
             *
             * root.children[1] = ParseStatements("if"); //already goes to next line, no select next required
             *
             * if(tokens.actual.type == "ELSE"){
             *  tokens.SelectNext();
             *  if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after ELSE;
             *      throw new SystemException ($"No LINEBREAK after ELSE (position {tokens.position}) [Line: {CurrentLine}]");
             *  }
             *  CurrentLine++;
             *  tokens.SelectNext(); //go to next token after linebreak
             *  root.children[2] = ParseStatements("else"); //left the token after the END, so it is probably on IF (END IF\n)
             * }
             *
             * if(tokens.actual.type != "IF"){ //expect IF after the statements END for END IF\n construction;
             *  throw new SystemException ($"IF section end symbol not found, found END instead of END IF (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * tokens.SelectNext();
             *
             * if(tokens.actual.type != "LINEBREAK"){ //expect linebreak after THEN;
             *  throw new SystemException ($"No LINEBREAK after Relative Expression (position {tokens.position}) [Line: {CurrentLine}]");
             * }
             * CurrentLine++;
             * tokens.SelectNext(); //go to next token after linebreak
             *
             * return root; */

            default:
                return(new NoOp());
            }
        }