Пример #1
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());
            }
        }
        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);
                }
            }
        }
Пример #3
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 (, received " + 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 (((Separator)t).Name != ',')
                    {
                        throw new SyntaxErrorException(@"Expected , (comma), received " + 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 ), received " + t, t);
            }

            t = sTokens.Pop();//{
            if (!(t is Parentheses) || ((Parentheses)t).Name != '{')
            {
                throw new SyntaxErrorException("Expected {, received " + 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);
            }
            //Need to check here that the last statement is a return statement
            if (Body.Count == 0)
            {
                throw new SyntaxErrorException("function has no statements", sTokens.LastPop);
            }
            StatetmentBase sb = Body.Last();

            if (!(sb is ReturnStatement))
            {
                throw new SyntaxErrorException("Expected return statement, received " + sb, sTokens.LastPop);//just token? need whole statement
            }
            //Finally, the function should end with }
            Token tEnd = sTokens.Pop();//}

            if (!(tEnd is Parentheses) || ((Parentheses)tEnd).Name != '}')
            {
                throw new SyntaxErrorException("Expected }, received " + tEnd, tEnd);
            }
        }
Пример #4
0
 public override void Parse(TokensStack sTokens)
 {
     throw new NotImplementedException();
 }
Пример #5
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) // ( operator opnd1 opnd2 )
        {
            Token tParentheses = sTokens.Pop();         // (

            if (!(tParentheses is Parentheses))
            {
                throw new SyntaxErrorException("Expected Parentheses '(', recived " + tParentheses, tParentheses);
            }
            NumOfParent++;
            Token tOperator = sTokens.Pop();            // operator

            if (!(tOperator is Operator))
            {
                throw new SyntaxErrorException("Expected Binary Operator, recived " + tOperator, tOperator);
            }
            Operator = tOperator.ToString();
            TokensStack temp = new TokensStack();

            Operand1 = Expression.Create(sTokens);
            Operand1.Parse(sTokens);
            Operand2 = Expression.Create(sTokens);
            Operand2.Parse(sTokens);
            Token tE_Parentheses = sTokens.Pop();

            if (sTokens.Count < 1)
            {
                throw new Exception("Code Terminated");
            }
            if (!(tE_Parentheses is Parentheses) && (tE_Parentheses).ToString().Equals(")"))
            {
                throw new SyntaxErrorException("Expected Parentheses ')', recived " + tE_Parentheses, tE_Parentheses);
            }

            /*while (NumOfParent != 0)
             * {
             *  if (sTokens.Peek(0) is Parentheses && (((Parentheses)sTokens.Peek(0)).Name  == '('))
             *      NumOfParent++;
             *  if (sTokens.Peek(0) is Parentheses && (((Parentheses)sTokens.Peek(0)).Name  == ')'))
             *      NumOfParent--;
             *  temp.Push(sTokens.Pop());
             * }
             * if (sTokens.Peek(0) is Separator && ((Separator)sTokens.Peek(0)).Name == ';') // if ';' exist OR it's BinaryEXPRESSION in functionCall ~ foo(BinEXP) ~ foo((*xy))
             * {
             *  Token tEnd = temp.Pop();
             * }
             * Token tParenthesesEnd = temp.Pop();        // ')' last one
             * if (!(tParenthesesEnd is Parentheses && (((Parentheses)tParenthesesEnd).Name  == ')')))
             *  throw new SyntaxErrorException("Expected Parentheses ')', recived " + tParenthesesEnd, tParenthesesEnd);
             * if (temp.Peek(0) is Parentheses && (((Parentheses)temp.Peek(0)).Name  == ')')) // if opnd2 is Expression - ')' because we ineserted backward from sTokens to temp stack
             * {
             *  TokensStack Operand2Stack = new TokensStack();
             *  Operand2Stack.Push(temp.Pop()); // opernad2 stack <-- ')'
             *  while (!(temp.Peek(0) is Parentheses))
             *      Operand2Stack.Push(temp.Pop()); // operand2 stack <-- 'operator opnd1 opnd2'
             *  Operand2Stack.Push(temp.Pop()); // operand2 stack <-- '('
             *  Operand2 = Expression.Create(Operand2Stack);
             *  Operand2.Parse(Operand2Stack);
             * }
             * else if (!(temp.Peek(0) is Parentheses))// if opnd2 is number or variable
             * {
             *  if (temp.Peek(0) is Identifier)
             *  {
             *      Operand2 = new VariableExpression();
             *      Operand2.Parse(temp);
             *  }
             *  else if (temp.Peek(0) is Number)
             *  {
             *      Operand2 = new NumericExpression();
             *      Operand2.Parse(temp);
             *  }
             * }
             * if (!(temp.Peek(0) is Parentheses))// if opnd1 is Number or variable
             * {
             *  if (temp.Peek(0) is Identifier)
             *  {
             *      Operand1 = new VariableExpression();
             *      Operand1.Parse(temp);
             *  }
             *  else if (temp.Peek(0) is Number)
             *  {
             *      Operand1 = new NumericExpression();
             *      Operand1.Parse(temp);
             *  }
             * }
             * else                          // if opnd1 is EXPRESSION
             * {
             *  TokensStack Operand1Stack = new TokensStack();
             *  while (!(temp.Count < 0))   // reverse EXPRESSION from temp stack to Operand1Stack to hold the legal expression for opnd1
             *      Operand1Stack.Push(temp.Pop());
             *  Operand1 = Expression.Create(Operand1Stack);
             *  Operand1.Parse(Operand1Stack);
             * }*/
        }
Пример #7
0
        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);
            }
        }
Пример #8
0
        public override void Parse(TokensStack sTokens)
        {
            Identifier t = (Identifier)sTokens.Pop();

            Name = t.Name;
        }
Пример #9
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());
                }
            }
        }