Exemple #1
0
        public override void Parse(TokensStack sTokens)
        {
            Token tWhile = sTokens.Pop();

            if (!(tWhile is Statement) || !((Statement)tWhile).Name.Equals("while"))
            {
                throw new SyntaxErrorException("$Expected while", tWhile);
            }

            Token con_open = sTokens.Pop();

            if (!(con_open is Parentheses) || ((Parentheses)con_open).Name != '(')
            {
                throw new SyntaxErrorException("$Expected (", con_open);
            }

            Term = Expression.Create(sTokens);
            Term.Parse(sTokens);

            /*while (!(sTokens.Peek() is Parentheses))
             * {
             *  sTokens.Pop();
             * }*/

            Token con_close = sTokens.Pop();

            if (!(con_close is Parentheses) || ((Parentheses)con_close).Name != ')')
            {
                throw new SyntaxErrorException("$Expected )", con_close);
            }

            Token while_open = sTokens.Pop();

            if (!(while_open is Parentheses) || ((Parentheses)while_open).Name != '{')
            {
                throw new SyntaxErrorException("$Expected {", while_open);
            }

            Body = new List <StatetmentBase>();
            while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek());
                statetmentBase.Parse(sTokens);
                Body.Add(statetmentBase);

                if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
                {
                    sTokens.Pop();
                }
            }
            Token while_close = sTokens.Pop();

            if (!(while_close is Parentheses) || ((Parentheses)while_close).Name != '}')
            {
                throw new SyntaxErrorException("$Expected }", while_close);
            }
        }
Exemple #2
0
        public override void Parse(TokensStack sTokens)
        {
            Token tFunc = sTokens.Pop();

            if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function")
            {
                throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc);
            }
            Token tType = sTokens.Pop();

            if (!(tType is VarType))
            {
                throw new SyntaxErrorException("Expected var type, received " + tType, tType);
            }
            ReturnType = VarDeclaration.GetVarType(tType);
            Token tName = sTokens.Pop();

            if (!(tName is Identifier))
            {
                throw new SyntaxErrorException("Expected function name, received " + tType, tType);
            }
            Name = ((Identifier)tName).Name;

            Token t = sTokens.Pop();                                      //(

            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses)) //)
            {
                if (sTokens.Count < 3)
                {
                    throw new SyntaxErrorException("Early termination ", t);
                }
                Token          tArgType = sTokens.Pop();
                Token          tArgName = sTokens.Pop();
                VarDeclaration vc       = new VarDeclaration(tArgType, tArgName);
                Args.Add(vc);
                if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
                {
                    sTokens.Pop();
                }
            }
            t = sTokens.Pop(); //)
            t = sTokens.Pop(); //{
            while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var"))
            {
                VarDeclaration local = new VarDeclaration();
                local.Parse(sTokens);
                Locals.Add(local);
            }
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                s.Parse(sTokens);
                Body.Add(s);
            }
            Token tEnd = sTokens.Pop();//}
        }
Exemple #3
0
        public static StatetmentBase Create(Token t)
        {
            if (t is Statement)
            {
                Statement s = (Statement)t;

                StatetmentBase se = Create(s.Name);
                if (se == null)
                {
                    throw new SyntaxErrorException("Expected statement type" + t.ToString() + "1", t);
                }
                return(se);
            }
            throw new SyntaxErrorException("Expected statement type" + t.ToString() + "2", t);
        }
Exemple #4
0
        public override void Parse(TokensStack sTokens)
        {
            Body = new List <StatetmentBase>();
            if (sTokens.Count < 6)
            {
                throw new SyntaxErrorException("Early termination ", sTokens.LastPop);
            }
            Token tWhile = sTokens.Pop();

            if (!(tWhile is Statement) || ((Statement)tWhile).Name != "while")
            {
                throw new SyntaxErrorException("Expected while statment , received " + tWhile, tWhile);
            }
            Token t = sTokens.Pop(); //(

            if (!(t is Parentheses) || ((Parentheses)t).Name != '(')
            {
                throw new SyntaxErrorException("Expected (  , received ", t);
            }
            Expression pTerm = (Expression.Create(sTokens));

            pTerm.Parse(sTokens);
            Term = pTerm;
            t    = sTokens.Pop(); //(
            if (!(t is Parentheses) || ((Parentheses)t).Name != ')')
            {
                throw new SyntaxErrorException("Expected )  , received ", t);
            }
            t = sTokens.Pop(); //(
            if (!(t is Parentheses) || ((Parentheses)t).Name != '{')
            {
                throw new SyntaxErrorException("Expected {  , received ", t);
            }

            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                s.Parse(sTokens);
                Body.Add(s);
            }
            Token tEnd = sTokens.Pop();//}
        }
Exemple #5
0
        protected static void ParseStatements(TokensStack sTokens, List <StatetmentBase> body)
        {
            Token token = sTokens.Pop(); // '{'

            if (!(token is Parentheses) || ((Parentheses)token).Name != '{')
            {
                throw new SyntaxErrorException($"Expected a '{{' but saw '{token}'", token);
            }

            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                token = sTokens.Peek();
                StatetmentBase statetment = Create(token);
                statetment.Parse(sTokens);
                body.Add(statetment);
            }

            token = sTokens.Pop(); // '}'
            if (!(token is Parentheses) || ((Parentheses)token).Name != '}')
            {
                throw new SyntaxErrorException($"Expected a '}}' but saw '{token}'", token);
            }
        }
Exemple #6
0
        //This is an example of the implementation of the Parse method
        public override void Parse(TokensStack sTokens)
        {
            //We check that the first token is "function"
            Token tFunc = sTokens.Pop();

            if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function")
            {
                throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc);
            }
            //Now there should be the return type. We pop it from the stack, check for errors, and then set the field
            Token tType = sTokens.Pop();

            if (!(tType is VarType))
            {
                throw new SyntaxErrorException("Expected var type, received " + tType, tType);
            }
            ReturnType = VarDeclaration.GetVarType(tType);
            //Next is the function name
            Token tName = sTokens.Pop();

            if (!(tName is Identifier))
            {
                throw new SyntaxErrorException("Expected function name, received " + tType, tType);
            }
            Name = ((Identifier)tName).Name;

            //After the name there should be opening paranthesis for the arguments
            Token t = sTokens.Pop(); //(

            if (!(t is Parentheses) || ((Parentheses)t).Name != '(')
            {
                throw new SyntaxErrorException($"Expected a '(' but saw '{t}'", t);
            }

            //Now we extract the arguments from the stack until we see a closing parathesis
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))//)
            {
                //For each argument there should be a type, and a name
                if (sTokens.Count < 3)
                {
                    throw new SyntaxErrorException("Early termination ", t);
                }
                Token          tArgType = sTokens.Pop();
                Token          tArgName = sTokens.Pop();
                VarDeclaration vc       = new VarDeclaration(tArgType, tArgName);
                Args.Add(vc);
                //If there is a comma, then there is another argument
                if (sTokens.Count > 0 && sTokens.Peek() is Separator) //,
                {
                    t = sTokens.Pop();
                    if (!(t is Separator) || ((Separator)t).Name != ',')
                    {
                        throw new SyntaxErrorException($"Expected a ',' but saw '{t}'", t);
                    }
                }
            }
            //Now we pop out the ) and the {. Note that you need to check that the stack contains the correct symbols here.
            t = sTokens.Pop();//)
            if (!(t is Parentheses) || ((Parentheses)t).Name != ')')
            {
                throw new SyntaxErrorException($"Expected a ')' but saw '{t}'", t);
            }
            t = sTokens.Pop();//{
            if (!(t is Parentheses) || ((Parentheses)t).Name != '{')
            {
                throw new SyntaxErrorException($"Expected a '{{' but saw '{t}'", t);
            }

            //Now we parse the list of local variable declarations
            while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var"))
            {
                VarDeclaration local = new VarDeclaration();
                //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration
                local.Parse(sTokens);
                Locals.Add(local);
            }

            //Now we parse the list of statements
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                //We create the correct Statement type (if, while, return, let) based on the top token in the stack
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                //And call the Parse method of the statement to parse the different parts of the statement
                s.Parse(sTokens);
                Body.Add(s);
            }

            bool hasReturn = false;

            if (Body.Count > 0)
            {
                StatetmentBase lastStatetment = Body[Body.Count - 1];
                Body.RemoveAt(Body.Count - 1);
                if (lastStatetment is ReturnStatement)
                {
                    Return    = (ReturnStatement)lastStatetment;
                    hasReturn = true;
                }
                else
                {
                    hasReturn = false;
                }
            }

            //Need to check here that the last statement is a return statement
            //Finally, the function should end with }
            Token tEnd = sTokens.Pop();//}

            if (!hasReturn)
            {
                throw new SyntaxErrorException("Missing a return statement.", tEnd);
            }
            if (!(tEnd is Parentheses) || ((Parentheses)tEnd).Name != '}')
            {
                throw new SyntaxErrorException($"Expected a '}}' but saw '{t}'", t);
            }
        }
        public override void Parse(TokensStack sTokens)
        {
            Body = new List <StatetmentBase>();
            if (sTokens.Count <= 0)
            {
                throw new SyntaxErrorException("Missing while", new Token());
            }
            else
            {
                Token t1 = sTokens.Pop();

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('('))
                {
                    Token t2 = sTokens.Pop();
                }
                else
                {
                    throw new  SyntaxErrorException("Missing (", sTokens.Pop());
                }
                Term = Expression.Create(sTokens);
                if (Term != null)
                {
                    Term.Parse(sTokens);
                }
                else
                {
                    throw new SyntaxErrorException("Bad exp", new Token());
                }

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')'))
                {
                    Token t3 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing )", sTokens.Pop());
                }

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{'))
                {
                    Token t4 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing {", sTokens.Pop());
                }
                while (/*sTokens.Count > 0 && (((sTokens.Peek() is Parentheses) && !(((Parentheses)sTokens.Peek()).Name.Equals('}'))) || (!(sTokens.Peek() is Parentheses)))*/ sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses))))
                {
                    StatetmentBase st = StatetmentBase.Create(sTokens.Peek());
                    if (st != null)
                    {
                        st.Parse(sTokens);
                        Body.Add(st);
                    }
                    else
                    {
                        throw new SyntaxErrorException("illegal statement", new Token());
                    }
                }
                if (sTokens.Count > 0 && ((sTokens.Peek() is Parentheses)))
                {
                    Token t5 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing }", sTokens.Pop());
                }
            }
        }
Exemple #8
0
        public override void Parse(TokensStack sTokens)
        {
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            if (sTokens.Count < 5)
            {
                throw new SyntaxErrorException("Early termination ", sTokens.LastPop);
            }
            Token tIF = sTokens.Pop();

            if (!(tIF is Statement) || ((Statement)tIF).Name != "if")
            {
                throw new SyntaxErrorException("Expected while statment , received " + tIF, tIF);
            }
            Token t = sTokens.Pop(); //(

            if (!(t is Parentheses) || ((Parentheses)t).Name != '(')
            {
                throw new SyntaxErrorException("Expected (  , received ", t);
            }

            Term = Expression.Create(sTokens);
            Term.Parse(sTokens);

            t = sTokens.Pop(); //)
            if (!(t is Parentheses) || ((Parentheses)t).Name != ')')
            {
                throw new SyntaxErrorException("Expected )  , received ", t);
            }
            t = sTokens.Pop(); //{
            if (!(t is Parentheses) || ((Parentheses)t).Name != '{')
            {
                throw new SyntaxErrorException("Expected {  , received ", t);
            }
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                s.Parse(sTokens);
                DoIfTrue.Add(s);
            }
            t = sTokens.Pop(); //}
            if (sTokens.Count > 3 && sTokens.Peek() is Statement && ((Statement)sTokens.Peek()).Name == "else")
            {
                t = sTokens.Pop();
                if (!(sTokens.Peek() is Parentheses))
                {
                    throw new SyntaxErrorException("Expected {  , received ", sTokens.Peek());
                }
                if (((Parentheses)sTokens.Peek()).Name != '{')
                {
                    throw new SyntaxErrorException("Expected {  , received ", sTokens.Peek());
                }
                t = sTokens.Pop(); //{
                while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
                {
                    StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                    s.Parse(sTokens);
                    DoIfFalse.Add(s);
                }
                Token tEnd = sTokens.Pop();
            }
        }
Exemple #9
0
        public override void Parse(TokensStack sTokens)
        {
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            Token tIf = sTokens.Pop();

            if (!(tIf.ToString().Equals("if")))
            {
                throw new SyntaxErrorException("Expected if statment, recived " + tIf, tIf);
            }
            Token termS_Parentheses = sTokens.Pop();

            if (!(termS_Parentheses is Parentheses) && !(termS_Parentheses).ToString().Equals("("))
            {
                throw new SyntaxErrorException("Expected Parentheses '(', recived " + termS_Parentheses, termS_Parentheses);
            }
            Term = Expression.Create(sTokens);
            Term.Parse(sTokens);
            Token termE_Parentheses = sTokens.Pop();

            if (!(termE_Parentheses is Parentheses) && !(termE_Parentheses).ToString().Equals(")"))
            {
                throw new SyntaxErrorException("Expected Parentheses ')', recived " + termE_Parentheses, termE_Parentheses);
            }
            Token tS_Parentheses = sTokens.Pop();

            if (!(tS_Parentheses is Parentheses) && !(tS_Parentheses).ToString().Equals("{"))
            {
                throw new SyntaxErrorException("Expected Parentheses '{', recived " + tS_Parentheses, tS_Parentheses);
            }
            while (!(sTokens.Peek(0) is Parentheses && ((Parentheses)sTokens.Peek(0)).Name == '}'))
            {
                Token          tStatment  = sTokens.Peek();
                StatetmentBase statetment = StatetmentBase.Create(tStatment);
                statetment.Parse(sTokens);
                DoIfTrue.Add(statetment);
            }
            Token tE_Parentheses = sTokens.Pop();

            if (!(tE_Parentheses is Parentheses) && !(tE_Parentheses).ToString().Equals("}"))
            {
                throw new SyntaxErrorException("Expected Parentheses '}', recived " + tE_Parentheses, tE_Parentheses);
            }
            if (sTokens.Peek(0).ToString().Equals("else"))        // else statment
            {
                Token tElse             = sTokens.Pop();
                Token elseS_Parentheses = sTokens.Pop();
                if (!(elseS_Parentheses is Parentheses) && !(elseS_Parentheses).ToString().Equals("{"))
                {
                    throw new SyntaxErrorException("Expected Parentheses '{', recived " + elseS_Parentheses, elseS_Parentheses);
                }
                while (!(sTokens.Peek(0) is Parentheses && (sTokens.Peek(0)).ToString().Equals("}")))
                {
                    Token          tStatment  = sTokens.Peek();
                    StatetmentBase statetment = StatetmentBase.Create(tStatment);
                    statetment.Parse(sTokens);
                    DoIfFalse.Add(statetment);
                }
                Token elseE_Parentheses = sTokens.Pop();
                if (!(elseE_Parentheses is Parentheses) && !(elseE_Parentheses).ToString().Equals("}"))
                {
                    throw new SyntaxErrorException("Expected Parentheses '}', recived " + elseE_Parentheses, elseE_Parentheses);
                }
            }
        }
        public override void Parse(TokensStack sTokens)
        {
            Token tWhile = sTokens.Pop();

            if (!(tWhile is Statement) || ((Statement)tWhile).Name != "if")
            {
                throw new SyntaxErrorException("Expected function received: " + tWhile, tWhile);
            }
            Token tPar = sTokens.Pop();

            if (!(tPar is Parentheses) || ((Parentheses)tPar).Name != '(')
            {
                throw new SyntaxErrorException("Expected var type, received " + tPar, tPar);
            }
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                Expression local = Expression.Create(sTokens);
                //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration
                local.Parse(sTokens);
                Term = local;
            }
            Token tPar1 = sTokens.Pop();

            if (!(tPar1 is Parentheses) || ((Parentheses)tPar1).Name != ')')
            {
                throw new SyntaxErrorException("Expected function received: " + tPar1, tPar1);
            }
            Token tPar2 = sTokens.Pop();

            if (!(tPar2 is Parentheses) || ((Parentheses)tPar1).Name != '{')
            {
                throw new SyntaxErrorException("Expected function received: " + tPar2, tPar2);
            }
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase state = StatetmentBase.Create(sTokens.Peek());
                //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration
                state.Parse(sTokens);
                DoIfTrue.Add(state);
            }
            Token tPar3 = sTokens.Pop();

            if (!(tPar3 is Parentheses) || ((Parentheses)tPar1).Name != '}')
            {
                throw new SyntaxErrorException("Expected function received: " + tPar3, tPar3);
            }
            if (sTokens.Count > 0 && sTokens.Peek() is Statement && ((Statement)sTokens.Pop()).Name.Equals("else"))
            {
                Token tPar4 = sTokens.Pop();
                if (!(tPar4 is Parentheses) || ((Parentheses)tPar4).Name != '{')
                {
                    throw new SyntaxErrorException("Expected function received: " + tPar4, tPar4);
                }
                while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
                {
                    StatetmentBase state = StatetmentBase.Create(sTokens.Peek());
                    //We call the Parse method of the VarDeclaration, which is responsible to parsing the elements of the variable declaration
                    state.Parse(sTokens);
                    DoIfFalse.Add(state);
                }
                Token tPar5 = sTokens.Pop();
                if (!(tPar5 is Parentheses) || ((Parentheses)tPar1).Name != '}')
                {
                    throw new SyntaxErrorException("Expected function received: " + tPar5, tPar5);
                }
            }
        }
Exemple #11
0
        public override void Parse(TokensStack sTokens)
        {
            /*
             * Function Type ID( args ){
             * varDec*
             * Statments*
             * Return Statment
             * }
             *
             * check more errors
             */
            Token t;

            StackIsNotEmpty(sTokens);
            Token tFunc = sTokens.Pop();

            if (!(tFunc is Statement) || ((Statement)tFunc).Name != "function")
            {
                throw new SyntaxErrorException("Expected function received: " + tFunc, tFunc);
            }
            StackIsNotEmpty(sTokens);
            Token tType = sTokens.Pop();

            if (!(tType is VarType))
            {
                throw new SyntaxErrorException("Expected var type, received " + tType, tType);
            }
            ReturnType = VarDeclaration.GetVarType(tType);
            StackIsNotEmpty(sTokens);
            Token tName = sTokens.Pop();

            if (!(tName is Identifier))
            {
                throw new SyntaxErrorException("Expected function name, received " + tType, tType);
            }
            Name = ((Identifier)tName).Name;
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(")
            {
                t = sTokens.Pop(); //(
            }
            else
            {
                throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                if (sTokens.Count < 3)
                {
                    throw new SyntaxErrorException("Early termination ", t);
                }
                Token          tArgType = sTokens.Pop();
                Token          tArgName = sTokens.Pop();
                VarDeclaration vc       = new VarDeclaration(tArgType, tArgName);
                Args.Add(vc);
                if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
                {
                    sTokens.Pop();
                }
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")")
            {
                t = sTokens.Pop(); //)
            }
            else
            {
                throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{")
            {
                t = sTokens.Pop(); //{
            }
            else
            {
                throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            while (sTokens.Count > 0 && (sTokens.Peek() is Statement) && (((Statement)sTokens.Peek()).Name == "var"))
            {
                VarDeclaration local = new VarDeclaration();
                local.Parse(sTokens);
                Locals.Add(local);
            }
            StackIsNotEmpty(sTokens);
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                s.Parse(sTokens);
                Body.Add(s);
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}")
            {
                t = sTokens.Pop(); //}
            }
            else
            {
                throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
        }
Exemple #12
0
        public override void Parse(TokensStack sTokens)
        {
            //init lists
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            //check for 'if'
            Token token = sTokens.Pop();

            if (!(token is Keyword) || ((Keyword)token).Name != "if")
            {
                throw new SyntaxErrorException("expected 'if', received " + token, token);
            }

            //check for '('
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '(')
            {
                throw new SyntaxErrorException("expected '(' for if condition, received " + token, token);
            }

            //define and parse condition
            Term = Expression.Create(sTokens);
            Term.Parse(sTokens);

            //check for ')'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != ')')
            {
                throw new SyntaxErrorException("expected ')' for if condition, received " + token, token);
            }

            //check for '{'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '{')
            {
                throw new SyntaxErrorException("expected '{' for if body, received " + token, token);
            }

            //create body for 'if true'
            token = sTokens.Peek();
            StatetmentBase statetment = null;

            if (token is Keyword)
            {
                statetment = StatetmentBase.Create(((Keyword)token).Name);
            }
            while (statetment != null)
            {
                statetment.Parse(sTokens);
                DoIfTrue.Add(statetment);
                token      = sTokens.Peek();
                statetment = null;
                if (token is Keyword)
                {
                    statetment = StatetmentBase.Create(((Keyword)token).Name);
                }
            }

            //check for '}'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '}')
            {
                throw new SyntaxErrorException("expected '}' for if body, received " + token, token);
            }

            //check for 'else'
            token = sTokens.Peek();
            if ((token is Keyword) && ((Keyword)token).Name == "else")
            {
                sTokens.Pop();

                //check for '{'
                token = sTokens.Pop();
                if (!(token is Parentheses) || ((Parentheses)token).Name != '{')
                {
                    throw new SyntaxErrorException("expected '{' for else body, received " + token, token);
                }

                //create body for 'else'
                statetment = null;
                token      = sTokens.Peek();
                if (token is Keyword)
                {
                    statetment = StatetmentBase.Create(((Keyword)token).Name);
                }
                while (statetment != null)
                {
                    statetment.Parse(sTokens);
                    DoIfFalse.Add(statetment);
                    token      = sTokens.Peek();
                    statetment = null;
                    if (token is Keyword)
                    {
                        statetment = StatetmentBase.Create(((Keyword)token).Name);
                    }
                }

                //check for '}'
                token = sTokens.Pop();
                Console.WriteLine(this);
                if (!(token is Parentheses) || ((Parentheses)token).Name != '}')
                {
                    throw new SyntaxErrorException("expected '}' for else body, received " + token, token);
                }
            }
        }
        public override void Parse(TokensStack sTokens)
        {
            //check for 'while'
            Token token = sTokens.Pop();

            if (!(token is Keyword) || ((Keyword)token).Name != "while")
            {
                throw new SyntaxErrorException("expected 'while', received " + token, token);
            }

            //check for '('
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '(')
            {
                throw new SyntaxErrorException("expected '(' for while condition, received " + token, token);
            }

            //define and parse condition
            Term = Expression.Create(sTokens);
            Term.Parse(sTokens);

            //check for ')'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != ')')
            {
                throw new SyntaxErrorException("expected ')' for while condition, received " + token, token);
            }

            //check for '{'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '{')
            {
                throw new SyntaxErrorException("expected '{' for while body, received " + token, token);
            }

            //create body for while
            Body  = new List <StatetmentBase>();
            token = sTokens.Peek();
            StatetmentBase statetment = null;

            if (token is Keyword)
            {
                statetment = StatetmentBase.Create(((Keyword)token).Name);
            }
            while (statetment != null)
            {
                statetment.Parse(sTokens);
                Body.Add(statetment);
                token      = sTokens.Peek();
                statetment = null;
                if (token is Keyword)
                {
                    statetment = StatetmentBase.Create(((Keyword)token).Name);
                }
            }

            //check for '}'
            token = sTokens.Pop();
            if (!(token is Parentheses) || ((Parentheses)token).Name != '}')
            {
                throw new SyntaxErrorException("expected '}' for while body, received " + token, token);
            }
        }
Exemple #14
0
        public override void Parse(TokensStack sTokens)
        {
            /*
             * 'if' '('Expression')' '{' Statment* '}' ('else' '{' Statment* '}' )
             */
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            Token t;

            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Statement && sTokens.Peek().ToString() == "if")
            {
                Token tStartIF = sTokens.Pop(); //if
            }
            else
            {
                throw new SyntaxErrorException("Expected if statment received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(")
            {
                t = sTokens.Pop(); //(
            }
            else
            {
                throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            while ((sTokens.Count > 0 && !(sTokens.Peek() is Parentheses) || (sTokens.Peek().ToString() != ")")))
            {
                if (sTokens.Count < 3)
                {
                    throw new SyntaxErrorException("Early termination ", t);
                }
                Term = Expression.Create(sTokens);
                if (Term == null)
                {
                    t = new Token();
                    throw new SyntaxErrorException("Invalid Exception", t);
                }
                Term.Parse(sTokens);
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")")
            {
                t = sTokens.Pop(); //)
            }
            else
            {
                throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{")
            {
                t = sTokens.Pop(); //{
            }
            else
            {
                throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            //DoIftrue
            StackIsNotEmpty(sTokens);
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase sTrue = StatetmentBase.Create(sTokens.Peek());
                sTrue.Parse(sTokens);
                DoIfTrue.Add(sTrue);
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}")
            {
                t = sTokens.Pop(); //}
            }
            else
            {
                throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            //DoIfFalse
            if (sTokens.Peek().ToString() == "else")
            {
                if (sTokens.Peek() is Statement && sTokens.Peek().ToString() == "else")
                {
                    Token tStartELSE = sTokens.Pop(); //else
                }
                else
                {
                    throw new SyntaxErrorException("Expected else statment received: " + sTokens.Peek().ToString(), sTokens.Peek());
                }
                StackIsNotEmpty(sTokens);
                if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{")
                {
                    t = sTokens.Pop(); //{
                }
                else
                {
                    throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
                }
                while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
                {
                    StatetmentBase sFalse = StatetmentBase.Create(sTokens.Peek());
                    sFalse.Parse(sTokens);
                    DoIfFalse.Add(sFalse);
                }
                StackIsNotEmpty(sTokens);
                if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}")
                {
                    t = sTokens.Pop(); //}
                }
                else
                {
                    throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
                }
            }
        }
Exemple #15
0
        public override void Parse(TokensStack sTokens)
        {
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            if (((Statement)sTokens.Peek()).Name.Equals("if"))
            {
                Token tIf = sTokens.Pop();
                if (!(tIf is Statement) || !((Statement)tIf).Name.Equals("if"))
                {
                    throw new SyntaxErrorException("$Expected if", tIf);
                }

                Token con_open = sTokens.Pop();
                if (!(con_open is Parentheses) || ((Parentheses)con_open).Name != '(')
                {
                    throw new SyntaxErrorException("$Expected (", con_open);
                }

                Term = Expression.Create(sTokens);
                Term.Parse(sTokens);

                /* while (!(sTokens.Peek() is Parentheses))
                 * {
                 *   sTokens.Pop();
                 * }*/
                Token con_close = sTokens.Pop();
                if (!(con_close is Parentheses) || ((Parentheses)con_close).Name != ')')
                {
                    throw new SyntaxErrorException("$Expected )", con_close);
                }

                Token open_if = sTokens.Pop();
                if (!(open_if is Parentheses) || ((Parentheses)open_if).Name != '{')
                {
                    throw new SyntaxErrorException("$Expected {", open_if);
                }


                while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses))
                {
                    StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek());
                    statetmentBase.Parse(sTokens);
                    DoIfTrue.Add(statetmentBase);

                    if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
                    {
                        sTokens.Pop();
                    }
                }

                Token close_if = sTokens.Pop();
                if (!(close_if is Parentheses) || ((Parentheses)close_if).Name != '}')
                {
                    throw new SyntaxErrorException("$Expected }", close_if);
                }

                if ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name.Equals("else"))
                {
                    Token tElse = sTokens.Pop();
                    if (!(tElse is Statement) || !((Statement)tElse).Name.Equals("else"))
                    {
                        throw new SyntaxErrorException("$Expected else" + tElse.ToString(), tElse);
                    }


                    Token open_else = sTokens.Pop(); // not must
                    if (!(open_else is Parentheses) || ((Parentheses)open_else).Name != '{')
                    {
                        throw new SyntaxErrorException("$Expected {", open_else);
                    }


                    while (sTokens.Count > 0 & !(sTokens.Peek() is Parentheses))
                    {
                        StatetmentBase statetmentBase = StatetmentBase.Create(sTokens.Peek());
                        statetmentBase.Parse(sTokens);
                        DoIfFalse.Add(statetmentBase);

                        if (sTokens.Count > 0 && sTokens.Peek() is Separator)//,
                        {
                            sTokens.Pop();
                        }
                    }


                    Token close_else = sTokens.Pop();
                    if (!(close_else is Parentheses) || ((Parentheses)close_else).Name != '}')
                    {
                        throw new SyntaxErrorException("$Expected }", close_else);
                    }
                }
            }
        }
        public override void Parse(TokensStack sTokens)
        {
            DoIfTrue  = new List <StatetmentBase>();
            DoIfFalse = new List <StatetmentBase>();
            if (sTokens.Count <= 0)
            {
                throw new SyntaxErrorException("Missing if", new Token());
            }
            else
            {
                Token t1 = sTokens.Pop();
                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('('))
                {
                    Token t2 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing (", sTokens.Pop());
                }
                Term = Expression.Create(sTokens);
                if (Term != null)
                {
                    Term.Parse(sTokens);
                }
                else
                {
                    throw new SyntaxErrorException("Bad Exp", sTokens.Pop());
                }

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals(')'))
                {
                    Token t3 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing )", sTokens.Pop());
                }

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{'))
                {
                    Token t4 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing {", sTokens.Pop());
                }
                while (sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses))))
                {
                    StatetmentBase st = StatetmentBase.Create(sTokens.Peek()); //maybe need fix
                    if (st != null)
                    {
                        st.Parse(sTokens);
                        DoIfTrue.Add(st);
                    }
                    else
                    {
                        throw new SyntaxErrorException("illegal exp", sTokens.Pop());
                    }
                }

                if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('}'))
                {
                    Token t3 = sTokens.Pop();
                }
                else
                {
                    throw new SyntaxErrorException("Missing }", sTokens.Pop());
                }

                if (sTokens.Peek() is Statement && ((Statement)sTokens.Peek()).Name.Equals("else"))
                {
                    Token t4 = sTokens.Pop();

                    if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('{'))
                    {
                        Token t5 = sTokens.Pop();
                    }
                    else
                    {
                        throw new SyntaxErrorException("Missing (", sTokens.Pop());
                    }
                    while (/*sTokens.Count > 0 && (((sTokens.Peek() is Parentheses) && ((Parentheses)sTokens.Peek()).Name.Equals('}')) || (!(sTokens.Peek() is Parentheses)))*/ sTokens.Count > 0 && ((!(sTokens.Peek() is Parentheses))))///
                    {
                        StatetmentBase st2 = StatetmentBase.Create(sTokens.Peek());
                        if (st2 != null)
                        {
                            st2.Parse(sTokens);
                            DoIfFalse.Add(st2);
                        }
                        else
                        {
                            throw new SyntaxErrorException("illegal exp", sTokens.Pop());
                        }
                    }

                    if (sTokens.Peek() is Parentheses && ((Parentheses)sTokens.Peek()).Name.Equals('}'))
                    {
                        Token t6 = sTokens.Pop();
                    }
                    else
                    {
                        throw new SyntaxErrorException("Missing }", sTokens.Pop());
                    }
                }
                else
                {
                }
            }
        }
Exemple #17
0
        public override void Parse(TokensStack sTokens)
        {
            /*
             * 'while' '(' expression ')' '{' statement* '}'
             */
            Body = new List <StatetmentBase>();
            Token t;

            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Statement && sTokens.Peek().ToString() == "while")
            {
                Token tStartWhile = sTokens.Pop(); //while
            }
            else
            {
                throw new SyntaxErrorException("Expected while statment received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "(")
            {
                t = sTokens.Pop();  //(
            }
            else
            {
                throw new SyntaxErrorException("Expected ( Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            while ((sTokens.Count > 0 && !(sTokens.Peek() is Parentheses) || (sTokens.Peek().ToString() != ")")))
            {
                if (sTokens.Count < 3)
                {
                    throw new SyntaxErrorException("Early termination ", t);
                }
                Term = Expression.Create(sTokens);
                if (Term == null)
                {
                    t = new Token();
                    throw new SyntaxErrorException("Invalid Exception", t);
                }
                Term.Parse(sTokens);
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == ")")
            {
                t = sTokens.Pop(); //)
            }
            else
            {
                throw new SyntaxErrorException("Expected ) Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "{")
            {
                t = sTokens.Pop(); //{
            }
            else
            {
                throw new SyntaxErrorException("Expected { Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
            StackIsNotEmpty(sTokens);
            while (sTokens.Count > 0 && !(sTokens.Peek() is Parentheses))
            {
                StatetmentBase s = StatetmentBase.Create(sTokens.Peek());
                s.Parse(sTokens);
                Body.Add(s);
            }
            StackIsNotEmpty(sTokens);
            if (sTokens.Peek() is Parentheses && sTokens.Peek().ToString() == "}")
            {
                t = sTokens.Pop(); //}
            }
            else
            {
                throw new SyntaxErrorException("Expected } Parentheses received: " + sTokens.Peek().ToString(), sTokens.Peek());
            }
        }