예제 #1
0
파일: JackProgram.cs 프로젝트: boaz23/ECS
        public override void Parse(TokensStack sTokens)
        {
            Globals = new List <VarDeclaration>();
            while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var")
            {
                VarDeclaration global = new VarDeclaration();
                global.Parse(sTokens);
                Globals.Add(global);
            }
            Functions = new List <Function>();
            while (sTokens.Count > 0)
            {
                if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function")
                {
                    throw new SyntaxErrorException("Expected function", sTokens.Peek());
                }
                Function f = new Function();
                f.Parse(sTokens);
                Functions.Add(f);
            }

            if (Functions.Count > 0)
            {
                Function f = Functions[Functions.Count - 1];
                Functions.RemoveAt(Functions.Count - 1);
                Main = f;
            }
            else
            {
                throw new Exception("Missing main method");
            }
        }
 public override void Parse(TokensStack sTokens)
 {
     Globals = new List <VarDeclaration>();
     while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var")
     {
         VarDeclaration global = new VarDeclaration();
         global.Parse(sTokens);
         Globals.Add(global);
     }
     Main = new Function();
     Main.Parse(sTokens);
     Functions = new List <Function>();
     while (sTokens.Count > 0)
     {
         if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function")
         {
             throw new SyntaxErrorException("Expected function", sTokens.Peek());
         }
         Function f = new Function();
         f.Parse(sTokens);
         Functions.Add(f);
         throw new Exception(string.Join("\n", f.ToString()));
     }
     //  throw new Exception(string.Join("\n", Functions.ToArray().ToString()));
 }
예제 #3
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();//}
        }
        public List <VarDeclaration> ParseVarDeclarations(List <string> lVarLines)
        {
            List <VarDeclaration> lVars = new List <VarDeclaration>();

            for (int i = 0; i < lVarLines.Count; i++)
            {
                List <Token>   lTokens = Tokenize(lVarLines[i], i);
                TokensStack    stack   = new TokensStack(lTokens);
                VarDeclaration var     = new VarDeclaration();
                var.Parse(stack);
                lVars.Add(var);
            }
            return(lVars);
        }
예제 #5
0
        public override void Parse(TokensStack sTokens)
        {
            Globals = new List <VarDeclaration>();
            while ((sTokens.Peek() is Statement) && ((Statement)sTokens.Peek()).Name == "var")
            {
                VarDeclaration global = new VarDeclaration();
                global.Parse(sTokens);
                Globals.Add(global);
            }

            /*
             * function int main(){
             * varDecl
             * Statment*
             * Return Statment
             * }
             */
            Main = new Function();
            Main.Parse(sTokens);
            Functions = new List <Function>();
            if (Main.Name != "main")
            {
                throw new Exception("The first func must be main");
            }
            if (Main.Args.Count() > 0)
            {
                throw new Exception("the main func recieves no Arguments");
            }
            while (sTokens.Count > 0)
            {
                if (!(sTokens.Peek() is Statement) || ((Statement)sTokens.Peek()).Name != "function")
                {
                    throw new SyntaxErrorException("Expected function", sTokens.Peek());
                }
                Function f = new Function();
                f.Parse(sTokens);
                Functions.Add(f);
            }
        }
예제 #6
0
파일: Function.cs 프로젝트: boaz23/ECS
        //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);
            }
        }
예제 #7
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());
            }
        }